今天看啥  ›  专栏  ›  一枕江风

一文快速搞懂Springboot发送邮件操作

一枕江风  · CSDN  ·  · 2019-01-01 00:00

写在前面: 从2018年底开始学习SpringBoot,也用SpringBoot写过一些项目。这里对学习Springboot的一些知识总结记录一下。如果你也在学习SpringBoot,可以关注我,一起学习,一起进步。

目录

开通QQ邮箱的POP3/SMPT协议

发送邮件所需要的依赖文件

相关配置

发送邮件方法


开通QQ邮箱的POP3/SMPT协议

打开qq邮箱点击设置,之后点击账户,往下翻,如图。

找到POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务,点击开启,开启后会出现一个授权码,发送邮件时会用到。

发送邮件所需要的依赖文件

<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-mail</artifactId>
</dependency>

相关配置

在application.properties配置文件中配置发邮件的相关配置:

#设置字符集
spring.mail.default-encoding=UTF-8
spring.mail.host=smtp.qq.com
#发送者的邮箱密码,这个密码不是QQ密码,而且上面的授权密码
spring.mail.password=发送者的邮箱密码
#端口
spring.mail.port=587
#协议
spring.mail.protocol=smtp
#发送者的邮箱账号
spring.mail.username=1234567@qq.com

发送邮件方法

新建一个测试控制器,编写发送邮件方法(一个不带附件发送,一个带附件发送),代码如下:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

@RestController
public class IndexController {
    //发送者的邮箱账号
    @Value("${spring.mail.username}")
    private String mailusername;
    @Autowired
    JavaMailSender jms;

    @RequestMapping(value = "send")
    public String send(){
        try {
            //建立邮件消息
            SimpleMailMessage mainMessage = new SimpleMailMessage();
            //发送者邮箱
            mainMessage.setFrom(mailusername);
            //接收者邮箱
            mainMessage.setTo("123@qq.com");
            //发送的邮件标题
            mainMessage.setSubject("Springboot发送邮件");
            //发送的内容
            mainMessage.setText("邮件内容");
            System.out.println("123");
            //发送
            jms.send(mainMessage);
            return "发送成功";
        }catch (Exception e){
            return "发送失败";
        }
    }

    //添加附件发送
    @RequestMapping(value = "sends")
    public String sends() {
        try {
            //建立邮件消息
            MimeMessage mainMessage = jms.createMimeMessage();
            //用MimeMessageHelper组装复杂邮件,第二个参数为true,可以发送附件
            MimeMessageHelper helper = new MimeMessageHelper(mainMessage, true);
            //发送者邮箱
            helper.setFrom(mailusername);
            //接收者邮箱
            helper.setTo("123@qq.com");
            //发送的邮件标题
            helper.setSubject("Springboot发送邮件");
            //发送的内容
            helper.setText("邮件内容");
            //添加附件
            helper.addAttachment("ATM.pptx", new File("E:\\HTML\\ATM.pptx"));
            //发送
            jms.send(mainMessage);
            return "发送成功";
        }catch (Exception e){
            return "发送失败";
        }
    }

}

测试效果

运行项目,在浏览器中输入 http://localhost:8080/send 发送邮件到对应的邮箱。可以看到标题和内容都是对应的(不带附件发送)。如下:

访问 http://localhost:8080/sends 地址,发送带附件的邮件,效果如下:

最后有什么不足之处,欢迎大家指出,期待与你的交流。如果感觉对你有帮助,点个赞在走呗 O(∩_∩)O ~




原文地址:访问原文地址
快照地址: 访问文章快照