MimeBodyPart没有为亚洲语言设置UTF-8编码

时间:2016-07-10 16:01:28

标签: java utf-8 io javamail mime-message

当我收到邮件时,我收到了所有角色的问号。我很困惑我错了。有些附件正确显示,只有字符显示????就像问号一样。我已经验证正文已正确转换为所有亚洲语言但在发送邮件之前我再次验证了他们正在显示的消息??

  public void addAttachmentsforMail(String text, MimeMessage message, List<File> attachments, MimeSubtype mimeSubtype) throws MessagingException {

    MimeBodyPart mbpText = new MimeBodyPart();
    mbpText.setHeader("Content-Type", "text/plain;charset=utf-8");

//I have verified till here the body is getting converted to respective asian languages
    if(mimeSubtype.equals(MimeSubtype.HTML)) {
                   mbpText.setDataHandler(new DataHandler(new ByteArrayDataSource(body, "text/html")));        

            }

            Multipart mp = new MimeMultipart();
            mp.addBodyPart(mbpText);        

            MimeBodyPart mimeAttachment;
            for (File file : attachment) {

                mbpAttachment =  new MimeBodyPart();

                FileDataSource foo = new FileDataSource(file);
                mimeAttachment.setDataHandler(new DataHandler(foo));
                mimeAttachment.setHeader("Content-ID","<" + foo.getName() + ">");
                mimeAttachment.setFileName(foo.getName());

                mp.addBodyPart(mimeAttachment);


            }

   //But When I verify the message in log at here before sending the mail all the charcters were converted in to ???         

 message.setContent(mp);

transport.send(message)
        }

这是邮件的标题

Message-ID: <-1251496143.10677.1468164058574.JavaMail.star@gmail.com>
Subject: =?UTF-8?B?5biQ5oi35Y+K5a+G56CB5o+Q6YaS?=
MIME-Version: 1.0
Content-Type: multipart/mixed; 
    boundary="----=_Part_10649_-1456564573.1468164040753"

------=_Part_10649_-1456564573.1468164040753
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit

1 个答案:

答案 0 :(得分:0)

由于以下mbpText.setDataHandler,mbpText.setHeader调用无效。如果可能,请使用允许您指定字符集的setText方法。您可能还想将系统属性“mail.mime.charset”设置为“utf-8”。