Java Mail“From”发件人名称坏字符ÃÃÃÃ

时间:2017-11-22 10:01:41

标签: java email encoding sender

我尝试发送包含以下代码段的邮件:

    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());

Bellow遵循其他方法:

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。测试管理员”

但是当我在邮箱中收到邮件时,发送邮件的名称如下...... M.测试ADMINISTRATEURÃÃÃ

来自:“M. Test ADMINISTRATEUR ???”

我认为问题来自“from”的编码,它由以下内容创建: from = new InternetAddress(sender); 和发件人是“M.TestINIVATEURÈÁÍ”

你知道一个可以解决这个问题的解决方案吗?

致以最诚挚的问候和非常感谢

阿德里安

1 个答案:

答案 0 :(得分:1)

您必须在InternetAddress构造函数中指定charset - 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);
    }