JavaMail邮件附件中文乱码问题

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

JavaMail附件中文名称乱码

问题: 用Javamail发邮件到邮件服务器,从邮箱中查看发现附件的中文名称变成了密码原因:不明

解决:在设置邮件附件的时候调用javax.mail.internet.MimeUtility来编码, 例如

MimeMessagemsg = new MimeMessage(session);

msg.setFrom(new InternetAddress(from));

InternetAddress[] address = { new InternetAddress(to) };

msg.setRecipients(Message.RecipientType.TO, address);

msg.setSubject(subject);

// create and fill the first message part

MimeBodyPart mbp1 = new MimeBodyPart();

mbp1.setText(msgText1);

// create the second message part

MimeBodyPart mbp2 = new MimeBodyPart();

// attach the file to the message

mbp2.attachFile(filePath);

mbp2.setFileName(MimeUtility.encodeWord(fileName));

// create the Multipart and add its parts to it

Multipart mp = new MimeMultipart();

mp.addBodyPart(mbp1);

mp.addBodyPart(mbp2);

// add the Multipart to the message

msg.setContent(mp);

// set the Date: header

msg.setSentDate(new Date());

/*

* If you want to control the Content-Transfer-Encoding of the

* attached file, do the following. Normally you should never need

* to do this.

*

* msg.saveChanges(); mbp2.setHeader("Content-Transfer-Encoding",

* "base64");

*/

// send the message Transport.send(msg);

相关文档
最新文档