如何更改 Apache Commons Email 中的字符集?

发布于 2024-11-03 09:04:29 字数 1431 浏览 1 评论 0原文

我正在使用 Apache Commons 电子邮件向我的客户发送电子邮件,但我有一个名为“Semana da Computação”(葡萄牙语 BR)的客户端,但它是“Semana da Computação”。 我尝试修改我的代码,但没有任何效果:

public static boolean emailWithImage(String subject, String message, String emailReceiver, String imageURL) {
    HtmlEmail email = new HtmlEmail();
    email.setCharset("UTF-8"); // I change here, but it is not working
    email.setHostName(Constantes.EMAIL_HOST_NAME);
    email.setSmtpPort(587);
    DefaultAuthenticator authenticator =
            new DefaultAuthenticator(Constantes.EMAIL_USER, Constantes.EMAIL_PASSWORD);
    email.setAuthenticator(authenticator);
    email.setTLS(true);

    try {
        email.setFrom(Constantes.EMAIL_USER, Constantes.EMAIL_NAME);
        email.setSubject(subject);
        email.addTo(emailReceiver);

        URL url = new URL(imageURL);
        String cid = email.embed(url, "image");        /* it must be 'cid' the name of the image */

        email.setHtmlMsg("<html><img src=\"cid:" + cid + "\"> <p>" + message + "</p> </html>"); /* set the html message */
        email.setTextMsg(message);                     /* send a alternative text in case when the email reader don't support html */

        email.send();
    } catch (EmailException ex) {
        return false;
    } catch (MalformedURLException ex) {
        return false;
    }

    return true;
}

有什么想法吗?为什么名称不能正确显示?如何修复?

I'm using Apache Commons email to send email to my clients, but I have a client called 'Semana da Computação' (in portugues BR) but it goes 'Semana da Computação' .
I try to modify my code, but it nothing working:

public static boolean emailWithImage(String subject, String message, String emailReceiver, String imageURL) {
    HtmlEmail email = new HtmlEmail();
    email.setCharset("UTF-8"); // I change here, but it is not working
    email.setHostName(Constantes.EMAIL_HOST_NAME);
    email.setSmtpPort(587);
    DefaultAuthenticator authenticator =
            new DefaultAuthenticator(Constantes.EMAIL_USER, Constantes.EMAIL_PASSWORD);
    email.setAuthenticator(authenticator);
    email.setTLS(true);

    try {
        email.setFrom(Constantes.EMAIL_USER, Constantes.EMAIL_NAME);
        email.setSubject(subject);
        email.addTo(emailReceiver);

        URL url = new URL(imageURL);
        String cid = email.embed(url, "image");        /* it must be 'cid' the name of the image */

        email.setHtmlMsg("<html><img src=\"cid:" + cid + "\"> <p>" + message + "</p> </html>"); /* set the html message */
        email.setTextMsg(message);                     /* send a alternative text in case when the email reader don't support html */

        email.send();
    } catch (EmailException ex) {
        return false;
    } catch (MalformedURLException ex) {
        return false;
    }

    return true;
}

Any ideas? Why is the name not coming through correctly and how can I fix it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

霞映澄塘 2024-11-10 09:04:29

这也有效,

email.setCharset("utf-8");

或者如果您使用的是 1.3,

email.setCharset(org.apache.commons.mail.EmailConstants.UTF_8);

This also works,

email.setCharset("utf-8");

Alternatively if you are using 1.3,

email.setCharset(org.apache.commons.mail.EmailConstants.UTF_8);
原来分手还会想你 2024-11-10 09:04:29

如果你不做 setHtmlMessage(...) ,而是做

email.addPart("<html>body here</html>", "text/html;charset=UTF-8");

另一种选择是尝试将字符集放入 html 中,那么它可能会起作用,

email.setHtmlMsg("<html><head><META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=UTF-8\"></head>body here</html>");

It might work if instead of doing setHtmlMessage(...), you do

email.addPart("<html>body here</html>", "text/html;charset=UTF-8");

Another alternative is to try and put the charset in the html,

email.setHtmlMsg("<html><head><META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=UTF-8\"></head>body here</html>");
心房的律动 2024-11-10 09:04:29

我遇到了同样的问题,为了解决它,我将编码设置为 ISO-8859-1,现在它可以工作了。

I faced the same issue, and to resolve it, I have set the encoding to ISO-8859-1 and now it is working.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文