3、SpringBoot------邮件发送(1)

开发工具:STS

代码下载链接:https://github.com/theIndoorTrain/Springboot/tree/8878e8e89ce01ceb967ef8c1193ac740a6f7dd40

前言:

每当你生日那天,腾讯官方都会给你致上一封精美的生日祝福邮件......

当你在某个网站注册账号时,往往需要去邮箱里激活验证......

我们今天就来探讨这个技术,邮件的发送。

我们往常发邮件的步骤为:

1.登录邮箱网站或者客户端

2.输入账号、密码

3.填写收件人

4.撰写邮件内容

5.发送

在我们的web项目中,我们要发送邮件给指定用户,步骤为:

1.绑定邮箱服务器

2.验证账号、密码(授权码)

3.建立邮件

4.填写接收者、邮件内容

5.发送

下面我们来实现邮件的发送。


一、简单邮件的发送

1.在pom.xml中添加mail依赖

1         <!--添加mail依赖  -->
2         <dependency>
3             <groupId>org.springframework.boot</groupId>
4             <artifactId>spring-boot-starter-mail</artifactId>
5         </dependency>

2.在application中配置mail

 1 #配置邮箱
 2 spring:
 3   mail:
 4     host: smtp.qq.com
 5     username: [email protected]
 6     password: 邮箱密码(qq邮箱填写授权码)
 7     default-encoding:  UTF-8
 8  
 9  #配置邮件发送人
10 mail: 
11   from: 
12     addr: [email protected]

3.定义邮件发送业务接口:

 1 package com.xm.service;
 2 
 3 
 4 /**
 5  * 邮件发送业务
 6  * @author xm
 7  *
 8  */
 9 public interface EmailService {
10     
11     /**
12      * 发送简单邮件
13      * @param to :收件人
14      *  @param subject : 标题
15      * @param content :邮件内容
16      */
17     void sendSimpleEmail(String to,String subject,String content);
18 
19 }

4.实现邮件发送业务:

 1 package com.xm.service.impl;
 2 
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.beans.factory.annotation.Value;
 5 import org.springframework.mail.SimpleMailMessage;
 6 import org.springframework.mail.javamail.JavaMailSender;
 7 import org.springframework.stereotype.Service;
 8 
 9 import com.xm.service.EmailService;
10 /**
11  * 邮件发送业务实现
12  * @author xm
13  *
14  */
15 @Service
16 public class EmailServiceImpl implements EmailService {
17     
18     //获取发送者信息
19     @Value("${mail.from.addr}")
20     private String from; 
21     
22     //定义邮件发送者
23     @Autowired
24     private JavaMailSender sender;
25 
26     @Override
27     public void sendSimpleEmail(String to, String subject,String content) {
28         //定义简单邮件
29         SimpleMailMessage message = new SimpleMailMessage();
30         //把发送者、收件人、标题、邮件内容封装入简单邮件中
31         System.out.println("from: "+from+",to:"+to+",subject:"+subject);
32         message.setFrom(from);
33         message.setTo(to);
34         message.setSubject(subject);
35         message.setText(content);
36         //交给邮件发送者进行转发
37         sender.send(message);
38         System.out.println("发送");
39     }
40 
41 }

5.定义邮件测试类:

 1 package com.xm;
 2 
 3 import org.junit.Test;
 4 import org.junit.runner.RunWith;
 5 import org.springframework.beans.factory.annotation.Autowired;
 6 import org.springframework.boot.test.context.SpringBootTest;
 7 import org.springframework.test.context.junit4.SpringRunner;
 8 
 9 import com.xm.service.EmailService;
10 
11 @RunWith(SpringRunner.class)
12 @SpringBootTest
13 public class EmailTest {
14     
15     @Autowired
16     private EmailService emailService;
17     
18     @Test
19     /**
20      * 测试简单邮件的发送
21      */
22     public void sendSimpleMassage() {
23         emailService.sendSimpleEmail("[email protected]", "122", "Hello Mail!");
24     }
25 
26 }

6.运行结果截图:

二、带附件的邮件发送

1.定义发送带附件的邮件接口:

1      /**
2      * 发送带附件的邮件
3      * @param to:收件人
4      * @param subject : 标题
5      * @param content:邮件内容
6      * @param attachment:附件
7      */
8     void sendAttachmentEmail(String to,String subject,String content,File attachment);

2.实现此业务:

   @Override
    public void sendAttachmentEmail(String to, String subject, String content, File attachment) {
        //创建多用途互联网邮件
        MimeMessage message = sender.createMimeMessage();
        
        try {
            //封装多用途互联网邮件
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(from);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(content);
            helper.addAttachment("附件", attachment);
        } catch (Exception e) {
            e.printStackTrace();
        }
        sender.send(message);
    }

3.定义测试:

1   @Test
2     /**
3      * 多用途互联网邮件
4      */
5     public void sendAttachmentEmail() {
6         File attachment = new File("src/main/resources/static/1.txt");
7         emailService.sendAttachmentEmail("[email protected]", "122", "Hello Mail!",attachment);
8     }

4.运行结果截图:


                                        2018-07-16

猜你喜欢

转载自www.cnblogs.com/TimerHotel/p/springboot03.html