spring自带的MailSender类在spring-context-support-xxx.RELEASE.jar文件中,这里以4.3.4版本为例。
1、新建gradle项目,引入依赖配置。
compile group:"org.springframework",name:"spring-context-support",version:"4.3.4.RELEASE" compile group:"javax.mail",name:"mail",version:"1.4.7"
2、编写发送邮件服务类。
package com.xx.sendmail; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; public class MailSenderService { @Autowired private JavaMailSender mailSender; public void send(SimpleMailMessage message){ mailSender.send(message); System.out.println("send message successfully."); } }
3、新建配置文件applicationContext.xml与mail.properties。
<?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-4.3.xsd"> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <value>mail.properties</value> </property> </bean> <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/> <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"> <property name="host" value="${mail.host}"></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.starttls.enable">true</prop> </props> </property> <property name="username" value="${mail.username}"></property> <property name="password" value="${mail.password}"></property> <property name="port" value="${mail.port}"></property> </bean> <bean id="mailSenderService" class="com.xx.sendmail.MailSenderService"></bean> </beans>
mail.properties,我这里使用的是126邮箱作为测试邮箱,因此,密码这里需要使用客户端授权码。
mail.host=smtp.126.com mail.username=y******9@126.com mail.password=y******2//这里是客户端授权码,不是邮箱登录密码 mail.port=465
4、编写测试邮件发送程序。
package com.xx.sendmail; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.mail.SimpleMailMessage; public class Application { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext( new String[]{"applicationContext.xml"}); MailSenderService senderService = (MailSenderService)context.getBean("mailSenderService"); SimpleMailMessage mail = new SimpleMailMessage(); mail.setTo("******5@sina.com"); mail.setFrom("y******9@126.com"); mail.setSubject("spring sendmail test"); mail.setText("Hello,***,this is my test,do not reply."); senderService.send(mail); } }
运行,如果配置均正确,就会看到打印send message successfully.然后去收件箱检查,发现邮件一封。
这里如果设置邮箱服务器的密码不是客户端授权码,那么可能会遇到以下错误。
Exception in thread "main" org.springframework.mail.MailAuthenticationException: Authentication failed; nested exception is javax.mail.AuthenticationFailedException: 535 Error: authentication failed
这个问题在126邮箱设置中有明确说明: