SpringBoot配置发送邮件

本文介绍如何使用 Spring Boot 发送多种类型的电子邮件,包括纯文本邮件和包含 HTML 内容及图片的邮件。文中提供了具体的 Java 代码实现,并详细说明了如何配置邮件发送的相关属性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Java代码

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.Component;

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

@Component
public class EmailUtil {

    @Autowired
    private JavaMailSender sender;

    @Value("${spring.mail.username}")
    private String from; // 发件人

    @Value("${spring.mail.to}")
    private String toStr; // 收件人

    @Value("${spring.mail.cc}")
    private String ccStr; // 抄送人

    /**
     * 发送纯文本邮件
     * @param subject 标题
     * @param content 内容
     */
    public void sendSimpleMail(String subject, String content) {

        String[] to = toStr.split(",");
        String[] cc = ccStr == null || "".equals(ccStr) ? null : ccStr.split(",");

        SimpleMailMessage message = new SimpleMailMessage();

        message.setFrom(from);
        message.setTo(to);
        if (cc != null && cc.length > 0) {
            message.setCc(cc);
        }
        message.setSubject(subject);
        message.setText(content);

        sender.send(message);

    }

    /**
     * 发送带html的邮件
     * @param subject 标题
     * @param text html片段
     * @param imgPath html片段中图片路径
     */
    public void senderHtmlMail(String subject, String text, String imgPath) throws MessagingException {

        String[] to = toStr.split(",");
        String[] cc = ccStr == null || "".equals(ccStr) ? null : ccStr.split(",");

        MimeMessage msg = sender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(msg, true);

        helper.setFrom(from);
        helper.setTo(to);
        if (cc != null && cc.length > 0) {
            helper.setCc(cc);
        }
        helper.setSubject(subject);
        helper.setSentDate(new Date());
        helper.setText(text, true);

        if (imgPath != null && !"".equals(imgPath)) {
            FileSystemResource res = new FileSystemResource(new File(imgPath));
            helper.addInline("imagePicId", res);
        }

        sender.send(msg);

    }
}
html模版(使用freemark)

引入依赖

<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>

Java代码

// 邮件html拼接
Map<String, Object> model = new HashMap<>();
model.put("timeSpanStr", year + "年 第" + weekNum + "周 " + timeSpan);
//读取 html 模板
Template template = freeMarkerConfigurer.getConfiguration().getTemplate("mail.html");
String html = FreeMarkerTemplateUtils.processTemplateIntoString(template, model); // html片段
EmailUtil.senderHtmlMail(... ... // 调用发送邮件方法
配置文件
#邮件配置
spring.mail.host= smtp.126.comspring.mail.username= elf***@126.com
spring.mail.password= *** ***
spring.mail.default-encoding= UTF-8
spring.mail.port= 25
spring.mail.to= elf***@126.com
spring.mail.cc=

注意:部分邮箱需要设置开启POP3/SMTP授权服务,在邮箱设置中进行设置

部分SpringBoot的jar中已整合mail,如未引入jar需在pom.xml中引入依赖

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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值