添加email的依赖项:
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.2.6.RELEASE</version>
</dependency>
新建邮件配置spring-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"
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">
<!-- spring mail begin ############################################## -->
<!--<bean id="configurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">-->
<!--<property name="locations">-->
<!--<list>-->
<!--<value>classpath:mail.properties</value>-->
<!--</list>-->
<!--</property>-->
<!--</bean>-->
<!--在location备注properties的路径,同时存在多个properties,每个properties添加ignore-unresolvable="true"-->
<context:property-placeholder location="classpath:mail.properties" ignore-unresolvable="true"/><bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
<!-- spring mail demo end ############################################## -->
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="${mail.host}"/>
<property name="port" value="${mail.port}"/>
<property name="username" value="${mail.username}"/>
<property name="password" value="${mail.password}"/>
<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>
新建邮件property:
# qq mail server
mail.protocol=smtp
mail.port=465
mail.host=smtp.qq.com
mail.username=*******@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=
新建demo:
package com.zju.visualization.com.zju.mail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
public class MailSenderDemo {
@Autowired
private JavaMailSender mailSender;
public void send(SimpleMailMessage mail) {
mailSender.send(mail);
}
public JavaMailSender getMailSender(){
return this.mailSender;
}
}
发送邮件;
@RequestMapping(value = "/mail") public void sendEmail(){ ApplicationContext ac = new ClassPathXmlApplicationContext( "spring//spring-email.xml"); MailSender sender = (MailSender) ac.getBean("MailSenderDemo"); JavaMailSender javaMailSender = sender.getMailSender(); MimeMessage mime = javaMailSender.createMimeMessage(); MimeMessageHelper helper; try { helper = new MimeMessageHelper(mime, true, "utf-8"); helper.setTo("1104253998@qq.com");// 收件人邮箱地址 helper.setFrom("1104253998@qq.com");// 收件人 helper.setSubject("Spring发送邮件");// 主题 helper.setText("杨老师您好啊");// 正文 } catch (MessagingException me) { me.printStackTrace(); } javaMailSender.send(mime); } }