IntelliJ IDEA 2017版 spring-boot 2.0.5 邮件发送简单实例 (三)

一、搭建SpringBoot项目

       详见此文:https://www.cnblogs.com/liuyangfirst/p/8298588.html

注意:

       需要添加mail依赖的包,同时还添加了lombock,方便日志打印。如图所示

       

二、启动Application,测试项目搭建是否成功

    

三、配置properties文档

1 #########邮箱协议
2 spring.mail.host=smtp.163.com  ####还可以是smtp.126.com 等
3 ##########发送邮件的用户名
4 spring.mail.username= 你的邮箱
5 ########移动端客户授权码(需要开通POP3授权)
6 spring.mail.password= 授权密码
7 #######配置邮件发送默认编码utf-8
8 spring.mail.default-encoding=UTF-8
View Code

注意:
        需要授权码,163为例,163官网授权码开通如下

            https://help.mail.163.com/faqDetail.do?code=d7a5dc8471cd0c0e8b4b8f4f8e49998b374173cfe9171305fa1ce630d7f67ac2cda80145a1742516

四、业务逻辑
   

        1、需要发件人邮箱地址

        2、需要调用自带邮箱封装的类,JavaMailSender

        3、需要将收件人,主题,内容填入到方法内,最后,调用JavaMailSender的send方法发送

 1 package com.baidu.mailtest.service;
 2 
 3 import lombok.extern.slf4j.Slf4j;
 4 import org.springframework.beans.factory.annotation.Autowired;
 5 import org.springframework.beans.factory.annotation.Value;
 6 import org.springframework.mail.SimpleMailMessage;
 7 import org.springframework.mail.javamail.JavaMailSender;
 8 import org.springframework.stereotype.Service;
 9 
10 /******************************
11  * @author : liuyang
12  * <p>ProjectName:mail  </p>
13  * @ClassName :  MailService
14  * @date : 2018/9/22 0022
15  * @time : 22:57
16  * @createTime 2018-09-22 22:57
17  * @version : 2.0
18  * @description :
19  *
20  *
21  *
22  *******************************/
23 
24 @Service
25 @Slf4j
26 public class MailService {
27 
28 
29     /**
30      * 发件人邮箱地址
31      */
32     @Value("${spring.mail.username}")
33     private String fromUserName;
34 
35 
36     @Autowired
37     private JavaMailSender javaMailSender;
38 
39 
40     /**
41      * 一般发送邮件方法
42      *
43      * @param to      发送给某人
44      * @param subject 邮件主题
45      * @param context 邮件内容
46      */
47     public void sendSimpleMail(String to, String subject, String context) {
48 
49         SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
50         simpleMailMessage.setTo(to);
51         simpleMailMessage.setSubject(subject);
52         simpleMailMessage.setText(context);
53         simpleMailMessage.setFrom(fromUserName);
54 
55         javaMailSender.send(simpleMailMessage);
56     }
57 
58 }
View Code

五、编写测试类

 1 package com.baidu.mailtest;
 2 
 3 import com.baidu.mailtest.service.MailService;
 4 import lombok.extern.slf4j.Slf4j;
 5 import org.junit.Test;
 6 import org.junit.runner.RunWith;
 7 import org.springframework.boot.test.context.SpringBootTest;
 8 import org.springframework.test.context.junit4.SpringRunner;
 9 
10 import javax.annotation.Resource;
11 
12 @RunWith(SpringRunner.class)
13 @SpringBootTest
14 @Slf4j
15 public class MailServiceApplicationTests {
16 
17     @Resource
18     MailService mailService;
19 
20     @Test
21     public void sayMail() {
22 
23         mailService.sendSimpleMail("收件人邮箱", "SpringBoot邮件测试", "今天需要很多美女陪我");
24 
25         log.info("发送成功");
26     }
27 
28 }
View Code

六、观察测试结果

注意发送时候,测试类比较慢,如图位置会卡一会儿。

        发送成功如图:

 七、源码分享

 [email protected]:liushaoye/mailtest.git

      

猜你喜欢

转载自www.cnblogs.com/liuyangfirst/p/9691713.html