Javamail 发送附件中文名过长以及乱码问题

最近在弄javamail发送邮件当邮件里含有附件并且附件是中文的时候发送后就会出现乱码!!

通过javax.mail.internet.MimeUtility.encodeText()就可解决这个附件乱码的问题

直接和发送邮件的代码一起奉上:


package Demo;


import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;


import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;


public class SendEmail {


/**
* @param args
*/
public  static final String  user = "";//发送邮件邮箱
public  static final String  pwd  = "";//发送邮件邮箱密码
public  static final String  smtp = "smtp.163.com";//简单邮件传输协议

public static void main(String[] args) {
List recipients = new ArrayList();//收件人列表
String recipient = "";//收件人邮箱
recipients.add(recipient);
List FileList = new ArrayList();//附件列表
File file = new File("D:\\附件.xls");
FileList.add(file);
String title = "邮件主题";
String username = "发送邮件人";
String text = "邮件文本";
try{
Properties props = System.getProperties();
props.put("mail.smtp.host", smtp);//这里默认使用163邮箱
Session session = null;


if (user != null && pwd != null) {
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.user", user);
props.put("mail.smtp.password", pwd);


} else {
props.put("mail.smtp.auth", "false");
}
session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, pwd);
}
});

MimeMessage message = new MimeMessage(session);
if (false) {//是否需要收条
message.addHeader("Disposition-Notification-To","接收收条地址");
}
message.setSubject(title);//
message.setFrom(new InternetAddress(user, username));


       int receiverCount = recipients.size();
       if (receiverCount > 0) {
        InternetAddress[] address = new InternetAddress[receiverCount];
        for (int i = 0; i < receiverCount; i++) {
         address[i] = new InternetAddress((String) recipients.get(i));
        }          
        message.addRecipients(Message.RecipientType.TO, address);
       }
       
          
MimeMultipart multi = new MimeMultipart();
BodyPart textBodyPart = new MimeBodyPart();

textBodyPart.setText(text);

multi.addBodyPart(textBodyPart);
if (FileList != null) {
for (int i = 0; i < FileList.size(); i++) {
File f = (File) FileList.get(i);
FileDataSource fds = new FileDataSource(f);
BodyPart fileBodyPart = new MimeBodyPart();
fileBodyPart.setDataHandler(new DataHandler(fds));
fileBodyPart.setFileName(MimeUtility.encodeText(f.getName()));//如果附件有中文通过转换没有问题了
multi.addBodyPart(fileBodyPart);
}
}
message.setContent(multi);
message.setSentDate(new Date());


Transport.send(message);
System.out.println("发送成功!");
}catch(Exception e){
e.printStackTrace();
}

}


}


补充:测试的时候发现有的时候中文名过长MimeUtility.encodeText方法会自动给添加下划线通过查找得知:对文件进行编码时超出长度会自动通过"/r","/n"替换而MimeUtility.encodeText可能通过"_"进行连接只要替换了"/r","/n"即可所以那段代码应该这样写:


String filename = MimeUtility.encodeText(f.getName());
filename = filename.replaceAll("\r", "").replaceAll("\n", "");
fileBodyPart.setFileName(filename);

你可能感兴趣的:(Javamail 发送附件中文名过长以及乱码问题)