Spring Mail中文乱码问题
最近项目中用到了spring的邮件功能,程序部署在客户服务器上(操作系统为繁体中文),每次发送邮件时接收者的操作系统如果也为繁体中文则不会出现乱码,如果为简体中文或英文,对于邮件标题或内容有中文(无论繁体与简体)的就会出现中文乱码现象,需要"查看"-"编码"-"Unicode(UTF-8)"才能正常看到邮件的内容。究其原因是由于邮件的MIME没有设定为UTF-8。最后换了一种实现方法解决问题。类MailSendTest中doSendMailOne()为会出现问题的方法,doSendMailTwo()为不会出现乱码现象的实现方法。
Spring关于mailSender的配制:
<bean id="mailSender"
class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host">
<value>mail.xxx.com</value>
</property>
</bean>
class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host">
<value>mail.xxx.com</value>
</property>
</bean>
Java类MailSendTest:
package com.jakin;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
public class MailSendTest {
private MailSender mailSender;
public MailSender getMailSender() {
return mailSender;
}
public void setMailSender(MailSender mailSender) {
this.mailSender = mailSender;
}
/**
* 没有对邮件的格式编码进行处理,可能会有乱码现象
*
*/
public void doSendMailOne() {
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom("jakin@xxx.com");
message.setTo("lilei@xxx.com");
message.setCc("hanmeimei@xxx.com");
message.setSubject("第一封測試郵件");
message.setText("Dear All:\n" + "這是一封測試郵件!");
mailSender.send(message);
}
/**
* 把邮件的格式编码设定为UTF-8,不会出现乱码现象
*
*/
public void doSendMailTwo() {
JavaMailSender javaMailSender = (JavaMailSender) mailSender;
MimeMessage mime = javaMailSender.createMimeMessage();
MimeMessageHelper helper;
try {
helper = new MimeMessageHelper(mime, true, "utf-8");
helper.setFrom("jakin@xxx.com");
helper.setTo("lilei@xxx.com");
helper.setCc("hanmeimei@xxx.com");
helper.setSubject("第二封測試郵件");
helper.setText("Dear All:\n" + "這是一封測試郵件!");
} catch (MessagingException me) {
me.printStackTrace();
}
javaMailSender.send(mime);
}
}
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
public class MailSendTest {
private MailSender mailSender;
public MailSender getMailSender() {
return mailSender;
}
public void setMailSender(MailSender mailSender) {
this.mailSender = mailSender;
}
/**
* 没有对邮件的格式编码进行处理,可能会有乱码现象
*
*/
public void doSendMailOne() {
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom("jakin@xxx.com");
message.setTo("lilei@xxx.com");
message.setCc("hanmeimei@xxx.com");
message.setSubject("第一封測試郵件");
message.setText("Dear All:\n" + "這是一封測試郵件!");
mailSender.send(message);
}
/**
* 把邮件的格式编码设定为UTF-8,不会出现乱码现象
*
*/
public void doSendMailTwo() {
JavaMailSender javaMailSender = (JavaMailSender) mailSender;
MimeMessage mime = javaMailSender.createMimeMessage();
MimeMessageHelper helper;
try {
helper = new MimeMessageHelper(mime, true, "utf-8");
helper.setFrom("jakin@xxx.com");
helper.setTo("lilei@xxx.com");
helper.setCc("hanmeimei@xxx.com");
helper.setSubject("第二封測試郵件");
helper.setText("Dear All:\n" + "這是一封測試郵件!");
} catch (MessagingException me) {
me.printStackTrace();
}
javaMailSender.send(mime);
}
}