最近开发遇到Javamail 发送邮件标题乱码问题,腾讯、网易邮箱不会乱码,阿里邮箱 标题则会乱码。解决办法:
String subject = MimeUtility.encodeWord(mailEntity.getMailSubject(), "UTF-8", "Q");
message.setSubject(subject);
发送邮件代码:
/**
* 发送邮件 可以带附件 内容可为HTML
*
* @param mailEntity
* @return
* @throws GeneralSecurityException
*/
public static boolean sendMail(MailEntity mailEntity)
throws GeneralSecurityException { // 发件人电子邮箱
final String from = mailEntity.getMailFromAddress();
// 发件人电子邮箱密码
final String pass = mailEntity.getMailFromPwd(); // 指定发送邮件的主机为 smtp.qq.com
String host = mailEntity.getMailFromHost(); // 邮件服务器 String port = mailEntity.getPort(); // 获取系统属性
Properties properties = System.getProperties(); // 设置邮件服务器
properties.setProperty("mail.smtp.host", host);
properties.setProperty("mail.smtp.port",port); properties.put("mail.smtp.auth", "true");
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
properties.put("mail.smtp.ssl.enable", "false");
properties.put("mail.smtp.ssl.socketFactory", sf);
// 获取默认session对象
Session session = Session.getDefaultInstance(properties, new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() { // qq邮箱服务器账户、第三方登录授权码
return new PasswordAuthentication(from, pass); // 发件人邮件用户名、密码
}
}); try {
// 创建默认的 MimeMessage 对象
MimeMessage message = new MimeMessage(session); // Set From: 头部头字段
message.setFrom(new InternetAddress(from)); // Set To: 头部头字段
message.addRecipient(Message.RecipientType.TO, new InternetAddress(mailEntity.getMailReceiverAddress())); // Set Subject: 主题文字
String subject = MimeUtility.encodeWord(mailEntity.getMailSubject(), "UTF-8", "Q");
message.setSubject(subject); // 创建消息部分
BodyPart messageBodyPart = new MimeBodyPart(); // 消息
// messageBodyPart.setText(mailEntity.getMailContent());
messageBodyPart.setContent(mailEntity.getMailContent(), "text/html;charset=utf-8"); // 创建多重消息
Multipart multipart = new MimeMultipart(); // 设置文本消息部分
multipart.addBodyPart(messageBodyPart); if (mailEntity.getFilePaths() != null && mailEntity.getFilePaths().size() > 0) {
for (String filePath : mailEntity.getFilePaths()) {
// 附件部分
messageBodyPart = new MimeBodyPart();
String suffix = "";
String oldPath = "";
if (filePath.indexOf(".") >0){
suffix = filePath.substring(filePath.indexOf(".")+1);
oldPath = filePath.substring(0,filePath.indexOf(".")); DataSource source = new FileDataSource(oldPath);
File file = ((FileDataSource) source).getFile();
file.renameTo(new File(oldPath + "." + suffix));
source = new FileDataSource(filePath);
messageBodyPart.setDataHandler(new DataHandler(source)); }else {
// 设置要发送附件的文件路径
DataSource source = new FileDataSource(filePath);
messageBodyPart.setDataHandler(new DataHandler(source));
} // 设置要发送附件的文件路径 // messageBodyPart.setFileName(filename);
// 处理附件名称中文(附带文件路径)乱码问题
messageBodyPart.setFileName(MimeUtility.encodeText(filePath));
multipart.addBodyPart(messageBodyPart);
}
} // 发送完整消息
message.setContent(multipart); // 发送消息
Transport.send(message);
logger.info("Sent mail successfully....");
return true;
} catch (MessagingException e) {
logger.debug(e.getMessage());
} catch (UnsupportedEncodingException e) {
logger.debug(e.getMessage());
}
return false;
}
实体:
/**
* @Auther: mxf
* @Date: 2018/10/31 16:08
* @Description:发送邮件实体类
*/
public class MailEntity implements Serializable {
/**
* 邮件接收地址
*/
private String mailReceiverAddress; /**
* 邮件主题
*/
private String mailSubject; /**
* 邮件内容
*/
private String mailContent; /**
* 附件路径
*/
private List<String> filePaths = new ArrayList<String>(); /**
* 发送地址
*/
private String mailFromAddress; /**
* 发送者的邮件密码
*/
private String mailFromPwd; /**
* 指定发送邮件的主机
*/
private String mailFromHost; /**
* 发送邮件主机端口
*/
private String port; public String getPort() {
return port;
} public void setPort(String port) {
this.port = port;
} public String getMailReceiverAddress() {
return mailReceiverAddress;
} public void setMailReceiverAddress(String mailReceiverAddress) {
this.mailReceiverAddress = mailReceiverAddress;
} public String getMailSubject() {
return mailSubject;
} public void setMailSubject(String mailSubject) {
this.mailSubject = mailSubject;
} public String getMailContent() {
return mailContent;
} public void setMailContent(String mailContent) {
this.mailContent = mailContent;
} public List<String> getFilePaths() {
return filePaths;
} public void setFilePaths(List<String> filePaths) {
this.filePaths = filePaths;
} public String getMailFromAddress() {
return mailFromAddress;
} public void setMailFromAddress(String mailFromAddress) {
this.mailFromAddress = mailFromAddress;
} public String getMailFromPwd() {
return mailFromPwd;
} public void setMailFromPwd(String mailFromPwd) {
this.mailFromPwd = mailFromPwd;
} public String getMailFromHost() {
return mailFromHost;
} public void setMailFromHost(String mailFromHost) {
this.mailFromHost = mailFromHost;
}
}