SpringBoot 邮件任务

SpringBoot 邮件任务

今天学习了异步任务和邮件任务等等,写篇随笔记录一下邮件任务

导入依赖(也可以自己创建SpringBoot项目时勾选上启动器)

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

开启POP3/SMTP服务(这里以QQ邮箱为例)
登录QQ邮箱,在设置->帐户寻找下面的服务

编写配置文件(这里使用properties)

# 填写自己的邮箱 这里以QQ邮箱为例
spring.mail.username=*********@qq.com
# 这里是自己邮箱的授权码,如果是QQ邮箱,需要自己去开启服务,并且获取授权码,其它邮箱就是自己的密码
spring.mail.password=************* 
spring.mail.host=smtp.qq.com
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
# 开启加密验证
spring.mail.properties.mail.smtp.ssl.enable=true

测试

	@Autowired
    JavaMailSender javaMailSender;
	@Test
    void contextLoads() {
        //发送简单邮件
        SimpleMailMessage message = new SimpleMailMessage();
        message.setSubject("激活邮件");							//设置标题
        message.setText("您好,这是一封测试邮件,无需回复");           //设置正文
        message.setTo("2933253458@qq.com");						//发给谁
        message.setFrom("2933253458@qq.com");					//谁发的
        javaMailSender.send(message);
    }

	@Autowired
	JavaMailSender javaMailSender;
	@Test
    public void test01() throws MessagingException {
        //发送复杂邮件(html+附件)
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage,true);
        mimeMessageHelper.setSubject("激活邮件");   //设置邮件主题
        mimeMessageHelper.setText("<p>您好,这是一封测试邮件!<p>",true);//开启解析html
        //发送附件
        mimeMessageHelper.addAttachment("图片.png",new File("C:\\Users\\gaoteng\\Desktop\\Typora截图\\QQ截图20200613091605.png"));
        //发给谁
        mimeMessageHelper.setTo("2933253458@qq.com");
        //谁发的
        mimeMessageHelper.setFrom("2933253458@qq.com");
        javaMailSender.send(mimeMessage);
    }

抽取工具类(这是在网上找的某大佬的,后面会附上链接)

package com.gaoteng.service;


import java.io.File;
import java.util.Map;

/**
 * created by lxh
 * Date: 2017/12/4
 * Time: 8:46
 * Description: 邮件发送服务类,不保证可靠性,由于发件邮箱限制,和收件者邮箱的限制,有可能邮件会被回退
 */
public interface MailSendService {

    /**
     * 发送简单文本邮件
     * @param to 收件人,一组,不能为空
     * @param subject 邮件主题
     * @param content 邮件内容
     * @throws Exception
     */
    void sendSimpleMail(String[] to, String subject, String content) throws Exception;

    /**
     * 发送Html邮件
     * @param to 收件人
     * @param subject 邮件主题
     * @param html 邮件内容,html
     * @throws Exception
     */
    void sendHtmlMail(String[] to, String subject, String html) throws Exception;

    /**
     * 发送带附件的邮件
     * @param to 收件人
     * @param subject 邮件主题
     * @param content 邮件内容
     * @param files 附件,key请将文件的扩展名也加上,否则无法预览。如:picture.jpg
     * @throws Exception
     */
    void sendAttachmentsMail(String[] to, String subject, String content, Map<String, File> files) throws Exception;

    /**
     * 发送带静态资源的邮件<br />
     * 将文件内容显示在邮件内容中,一般为图片,如果为文件,有待测试
     * @param to 收件人
     * @param subject 邮件主题
     * @param html html文本
     * @param files 要加入html中的静态资源<br />
     * example: <br />
     * html: "带静态资源的邮件内容 图片:img src='cid:dog'    img src='cid:pig'"<br />
     * files: [{key:dog, value: dog.jpg},{key:pig, value:pig.jpg}]<br />
     * files中的key要和html中的cid一一对应。
     * @throws Exception
     */
    void sendInlineMail(String[] to, String subject, String html, Map<String, File> files) throws Exception;


}

package com.gaoteng.service.impl;

import com.gaoteng.service.MailSendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;


import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.Map;
import java.util.Set;

/**
 * created by lxh
 * Date: 2017/12/4
 * Time: 8:54
 * Description:
 */
@Service
public class MailSendServiceImpl implements MailSendService {

    @Autowired
    private JavaMailSender mailSender;

    //@Value("spring.mail.username")
    private static final String sender = "2933253458@qq.com";

    @Override
    public void sendSimpleMail(String[] to, String subject, String content) throws Exception {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(sender);
        message.setTo(to);
        message.setSubject(subject);
        message.setText(content);
        mailSender.send(message);
    }

    @Override
    public void sendHtmlMail(String[] to, String subject, String html) throws Exception {
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom(sender);
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(html, true);
        mailSender.send(message);
    }

    @Override
    public void sendAttachmentsMail(String[] to, String subject, String content, Map<String, File> files) throws Exception {
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom(sender);
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(content);
        //注意项目路径问题,自动补用项目路径
        //FileSystemResource file = new FileSystemResource(new File("src/main/resources/static/image/picture.jpg"));
        //加入邮件
        //helper.addAttachment("图片.jpg", file);
        Set<Map.Entry<String,File>> fileSet = files.entrySet();
        for (Map.Entry f : fileSet) {
            helper.addAttachment((String) f.getKey(), (File) f.getValue());
        }
        mailSender.send(message);
    }

    @Override
    public void sendInlineMail(String[] to, String subject, String html, Map<String, File> files) throws Exception {
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom(sender);
        helper.setTo(to);
        helper.setSubject(subject);
        //第二个参数指定发送的是HTML格式,同时cid:是固定的写法
        //"<html><body>带静态资源的邮件内容 图片:<img src='cid:picture' /></body></html>"
        helper.setText(html, true);
        Set<Map.Entry<String,File>> fileSet = files.entrySet();
        for (Map.Entry f : fileSet) {
            helper.addInline((String) f.getKey(), (File) f.getValue());
        }
        //FileSystemResource file = new FileSystemResource(new File("src/main/resources/static/image/picture.jpg"));
        //helper.addInline("picture",file);
        mailSender.send(message);
    }
}

因为测试结果太多,所以我就不贴图片了.

本篇随笔参考使用spring-boot-starter-mail实现邮件发送
特别感谢!!!

posted @ 2020-06-13 18:08  毕竟是曾经  阅读(224)  评论(0编辑  收藏  举报