使用spring-boot-starter-mail发送邮件,HTML,附件

前言

这里使用的是spring-boot-starter-mail
当然了,你也可以使用com.sun.mail(javax.mail)Hutool对这个进行了封装,
但是我的项目是springboot3javax改为了jakarta,导致我无法使用Hutool封装的(在我写这篇文章时)
所以,自己写咯,既然自己写,那就用springboot提供的mail包咯

POM依赖

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

完整的测试类

package cn.daenx.myadmin;

import jakarta.mail.internet.MimeMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * 使用spring-boot-starter-mail发送邮件
 *
 * @author DaenMax
 */
public class testSend {
    public static void main(String[] args) {
//        String toEmail = "1330166565@qq.com,wangjingen163@163.com";
        String toEmail = "1330166565@qq.com";

        //发送普通文字邮件,不带附件
//        Boolean aBoolean = sendEmail(toEmail, "测试阿萨德", "测试邮件111", false, null);

        //发送HTML邮件,不带附件
//        Boolean aBoolean = sendEmail(toEmail, "测试HHHH", "<h1>分手吧</h1>", true, null);

        //发送HTML邮件,带附件
        List<File> list = new ArrayList<>();
        File file1 = new File("C:\\Users\\13301\\Desktop\\接口测试.e");
        File file2 = new File("C:\\Users\\13301\\Desktop\\1.txt");
        list.add(file1);
        list.add(file2);
        Boolean aBoolean = sendEmail(toEmail, "测试LLLL", "<h1>分手吧</h1>", true, list);

        System.out.println("发送邮件结果:" + aBoolean);
    }

    /**
     * 发送邮件
     * 按照系统邮箱配置的使用模式进行选择邮箱号
     *
     * @param toEmail  多个用,隔开
     * @param subject
     * @param content
     * @param isHtml   是否是HTML
     * @param fileList 附件内容,留空则无
     * @return
     */
    public static Boolean sendEmail(String toEmail, String subject, String content, Boolean isHtml, List<File> fileList) {
        JavaMailSenderImpl javaMailSender = getJavaMailSenderImpl();
        //以下两种写法都可以
//        String from = "1330166564@qq.com";
        String from = "MyAdmin<1330166564@qq.com>";
        try {
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            MimeMessageHelper mimeMessageHelper;
            mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
            mimeMessageHelper.setFrom(from);
            mimeMessageHelper.setTo(toEmail.split(","));
            mimeMessageHelper.setSubject(subject);
            mimeMessageHelper.setText(content, isHtml);
            if (fileList != null) {
                for (File file : fileList) {
                    mimeMessageHelper.addAttachment(file.getName(), file);
                }
            }
            javaMailSender.send(mimeMessage);

        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public static JavaMailSenderImpl getJavaMailSenderImpl() {
        JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
        javaMailSender.setHost("smtp.qq.com");
        javaMailSender.setUsername("5201314@qq.com");
        javaMailSender.setPassword("ewduxxxxxxxhgd");
        javaMailSender.setPort(587);
        javaMailSender.setDefaultEncoding("UTF-8");
        javaMailSender.setProtocol(JavaMailSenderImpl.DEFAULT_PROTOCOL);
        Properties properties = new Properties();
        properties.setProperty("mail.smtp.timeout", "25000");
        properties.setProperty("mail.smtp.auth", "true");
        properties.setProperty("mail.smtp.socketFactoryClass", "javax.net.ssl.SSLSocketFactory");
        javaMailSender.setJavaMailProperties(properties);
        return javaMailSender;
    }
}

最简单的文字邮件,测试类

package cn.daenx.myadmin;

import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;

import java.util.Properties;

public class testSend {
    public static void main(String[] args) {
        JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
        javaMailSender.setHost("smtp.qq.com");
        javaMailSender.setUsername("5201314@qq.com");
        javaMailSender.setPassword("ewduxxxxxxxhgd");
        javaMailSender.setPort(587);
        javaMailSender.setDefaultEncoding("UTF-8");
        javaMailSender.setProtocol(JavaMailSenderImpl.DEFAULT_PROTOCOL);
        Properties properties = new Properties();
        properties.setProperty("mail.smtp.timeout", "25000");
        properties.setProperty("mail.smtp.auth", "true");
        properties.setProperty("mail.smtp.socketFactoryClass", "javax.net.ssl.SSLSocketFactory");
        javaMailSender.setJavaMailProperties(properties);

        SimpleMailMessage message = new SimpleMailMessage();
        message.setSubject("测试发送邮件3");
        //message.setFrom("5201314@qq.com");
        message.setFrom("MyAdmin<5201314@qq.com>");
        String toEmail = "1330166565@qq.com,testwy@163.com";
        message.setTo(toEmail.split(","));
        message.setText("这是一封测试邮件,收到即为正常");

        javaMailSender.send(message);
    }
}
posted @ 2023-05-11 09:43  DaenMax  阅读(562)  评论(0编辑  收藏  举报