今天使用Spring的mail API发送邮件。
下面说一下流程。
Maven依赖
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
<!--Spring mail 类库-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>5.0.4.RELEASE</version>
</dependency>
创建bean
启用邮箱smtp功能
可以看到smtp的服务器地址是:smtp.sina.com。即为下面的host值。
填入该邮箱的username和password,邮件将会从该邮箱发出。
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="smtp.sina.com"></property>
<property name="username" value="xxxx@sina.com"></property>
<property name="password" value="xxxxx"></property>
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.timeout">20000</prop>
</props>
</property>
</bean>
创建自定义发送的bean,
<bean id="userMailSender" class="org.lanmei.cms.email.UserMailSender">
<property name="mailSender" ref="mailSender" />
</bean>
完整的配置文件:applicationContext-email.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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-4.3.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="smtp.sina.com"></property>
<property name="username" value="lanmeishop1@sina.com"></property>
<property name="password" value="lanmei"></property>
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.timeout">20000</prop>
</props>
</property>
</bean>
<bean id="userMailSender" class="org.lanmei.cms.email.UserMailSender">
<property name="mailSender" ref="mailSender" />
</bean>
</beans>
编写UserMailSender类
mailSender已经在bean定义中进行依赖注入。
package org.lanmei.cms.email;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
public class UserMailSender {
private JavaMailSenderImpl mailSender;
public void send(SimpleMailMessage mail) {
mailSender.send(mail);
}
public JavaMailSenderImpl getMailSender() {
return mailSender;
}
public void setMailSender(JavaMailSenderImpl mailSender) {
this.mailSender = mailSender;
}
}
编写测试类
package org.lanmei.cms;
import org.junit.Test;
import org.lanmei.cms.email.UserMailSender;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.mail.SimpleMailMessage;
public class UserMailSenderTest {
@Test
public void emailTest() {
ApplicationContext ac = new ClassPathXmlApplicationContext("spring/applicationContext-email.xml");
UserMailSender sender = (UserMailSender) ac.getBean("userMailSender");
SimpleMailMessage mail = new SimpleMailMessage();
mail.setTo("xxxxx@qq.com");//收件人邮箱地址
mail.setFrom("xxxxx@sina.com");//发件人,需与mailSender bean中的username一致
mail.setSubject("spring自带javamail发送的邮件");//主题
mail.setText("hello this mail is from spring javaMail ");//正文
sender.send(mail);
}
}