Java SpringBoot发送邮件,HTML,附件 

图片[1] - Java SpringBoot发送邮件,HTML,附件  - 老友小站

引入依赖

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

测试类

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 = "xxxxxx@qq.com,xxxxxx@163.com";
        String toEmail = "xxxxxx@qq.com";

        //发送普通文字邮件,不带附件
        //Boolean aBoolean = sendEmail(toEmail, "标题", "内容", false, null);

        //发送HTML邮件,不带附件
        //Boolean aBoolean = sendEmail(toEmail, "标题", "<h1>内容</h1>", true, null);

        //发送HTML邮件,带附件
        List<File> list = new ArrayList<>();
        File file1 = new File("文件路径");
        File file2 = new File("文件路径");
        list.add(file1);
        list.add(file2);

        Boolean aBoolean = sendEmail(toEmail, "标题", "<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 = "xxxxxx@qq.com";
        String from = "xxx<xxxxxx@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("xxxxxx@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;
    }
}
© 版权声明
THE END
喜欢就支持一下吧
点赞10 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容