哪有什么岁月静好,不过是有人替你负重前行!

Java实现发送邮件功能

一、准备工作

以QQ邮箱为例
登录QQ邮箱,设置,点击账户

找到

点击开启,发送短信

生成授权码并复制,保存起来,后面要用

二、项目集成

1.加入依赖

<!--邮件发送依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>

2.yml配置

spring:
  mail:
      host: smtp.qq.com # 配置 smtp 服务器地址
      port: 587 # smtp 服务器的端口
      username: xxxx@qq.com # 配置邮箱用户名(你的邮箱地址)
      password: xxxx # 配置申请到的授权码(刚让复制的授权码)
      default-encoding: UTF-8 # 配置邮件编码
      properties:
        mail:
          smtp:
            socketFactoryClass: javax.net.ssl.SSLSocketFactory # 配饰 SSL 加密工厂
          debug: true
      from: xxxx@qq.com # 发送方邮件,配在yml中可方便更改

3.工具类

package com.test.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

/**
 * @ClassName EmailUtil
 * @Description 邮件发送工具
 */
@Component
public class EmailUtil {

    @Value("${spring.mail.from}") // 从application.yml配置文件中获取
    private String from; // 发送发邮箱地址

    @Autowired
    private JavaMailSender mailSender;

    /**
     * 发送纯文本邮件信息
     *
     * @param to      接收方
     * @param subject 邮件主题
     * @param content 邮件内容(发送内容)
     */
    public void sendMessage(String to, String subject, String content) {
        // 创建一个邮件对象
        SimpleMailMessage msg = new SimpleMailMessage();
        msg.setFrom(from); // 设置发送发
        msg.setTo(to); // 设置接收方
        msg.setSubject(subject); // 设置邮件主题
        msg.setText(content); // 设置邮件内容
        // 发送邮件
        mailSender.send(msg);
    }

    /**
     * 发送带附件的邮件信息
     *
     * @param to      接收方
     * @param subject 邮件主题
     * @param content 邮件内容(发送内容)
     * @param files 文件数组 // 可发送多个附件
     */
    public void sendMessageCarryFiles(String to, String subject, String content, File[] files) {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
            helper.setFrom(from); // 设置发送发
            helper.setTo(to); // 设置接收方
            helper.setSubject(subject); // 设置邮件主题
            helper.setText(content); // 设置邮件内容
            if (files != null && files.length > 0) { // 添加附件(多个)
                for (File file : files) {
                    helper.addAttachment(file.getName(), file);
                }
            }
        } catch (MessagingException e) {
            e.printStackTrace();
        }
        // 发送邮件
        mailSender.send(mimeMessage);
    }
    /**
     * 发送带附件的邮件信息
     *
     * @param to      接收方
     * @param subject 邮件主题
     * @param content 邮件内容(发送内容)
     * @param file 单个文件
     */
    public void sendMessageCarryFile(String to, String subject, String content, File file) {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
            helper.setFrom(from); // 设置发送发
            helper.setTo(to); // 设置接收方
            helper.setSubject(subject); // 设置邮件主题
            helper.setText(content); // 设置邮件内容
            helper.addAttachment(file.getName(), file); // 单个附件
        } catch (MessagingException e) {
            e.printStackTrace();
        }
        // 发送邮件
        mailSender.send(mimeMessage);
    }

    public String getFrom() {
        return from;
    }

    public void setFrom(String from) {
        this.from = from;
    }
}

4.测试

package com.test;

import com.biomatch.util.EmailUtil;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.File;

@SpringBootTest
public class BlogApiApplicationTests {
    @Autowired
    private EmailUtil emailUtil;

    @Test
    void contextLoads() {
    }

    @Test
    void sendStringEmail() {
        // 测试文本邮件发送(无附件)
        String to = "xxxx@qq.com"; // 这是个假邮箱,写成你自己的邮箱地址就可以
        String title = "文本邮件发送测试";
        String content = "文本邮件发送测试";
        emailUtil.sendMessage(to, title, content);
    }

    @Test
    void sendFileEmail() {
        // 测试单个附件邮件发送
        String to = "xxxx@qq.com"; // 这是个假邮箱,写成你自己的邮箱地址就可以
        String title = "单个附件邮件发送测试";
        String content = "单个附件邮件发送测试";
        File file = new File("D:\\注解.md");
        emailUtil.sendMessageCarryFile(to, title, content, file);
    }

    @Test
    void sendFilesEmail() {
        // 测试多个附件邮件发送
        String to = "xxxx@qq.com"; // 这是个假邮箱,写成你自己的邮箱地址就可以
        String title = "多个附件邮件发送测试";
        String content = "多个附件邮件发送测试";
        File[] files = new File[2];
        files[0] = new File("D:\\注解1.md");
        files[1] = new File("D:\\注解2.md");
        emailUtil.sendMessageCarryFile(to, title, content, files[0]);
        emailUtil.sendMessageCarryFile(to, title, content, files[1]);
    }
}

参考:https://blog.csdn.net/qq_42320934/article/details/124021133

posted @ 2022-12-27 17:05  pz_slider  阅读(56)  评论(0)    收藏  举报
/* 粒子吸附*/