Site Overlay

JavaMail邮件服务MailUtil

需求:

统一一个公共服务模块发送邮件以及短信信息供所有项目使用

定义一个MailBean用于存放Mail数据

import lombok.Data;

import java.io.File;
/**
 * @Author Diuut
 * @Date 2020/7/9  17:25
 */
@Data
public class MailBean {
    private String subject;  //主题
    private String content;  //内容
    private String toAccount;  //发送地址
    private File attachmentFile;  //附件文件
    private String attachmentFileName;  //附件文件名
}

在yml中准备好邮件发送所需配置参数

spring:
     mail:
         host: smtp.xxxx.aliyun.com            #发送服务器地址
         port: 25                              #端口号
         username: xxxxxxx@mxxxxxxxx.net       #发送人地址
         password: xxxxxxxxxxx                 #密码

编写MailUtil

import com.miracle.message.pojo.MailBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.mail.MailProperties;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;
import java.io.UnsupportedEncodingException;

/**
 * @Author Diuut
 * @Date 2020/7/9  17:10
 */
@Component
public class MailUtil {
    @Autowired
    JavaMailSender mailSender;
    @Autowired
    MailProperties mailProperties;

    /**
     * 发送邮件
     * @param mailBean 邮件内容
     */
    public void sendMail(MailBean mailBean) {
        SimpleMailMessage mimeMessage = new SimpleMailMessage();
        mimeMessage.setFrom(mailProperties.getUsername());
        mimeMessage.setTo(mailBean.getToAccount());
        mimeMessage.setSubject(mailBean.getSubject());
        mimeMessage.setText(mailBean.getContent());
        mailSender.send(mimeMessage);
    }

    /**
     * 发送邮件-附件邮件
     *
     * @param mailBean
     */
    public boolean sendMailAttachment(MailBean mailBean) {
        try {
            MimeMessage mimeMessage = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
            helper.setFrom(mailProperties.getUsername());
            helper.setTo(mailBean.getToAccount());
            helper.setSubject(mailBean.getSubject());
            helper.setText(mailBean.getContent(), true);
            // 增加附件名称和附件
            helper.addAttachment(MimeUtility.encodeWord(mailBean.getAttachmentFileName(), "utf-8", "B"), mailBean.getAttachmentFile());
            mailSender.send(mimeMessage);
            return true;
        } catch (MessagingException | UnsupportedEncodingException e) {
            e.printStackTrace();
            return false;
        }
    }
}

//再附上邮箱的正则表达判断,应该用得着   
//emil.matches("^[0-9a-z]+\\w*@([0-9a-z]+\\.)+[0-9a-z]+$")

以上,就可以在service中@Autowire调用了,

填坑:不要乱用异步@Async,自以为异步可以提高响应速度,却导致响应太快,后台还没有处理好前台已经响应了,结果消费方返回值一直是null。

发表回复

您的电子邮箱地址不会被公开。

A beliving heart is your magic My heart
欢迎来到Diuut的个人博客,这里是我的一些零零碎碎的知识汇总,希望有能帮到你的内容。 | 蜀ICP备2021011635号-1 | Copyright © 2024 Diuut. All Rights Reserved.