spring 发邮件,解决韩语乱码问题
1、配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<!-- 注意:这里的参数(如用户名、密码)都是针对邮件发送者的 -->
<bean id="mailSender"
class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host">
<value>smtp.gmail.com</value>
</property>
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.timeout">25000</prop>
<prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>
<prop key="mail.smtp.socketFactory.fallback">false</prop>
<prop key="mail.smtp.starttls.enable">ture</prop>
<prop key="mail.smtp.port">465</prop>
</props>
</property>
<property name="username">
<value>XXX@raker.kr</value>
</property>
<property name="password">
<value>XXX</value>
</property>
</bean>
</beans>
2、测试类
package test.util;
import javax.mail.internet.MimeMessage;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import test.base.BaseDaoTestCase;
public class TestEmail extends BaseDaoTestCase {
JavaMailSender mailSender;
public void setMailSender(JavaMailSender mailSender) {
this.mailSender = mailSender;
}
public void testSend() {
//获取JavaMailSender bean
SimpleMailMessage mail = new SimpleMailMessage(); //<span style="color: #ff0000;">注意SimpleMailMessage只能用来发送text格式的邮件</span>
MimeMessage mime = mailSender.createMimeMessage();
MimeMessageHelper helper;
try {
helper = new MimeMessageHelper(mime, true, "utf-8");
helper.setFrom("jakin@xxx.com");
helper.setTo("zhangzenglun@raker.com.cn");
helper.setCc("zhangzenglun@raker.com.cn");
helper.setSubject("챔피언쉽");
helper.setText("안녕하세요spri레이커 포인트가 부족합니다.충전하거나 다른 신청방식을 선택해 주세요.ngMail的简单发送测试dd");
mailSender.send(mime);
/* mail.setTo("zhangzenglun@raker.com.cn");//接受者
mail.setFrom("xxxx@163.com");//发送者,这里还可以另起Email别名,不用和xml里的username一致
mail.setSubject("spring mail cse!");//主题
mail.setText("안녕하세요springMail的简单发送测试dd-こんにちは-内容 !챔피언쉽");//邮件内容
mailSender.send(mail); */
} catch (Exception e) {
e.printStackTrace();
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<!-- 注意:这里的参数(如用户名、密码)都是针对邮件发送者的 -->
<bean id="mailSender"
class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host">
<value>smtp.gmail.com</value>
</property>
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.timeout">25000</prop>
<prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>
<prop key="mail.smtp.socketFactory.fallback">false</prop>
<prop key="mail.smtp.starttls.enable">ture</prop>
<prop key="mail.smtp.port">465</prop>
</props>
</property>
<property name="username">
<value>XXX@raker.kr</value>
</property>
<property name="password">
<value>XXX</value>
</property>
</bean>
</beans>
2、测试类
package test.util;
import javax.mail.internet.MimeMessage;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import test.base.BaseDaoTestCase;
public class TestEmail extends BaseDaoTestCase {
JavaMailSender mailSender;
public void setMailSender(JavaMailSender mailSender) {
this.mailSender = mailSender;
}
public void testSend() {
//获取JavaMailSender bean
SimpleMailMessage mail = new SimpleMailMessage(); //<span style="color: #ff0000;">注意SimpleMailMessage只能用来发送text格式的邮件</span>
MimeMessage mime = mailSender.createMimeMessage();
MimeMessageHelper helper;
try {
helper = new MimeMessageHelper(mime, true, "utf-8");
helper.setFrom("jakin@xxx.com");
helper.setTo("zhangzenglun@raker.com.cn");
helper.setCc("zhangzenglun@raker.com.cn");
helper.setSubject("챔피언쉽");
helper.setText("안녕하세요spri레이커 포인트가 부족합니다.충전하거나 다른 신청방식을 선택해 주세요.ngMail的简单发送测试dd");
mailSender.send(mime);
/* mail.setTo("zhangzenglun@raker.com.cn");//接受者
mail.setFrom("xxxx@163.com");//发送者,这里还可以另起Email别名,不用和xml里的username一致
mail.setSubject("spring mail cse!");//主题
mail.setText("안녕하세요springMail的简单发送测试dd-こんにちは-内容 !챔피언쉽");//邮件内容
mailSender.send(mail); */
} catch (Exception e) {
e.printStackTrace();
}
}
}