我尝试发送带有以下代码段的邮件:

    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.测试ADMINISTRATEURÈÁÁ”

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

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

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

您知道一种可以解决此问题的解决方案吗?

致以诚挚的谢意

阿德里安

最佳答案

您将必须在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 - Java Mail“发件人”发件人名称错误字符ÃÃÃÃÃÃ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47431691/

10-13 08:25