Val*_*lva 14 java email apache-commons-email
我正在使用Apache Commons电子邮件向我的客户发送电子邮件,但我有一个名为'SemanadaComputação'的客户(在portugues BR中),但它是'SemanadaComputaçã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;
}
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?为什么名称没有正确通过,我该如何解决?
nyl*_*und 23
这也有效,
email.setCharset("utf-8");
Run Code Online (Sandbox Code Playgroud)
或者,如果您使用1.3,
email.setCharset(org.apache.commons.mail.EmailConstants.UTF_8);
Run Code Online (Sandbox Code Playgroud)
如果你不做 setHtmlMessage(...) ,它可能会起作用
email.addPart("<html>body here</html>", "text/html;charset=UTF-8");
Run Code Online (Sandbox Code Playgroud)
另一种选择是尝试将字符集放入 html 中,
email.setHtmlMsg("<html><head><META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=UTF-8\"></head>body here</html>");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9439 次 |
| 最近记录: |