我必须通过电子邮件发送HTML文件,但不是作为附件。
Message simpleMessage = new MimeMessage(mailSession);
try {
fromAddress = new InternetAddress(from);
toAddress = new InternetAddress(to);
} catch (AddressException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
simpleMessage.setFrom(fromAddress);
simpleMessage.setRecipient(RecipientType.TO, toAddress);
simpleMessage.setSubject(subject);
simpleMessage.setText(text);
Transport.send(simpleMessage);
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
它只是通过文本消息发送电子邮件。我想发送存储在另一个文件中但不是作为附件的HTML内容
发布于 2011-03-08 06:29:25
不要将你的MimeMessage
上传到Message
MimeMessage simpleMessage = new MimeMessage(mailSession);
然后,当您想要设置消息正文时,调用
simpleMessage.setText(text, "utf-8", "html");
或呼叫
simpleMessage.setContent(text, "text/html; charset=utf-8");
如果您希望使用utf-8
以外的字符集,请在适当的位置替换它。
JavaMail有一个额外的、无用的抽象层,通常会让您持有Multipart
、Message
和Address
等类,这些类的功能都比实际正在构造的真正的子类(MimeMultipart
、MimeMessage
和InternetAddress
)少得多……
https://stackoverflow.com/questions/5223079
复制相似问题