如何更改 Apache Commons Email 中的字符集?
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这也有效,
或者如果您使用的是 1.3,
This also works,
Alternatively if you are using 1.3,
如果你不做 setHtmlMessage(...) ,而是做
另一种选择是尝试将字符集放入 html 中,那么它可能会起作用,
It might work if instead of doing setHtmlMessage(...), you do
Another alternative is to try and put the charset in the html,
我遇到了同样的问题,为了解决它,我将编码设置为 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.