JavaMail邮件服务MailUtil

2022-11-22 20:10:28 浏览数 (1)

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

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

代码语言:javascript复制
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中准备好邮件发送所需配置参数

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

编写MailUtil

代码语言:javascript复制
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。

Post Views: 120

0 人点赞