1.需要mail-1.4.1.jar包,配置一个属性文件email.properties
#邮箱服务器
email.host=smtp.163.com
#邮箱账号
email.username=z19160343743@163.com
#邮箱SMTP的授权码,不是邮箱密码
email.password=xxxxxxxxxxxx
2.编写applicationContext-email.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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd">
<!--该标签的作用是向spring容器中注册4个BeanPostProcessor,这样就可以直接使用@Autowired、@Resource 、@PostConstruct、@PreDestroy-->
<context:annotation-config/>
<!-- 引入属性文件 -->
<context:property-placeholder location="classpath:email.properties" />
<bean id ="mailSender" class ="org.springframework.mail.javamail.JavaMailSenderImpl" >
<!--服务器-->
<property name="host" value="${email.host}" />
<!--邮箱账号-->
<property name="username" value="${email.username}" />
<!--授权码-->
<property name="password" value="${email.password}" />
<!--设置默认编码格式-->
<property name="defaultEncoding" value="UTF-8"></property>
<!--SMTP服务器验证-->
<property name="javaMailProperties">
<props>
<!--开启权限验证-->
<prop key="mail.smtp.auth">true</prop>
<!--设置链接超时时间-->
<prop key="mail.smtp.timeout">25000</prop>
<!--设置服务器端口号-->
<prop key="mail.smtp.port">25</prop>
</props>
</property>
</bean >
<bean id="simpleMailMessage" class="org.springframework.mail.SimpleMailMessage">
<!--发件人的邮箱账号-->
<property name="from" value="${email.username}" />
</bean>
<!--将属性注入到工具类中-->
<bean id="mailUtil" class="com.zengjx.email.utils.EmailUtil">
<property name="mailSender" ref="mailSender"></property>
<property name="simpleMailMessage" ref="simpleMailMessage"></property>
</bean>
</beans>
3.在主配置文件applicationContext.xml中导入applicationContext-email.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" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--导入applicationContext-email.xml文件-->
<import resource="applicationContext-email.xml"/>
<!--设置扫描-->
<context:component-scan base-package="com.zengjx.email" />
</beans>
4.在web.xml中开启对applicationContext.xml文件的监听
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>
<!--监听applicationContext.xml文件-->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<!--设置拦截-->
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--设置首页-->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
5.编写一个工具类,用来发送邮件
package com.zengjx.email.utils;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
/**
* @author: FastRabbit
* @date: 2021/4/11 17:08
* @description:
*/
public class EmailUtil {
//邮箱发送者
private MailSender mailSender;
//邮件
private SimpleMailMessage simpleMailMessage;
//将xml配置MailSender的bean注入到该字段中
public void setMailSender(MailSender mailSender) {
this.mailSender = mailSender;
}
//将xml配置SimpleMailMessage的bean注入到该字段中
public void setSimpleMailMessage(SimpleMailMessage simpleMailMessage) {
this.simpleMailMessage = simpleMailMessage;
}
/**
* @param recipient 收件人
* @param subject 主题
* @param content 内容
*/
public void send(String recipient,String subject,String content){
simpleMailMessage.setTo(recipient);
simpleMailMessage.setSubject(subject);
simpleMailMessage.setText(content);
mailSender.send(simpleMailMessage);
}
}
6.测试一下
package com.zengjx.email.test;
import com.zengjx.email.utils.EmailUtil;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author: FastRabbit
* @date: 2021/4/11 17:55
* @description:
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class TestSend {
@Autowired
EmailUtil emailUtil;
@Test
public void testSendEmail(){
emailUtil.send("1095962937@qq.com","洞拐洞拐,收到请回答","请求活力支援");
}
}