SpringBoot发送邮件工具类

1、引入依赖

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

2、配置邮箱信息(授权码的获取可以查看QQ邮箱->设置,里面可以生成授权码)

# 名称不要改变,依赖包会自动寻找spring.mail开头的配置信息
# 用户名
spring.mail.username=
#授权码
spring.mail.password=
# 协议 一般是smtp.exmail.qq.com
spring.mail.host=
# 端口
spring.mail.port=
# smtp配置
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true

3、因为配置信息的前缀,使用Spring默认的邮箱配置前缀,所以不需要自己去创建实体类去配置
源代码
4、工具类

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
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.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

/**
 * 邮件工具类
 *
 * @author chenkailiang
 * @date 2021/1/8
 */
@Service
@Slf4j
public class SendMailUtil {

    /**
     * 发件人
     */
    @Value("${xxx}")
    private String mailAddress;

    @Autowired
    private JavaMailSender mailSender;

    /**
     * 发送普通文本邮件
     * @param from 发件人,如果未指定则使用默认配置的 service28@bulkea.oristand.com
     * @param to 收件人
     * @param cc 抄送
     * @param subject 邮件主题
     * @param content 邮件内容
     */
    public void sendSimpleMail(String from,String[] to , String[] cc,String subject, String content) {

        SimpleMailMessage message = new SimpleMailMessage();
        //发件人
        String realFrom = from;
        if(StringUtils.isBlank(from)){
            realFrom = mailAddress;
        }
        message.setFrom(realFrom);
        //收信人
        message.setTo(to);
        //抄送
        message.setCc(cc);
        //主题
        message.setSubject(subject);
        //文本
        message.setText(content);
        mailSender.send(message);
        log.info("发送邮件成功:发件人:{},收件人:{},抄送:{},主题:{}",realFrom, to,cc, subject);
    }

    /**
     * 发送html邮件
     * @param from 发件人
     * @param to 收件人
     * @param cc 抄送
     * @param subject 主题
     * @param content 内容
     */
    public void sendHtmlMail(String from ,String[] to,String[] cc, String subject, String content) {
        //使用MimeMessage,MIME协议
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper;
        String realFrom = from;
        try {
            helper = new MimeMessageHelper(message, true);
            if(StringUtils.isBlank(from)){
                realFrom = mailAddress;
            }
            helper.setFrom(realFrom);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(content, true);
            mailSender.send(message);
            log.info("发送HTML邮件成功:发件人:{},收件人:{},抄送:{},主题:{}",realFrom, to,cc, subject);
        } catch (MessagingException e) {
            e.printStackTrace();
            log.error("发送HTML邮件失败:发件人:{},收件人:{},抄送:{},主题:{}",realFrom, to,cc, subject);
        }

    }

    /**
     * 发送带附件邮件
     * @param from 发件邮箱
     * @param to 收件人
     * @param cc 抄送
     * @param subject 主题
     * @param content 内容
     * @param filePath 附件地址
     */
    public void sendAttachmentMail(String from,String[] to, String[] cc,String subject, String content, String filePath) {
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper;
        String realFrom = from;
        try {
            //true代表支持多组件,如附件,图片等
            helper = new MimeMessageHelper(message, true);
            if(StringUtils.isBlank(from)){
                realFrom = mailAddress;
            }
            helper.setFrom(realFrom);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(content, true);
            FileSystemResource file = new FileSystemResource(new File(filePath));
            String fileName = file.getFilename();
            //添加附件,可多次调用该方法添加多个附件
            helper.addAttachment(fileName, file);
            mailSender.send(message);
            log.info("发送带附件邮件成功:发件人:{},收件人:{},抄送:{},主题:{}",realFrom, to,cc, subject);
        } catch (MessagingException e) {
            e.printStackTrace();
            log.error("发送带附件邮件成功:发件人:{},收件人:{},抄送:{},主题:{}",realFrom, to,cc, subject);
        }


    }

    /**
     * 发送带图片邮件
     * @param from 发件人
     * @param to 收件人
     * @param cc 抄送
     * @param subject 主题
     * @param content 内容
     * @param rscPath 图片路径
     * @param rscId 图片id
     */
    public void sendInlineResourceMail(String from,String[] to, String[] cc,String subject, String content, String rscPath, String rscId) {

        MimeMessage message = mailSender.createMimeMessage();
        String realFrom = from;
        MimeMessageHelper helper;
        try {
            helper = new MimeMessageHelper(message, true);
            if(StringUtils.isBlank(from)){
                realFrom = mailAddress;
            }
            helper.setFrom(realFrom);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(content, true);
            FileSystemResource res = new FileSystemResource(new File(rscPath));
            //重复使用添加多个图片
            helper.addInline(rscId, res);
            mailSender.send(message);
            log.info("发送带图片邮件成功:发件人:{},收件人:{},抄送:{},主题:{}",realFrom, to,cc, subject);
        } catch (MessagingException e) {
            e.printStackTrace();
            log.error("发送带图片邮件成功:发件人:{},收件人:{},抄送:{},主题:{}",realFrom, to,cc, subject);
        }
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值