tent().toString().trim();
}
private String getSubject(WiserMessage wiserMessage) throws MessagingException {
return wiserMessage.getMimeMessage().getSubject();
}
private SimpleMailMessage composeEmailMessage() {
SimpleMailMessage mailMessage = new SimpleMailMessage();
mailMessage.setTo(userTo);
mailMessage.setReplyTo(userFrom);
mailMessage.setFrom(userFrom);
mailMessage.setSubject(subject);
mailMessage.setText(textMail);
return mailMessage;
}
}
在测试中,@Before 和 @After 方法负责启动和停止邮件服务器。
请注意,我们装配了 JavaMailSender bean — 该 bean 是由 Spring Boot 自动创建。
与 Boot 中的其他默认值一样,JavaMailSender 的 email 设置可以在 application.properties 中自定义:
spring.mail.host=localhost
spring.mail.port=25
spring.mail.properties.mail.smtp.auth=false
我们在 localhost:25 上配置了邮件服务器,不需要身份验证。
6、结论
在本文中,我们介绍了 Starter,解释了为什么我们需要它们,并提供了如何在项目中使用它们的示例。
让我们回顾一下使用 Spring Boot starter 的好处:
- 增加 pom 可管理性
- 生产就绪、测试与依赖配置支持
- 减少项目的整体配置时间
这里(https://github.com/spring-projects/spring-boot/tree/master/spring-boot-starters)可以找到相关的 starter 列表。示例源码可以在这里(https://github.com/eugenp/tutorials/tree/master/spring-boot)找到。
原文示例代码
https://github.com/eugenp/tutorials/tree/master/spring-boot
|