【发布时间】:2017-03-28 21:36:50
【问题描述】:
我正在发送带有 html 文本和附件的电子邮件并收到错误消息:
java.io.UnsupportedEncodingException: text/html
代码是:
public void emailMessage(String emailSubject, String message, String emailaddress, String imagePath) {
//Send an email
try {
//Send an email
SimpleEmail email = new SimpleEmail();
email.setHostName("mail.org");
email.setSmtpPort(25); //No authentication required
email.setFrom("address.org");
email.addTo(emailaddress);
email.setSubject(emailSubject);
email.setCharset("utf-8");
// Set the email message text.
MimeBodyPart messagePart = new MimeBodyPart();
messagePart.setText(message, "text/html");
// Set the email attachment file
FileDataSource fileDataSource = new FileDataSource(imagePath);
MimeBodyPart attachmentPart = new MimeBodyPart();
attachmentPart.setDataHandler(new DataHandler(fileDataSource));
attachmentPart.setFileName(fileDataSource.getName());
// Create Multipart E-Mail.
MimeMultipart multipart = new MimeMultipart();
multipart.addBodyPart(messagePart);
multipart.addBodyPart(attachmentPart);
email.setContent(multipart);
//Send the email
email.send();
} catch (EmailException e) {
e.printStackTrace();
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
最初我发送一封没有附件的电子邮件,但它有效。然后我为附件添加了多部分,文本/html不再有效。
【问题讨论】:
-
这个 Jakata 通用电子邮件 API 是解决您的问题的另一种方法 commons.apache.org/proper/commons-email ,这个 API 也适用于邮件处理
标签: java email html-email