Spring Boot 整合邮件发送

 

1. 选择依赖:

引入邮件启动器: spring-boot-starter-mail ,上图选择Mail依赖即可。

 

2. Spring Boot 提供了自动配置类 MailSenderAutoConfiguration(有需要的可以自行查看)

 

3. 在 application.properties 中配置邮箱信息, 参考 MailProperties

 	spring.mail.username=13160081226@163.com  
 	spring.mail.password=PHYP*******SA  
 	spring.mail.host=smtp.163.com  
 	spring.mail.port=465  
 	  
 	# 此处以下用于解决项目发布到服务器启动 发送邮件出现错误(下方文档说明)
 	#SSL证书Socket工厂  
 	spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory  
 	#使用SMTPS协议465端口  
 	spring.mail.properties.mail.smtp.socketFactory.port=465  
 	spring.mail.properties.mail.smtp.auth=true  
 	spring.mail.properties.mail.smtp.starttls.enable=true  
 	spring.mail.properties.mail.smtp.starttls.required=tru  

不加上述SSL证书以下的代码可能在云服务器上会报错:org.springframework.mail.MailSendException: Mail server connection failed; nested exception is com.sun.mail.util.MailConnectException: Couldn’t connect to host, port: smtp.qq.com, 25/465; timeout -1;

 

4、密码不写明文在配置中,在163邮箱中进行获取制授权码,如下操作

切记授权密码只会显示一次,按照上图设置邮箱,QQ邮箱或其他邮箱同理。

 

 

5. Spring Boot 自动装配 JavaMailSenderImpl 进行发送邮件

 	@SpringBootTest  
 	class SpringbootMailApplicationTests {  
 	  
 	    @Autowired  
 	    JavaMailSenderImpl javaMailSender;  
 	  
 	    /** 
 	     * 封装简单的邮件内容 
 	     */  
 	    @Test  
 	    void contextLoads() {  
 	        SimpleMailMessage message = new SimpleMailMessage();  
 	        //邮件主题  
 	        message.setSubject("放假通知");  
 	        message.setText("春节放假7天");  
 	        //发件人  
 	        message.setFrom("13160081226@163.com");  
 	        message.setTo("13160081226@163.com");  
 	        javaMailSender.send(message);  
 	    }  
 	  
 	    /** 
 	     * 发送复杂邮件带附件和html的邮件 
 	     * @throws MessagingException 
 	     */  
 	    @Test  
 	    public void testMimeMail() throws MessagingException {  
 	        //创建一个发送复杂消息对象  
 	        MimeMessage mimeMessage = javaMailSender.createMimeMessage();  
 	        //通过消息帮助对象,来设置发送的内容  
 	        MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true);  
 	        //邮件主题  
 	        messageHelper.setSubject("放假通知");  
 	        //第2个参数为true表示是html  
 	        messageHelper.setText("<h2 style='color:red'>春节放假7天</h2>", true);  
 	  
 	        //上传文件 (文件名,File或IO对象)  
 	        messageHelper.addAttachment("01.png", new File("D:\\01.png"));  
 	        messageHelper.addAttachment("02.png", new File("D:\\02.png"));  
 	        //发件人  
 	        messageHelper.setFrom("13160081226@163.com");  
 	        messageHelper.setTo("13160081226@163.com");  
 	        javaMailSender.send(mimeMessage);  
 	    }  
 	}  

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Ark方舟

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

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

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

打赏作者

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

抵扣说明:

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

余额充值