Spring Boot 发送邮件

1、前期准备

邮件发送功能是每个完整业务系统要集成的功能之一,在进行邮件发送功能开发之前,需要进行一些前提条件的准备。

1、准备发送邮件的邮箱,这里以企业邮箱为例。登录企业微信邮箱 —> 设置,在收发信设置中开启IMAP/SMTP 和POP/STMP服务(前提是所属企业先开启,需要登录企业微信后台管理配置)。

Spring Boot 发送邮件_第1张图片

2、邮箱绑定配置,配置邮箱绑定,可以绑定自己的微信,然后开启安全登录,生成客户端专用密码,此密码只显示一次,建议复制保存。

Spring Boot 发送邮件_第2张图片

2、项目配置

创建一个基本的SprinBoot项目,再pom.xml文件中引入发送邮件的依赖。


<dependency>
  <groupId>org.springframework.bootgroupId>
  <artifactId>spring-boot-starter-mailartifactId>
dependency>

在 application.yml 配置文件中添加spring对email的支持配置。

# 邮箱服务器地址
mail:
 host: smtp.exmail.qq.com # 这里是发送服务器的域名
 username: [email protected] # 这里是发送邮箱
 password: 1234 # 这里是客户端专用密码
 default-encoding: UTF-8
 protocol: smtp
 properties:
  mail:
   smtp:
    auth: true
    port: 465 #发件邮箱端口
    ssl:
     enable: true #是否启用ssl

3、服务代码

服务类

public interface IEmailService {
    /**
     * 发送简单文本邮件
     */
    void sendSimpleMail(String receiveEmail, String subject, String content);
    /**
     * 发送HTML格式的邮件
     */
    void sendHtmlMail(String receiveEmail, String subject, String emailContent) throws MessagingException;
    /**
     * 发送包含附件的邮件
     */
    void sendAttachmentsMail(List<String> receiveEmails, String subject, String emailContent, List<String> filePathList) throws MessagingException;
}

实现类

@Service
public class IEmailServiceImpl implements IEmailService {

    @Resource
    private JavaMailSender mailSender;


    @Value("${spring.mail.username}")
    private String fromEmail;


    /**
     * 发送简单文本邮件
     */
    public void sendSimpleMail(String receiveEmail, String subject, String content) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(fromEmail);
        message.setTo(receiveEmail);
        message.setSubject(subject);
        message.setText(content);
        mailSender.send(message);
    }

    /**
     * 发送Html格式的邮件
     */
    public  void sendHtmlMail(String receiveEmail,String subject,String emailContent) throws MessagingException
    {
        init(receiveEmail, subject, emailContent, mailSender, fromEmail);
    }

    public static void init(String receiveEmail, String subject, String emailContent, JavaMailSender mailSender, String fromEmail) throws MessagingException {
        MimeMessage message= mailSender.createMimeMessage();
        MimeMessageHelper helper=new MimeMessageHelper(message,true);
        helper.setFrom(fromEmail);
        helper.setTo(receiveEmail);
        helper.setSubject(subject);
        helper.setText(emailContent,true);
        mailSender.send(message);
    }

    /**
     * 发送包含附件的邮件,filePathList 传本地文件路径
     */
    public void sendAttachmentsMail(List<String> receiveEmails, String subject, String emailContent, List<String> filePathList) throws MessagingException {
        MimeMessage message = mailSender.createMimeMessage();
        //带附件第二个参数true
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom(fromEmail);
        helper.setTo(receiveEmails.toArray(new String[0]));
        helper.setSubject(subject);
        helper.setText(emailContent, true);
        //添加附件资源
        for (String item : filePathList) {
            FileSystemResource file = new FileSystemResource(new File(item));
            String fileName = item.substring(item.lastIndexOf(File.separator));
            helper.addAttachment(fileName, file);
        }
        //发送邮件
        mailSender.send(message);
    }
}

4、测试示例

以发送简单文本测试

String receiveEmail = "[email protected]";
emailService.sendSimpleMail(receiveEmail, "测试邮件", "springboot测试邮件");

成功接收到邮件

Spring Boot 发送邮件_第3张图片

你可能感兴趣的:(spring,java,spring,boot)