我尝试使用以下代码片段发送邮件:
message = new MimeMessage(mailSession);
message.setFrom(from);
message.setRecipients(Message.RecipientType.TO, to);
if(cc != null && cc.length > 0){
message.setRecipients(Message.RecipientType.CC, cc);
}
if(bcc != null && bcc.length > 0){
message.setRecipients(Message.RecipientType.BCC, bcc);
}
if(replyTo != null && replyTo.length > 0){
message.setReplyTo(replyTo);
}
message.setSubject(subject, "utf-8");
message.setSentDate(new java.util.Date());
if (headers != null && !headers.isEmpty()) {
for (String headName : headers.keySet()) {
message.addHeader(headName, headers.get(headName));
}
}
if (Utils.isEmpty(bodyText)) {
bodyText = HTMLHelper.html2text(body);
}
message.setContent(this.buildMessageBody(body, bodyText));
transporter = mailSession.getTransport();
transporter.connect();
transporter.sendMessage(message, message.getAllRecipients());
以下附加方法:
private Multipart buildMessageBody(String body, String bodyText) throws MessagingException {
if(attachments == null || attachments.isEmpty()){
return getAlternativeBodyMimeMultipart(body, bodyText);
}
MimeMultipart multipartRoot = new MimeMultipart("mixed");
BodyPart contentBodyPart = buildContentBodyPart(body, bodyText);
multipartRoot.addBodyPart(contentBodyPart);
List<BodyPart> attachmentParts = buildAttachmentParts();
for(BodyPart singleAttachmentPart : attachmentParts){
multipartRoot.addBodyPart(singleAttachmentPart);
}
return multipartRoot;
}
private List<BodyPart> buildAttachmentParts() {
List<BodyPart> attachmentsParts = new ArrayList<BodyPart>();
for (int i = 0; i < attachments.size(); i++) {
BinaryAttachment attach = attachments.get(i);
MimeBodyPart mbp = new MimeBodyPart();
System.setProperty("mail.mime.encodefilename", "true");
try {
mbp.setDataHandler(new DataHandler(attach));
mbp.setFileName(MimeUtility.encodeText(attach.getName()));
attachmentsParts.add(mbp);
} catch (Exception e) {
logger.error("buildBodyWithAttachment",e);
}
}
return attachmentsParts;
}
private BodyPart buildContentBodyPart(String body, String bodyText) throws MessagingException {
MimeMultipart alternativePart = getAlternativeBodyMimeMultipart(body, bodyText);
BodyPart content = new MimeBodyPart();
content.setContent(alternativePart);
return content;
}
例如,当我调用 messages.setFrom(from)
时,“from”变量中的发件人具有以下值:
“M.Test ADMINISTRATEURÈÁÍ admin@demo.onmicrosoft.com”
但是当我在我的邮箱中收到我的邮件时,发送的名称如下...... M. 测试 ADMINISTRATEURÃÃÃ
来自:“M. 测试管理员???” admin@demo.onmicrosoft.com
我认为问题出在“from”的编码上,它是由以下人员创建的:
来自 = new InternetAddress(sender)
,发件人是“M.Test ADMINISTRATEURÈÁÍ admin@demo.onmicrosoft.com”。
我该如何解决这个问题?
最佳答案
您必须在 InternetAddress 构造函数中指定字符集 - UTF-8。
from = new InternetAddress(email, sender, "UTF-8")
上述代码的 JavaMail 构造函数如下所示。
/**
* Construct an InternetAddress given the address and personal name.
* The address is assumed to be a syntactically valid RFC822 address.
*
* @param address the address in RFC822 format
* @param personal the personal name
* @param charset the MIME charset for the name
* @exception UnsupportedEncodingException if the personal name
* can't be encoded in the given charset
*/
public InternetAddress(String address, String personal, String charset)
throws UnsupportedEncodingException {
this.address = address;
setPersonal(personal, charset);
}
关于Java Mail "From"发件人姓名错误字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47431691/