spring自带JavaMailSender发送邮件

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邮箱设置中有明确说明:


  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

luffy5459

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值