1.配置spring-mail.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- spring mail begin ############################################## -->
<bean id="configurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>/mail.properties</value>
</list>
</property>
</bean>
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
<bean id="MailSenderDemo" class="com.zlg.test.springmail.MailSenderDemo"></bean>
<!-- spring mail demo end ############################################## -->
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="${mail.host}"></property>
<property name="port" value="${mail.port}"></property>
<property name="username" value="${mail.username}"></property>
<property name="password" value="${mail.password}"></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>
<!-- 如果是网易邮箱, mail.smtp.starttls.enable 设置为 false-->
<prop key="mail.smtp.starttls.enable">true</prop>
</props>
</property>
</bean>
</beans>
2 mail.properties
# qq mail server
#mail.protocol=smtp
#mail.port=465
#mail.host=smtp.exmail.qq.com
#mail.username=xxx@qq.com
#mail.password=
# 163 mail server
mail.protocol=smtp
mail.port=465
mail.host=smtp.163.com
mail.username=xxx@163.com
mail.password=
3.web.xml中加入bean配置
<servlet>
<servlet-name>provider</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
<!-- classpath:applicationContext.xml, -->
<!-- classpath:applicationContext-servlet.xml, -->
<!-- classpath:dubbo.xml, -->
classpath:spring-mail.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>provider</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
4。pom.xml中加入jar包依赖
<!-- spring mail需要的jar包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
5.编写Java接口文件
package com.zlg.test.springmail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
/**
* @Title: MailSenderDemo.java
* @Package: com.zlg.test.springmail
* @Description: spring mail 测试
* @author: zlg
* @date: 2017年6月20日 上午9:43:45
* @version: V1.0
*/
public class MailSenderDemo {
@Autowired
private JavaMailSender mailSender;
public void send(SimpleMailMessage mail) {
mailSender.send(mail);
}
public JavaMailSender getMailSender(){
return this.mailSender;
}
}
6.添加测试类
package com.zlg.test.springmail;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
/**
* @Title: SendTest.java
* @Package: com.zlg.test.springmail
* @Description: TODO
* @author: zlg
* @date: 2017年6月20日 上午10:03:36
* @version: V1.0
*/
public class SendTest {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext(
"spring-mail.xml");
MailSenderDemo sender = (MailSenderDemo) ac.getBean("MailSenderDemo");
/*SimpleMailMessage mail = new SimpleMailMessage();
mail.setTo("673376601@qq.com");// 收件人邮箱地址
mail.setFrom("zlg");// 收件人
mail.setSubject("SpringMailTest");// 主题
mail.setText("测试Spring自带邮件发送功能");// 正文
sender.send(mail);*/
JavaMailSender javaMailSender = sender.getMailSender();
MimeMessage mime = javaMailSender.createMimeMessage();
MimeMessageHelper helper;
try {
helper = new MimeMessageHelper(mime, true, "utf-8");
helper.setTo("dszlg@163.com");// 收件人邮箱地址
helper.setFrom("dszlg@163.com");// 收件人
helper.setSubject("SpringMailTest");// 主题
helper.setText("测试Spring自带邮件发送功能");// 正文
} catch (MessagingException me) {
me.printStackTrace();
}
javaMailSender.send(mime);
}
}