在springboot 4.3中启用邮件发送需完成三步:引入spring-boot-starter-mail及thymeleaf或freemarker依赖;配置application.yml中smtp参数(端口必须为587);将模板文件置于src/main/resources/templates/并按引擎要求命名,否则启动报错。

要在SpringBoot 4.3项目中快速启用通知类邮件发送能力,必须完成依赖引入、SMTP安全配置、模板引擎绑定三步闭环,缺一不可——否则启动时JavaMailSender会因找不到bean或模板路径而抛出NoSuchBeanDefinitionException或TemplateNotFoundException。
添加核心依赖
打开pom.xml,在<dependencies></dependencies>节点内插入以下两项:
第一项是邮件基础支持:<dependency><groupid>org.springframework.boot</groupid><artifactid>spring-boot-starter-mail</artifactid></dependency>
第二项选其一:若用Thymeleaf(推荐),加<dependency><groupid>org.springframework.boot</groupid><artifactid>spring-boot-starter-thymeleaf</artifactid></dependency>;若用FreeMarker,加<dependency><groupid>org.springframework.boot</groupid><artifactid>spring-boot-starter-freemarker</artifactid></dependency>。注意SpringBoot 4.3已移除对Velocity的默认支持,勿再引用旧版starter。
配置application.yml中的SMTP参数
在src/main/resources/application.yml中写入以下内容(以QQ邮箱为例):
spring:
mail:
host: smtp.qq.com
port: 587
username: your_email@qq.com
password: your_authorization_code
protocol: smtp
properties:
mail.smtp.auth: true
mail.smtp.starttls.enable: true
mail.smtp.starttls.required: true
mail.smtp.connectiontimeout: 5000
mail.smtp.timeout: 3000
mail.smtp.writetimeout: 5000
【端口必须用587而非465】 SpringBoot 4.3默认启用STARTTLS协商机制,465端口仅支持SSL直连,会导致握手失败并抛出javax.net.ssl.SSLHandshakeException。
创建模板文件并放置到正确路径
方法一(Thymeleaf):在src/main/resources/templates/下新建notify.html,内容示例:
<p th:text="'您好,'+${name}+'!'"></p><div class="aritcle_card flexRow artxards">
<div class="artcardd flexRow">
<a class="aritcle_card_img" rel="nofollow" href="/ai/1542" title="Anybot"><img
src="https://img.php.cn/upload/ai_manual/000/000/000/175680234166583.jpg" alt="Anybot" onerror="this.onerror='';this.src='/static/lhimages/moren/morentu.png'" ></a>
<div class="aritcle_card_info flexColumn">
<a rel="nofollow" href="/ai/1542" title="Anybot" class="overflowclass">Anybot</a>
<p class="overflowclass">Anybot是一款面向客服、销售和客户运营自动化的智能聊天机器人平台。</p>
</div>
<a rel="nofollow" href="/ai/1542" title="Anybot" class="aritcle_card_btn flexRow flexcenter"><b></b><span>下载</span>
</a>
</div>
</div>
<p>您的订单<span th:text="${orderId}"></span>已发货。</p>
方法二(FreeMarker):在src/main/resources/templates/下新建notify.ftl,内容示例:
<p>您好,${name}!</p>
<p>您的订单${orderId}已发货。</p>
注意:FreeMarker模板后缀必须为.ftl,Thymeleaf必须为.html,且template-loader-path配置值必须与实际路径严格一致,多一个斜杠或少一个都会导致模板加载失败。
编写MailService发送模板邮件
第一步:定义接口MailService.java:
public interface MailService {
void sendTemplateMail(String to, String subject, Map<string object> model);</string>
}
第二步:实现类MailServiceImpl.java中注入对应模板引擎:
若用Thymeleaf,注入@Autowired private TemplateEngine templateEngine;;
若用FreeMarker,注入@Autowired private FreeMarkerConfigurer freeMarkerConfigurer;。
第三步:构建邮件正文:
Thymeleaf方式:创建Context context = new Context(); → context.setVariables(model); → String content = templateEngine.process("notify", context);
FreeMarker方式:获取Configuration config = freeMarkerConfigurer.getConfiguration(); → Template template = config.getTemplate("notify.ftl"); → StringWriter writer = new StringWriter(); → template.process(model, writer); → String content = writer.toString();
第四步:使用MimeMessageHelper组装并发送:
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
helper.setFrom(fromEmail);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content, true); //第二个参数true表示启用HTML解析
mailSender.send(message);










