[Spring Boot]整合Java Mail实现Outlook发送邮件
日常开发过程中,我们经常需要使用到邮件发送任务,比方说验证码的发送、日常信息的通知等。日常比较常用的邮件发送方包括:163、QQ等,本文主要讲解Outlook SMTP的开启方式、OutLook STARTTTL的配置、如何通过JavaMail来实现电子邮件的发送等。
Outlook作为微软提供的企业电子邮件服务品牌,与其他品牌不同的是:Outlook使用的加密方式为STARTTTL。
一、开启账号的SMTP服务
使用个人邮箱的话,首先,通过office.com登录你的微软邮箱。进入设置页面,点击"同步电子邮件"选项,将POP选项选为"是",然后保存即可。
如果是商用类型帐号,子账号是不具备开启SMTP选项的权限的,需要联系管理员开启。
二、添加依赖
在pom.xml中添加Java Mail的依赖
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-mail</artifactId></dependency>
三、添加配置
在yml文件中添加配置
spring:mail:username: ******@outlook.compassword: ****yourshost: smtp.office365.comport: 587properties:mail:default-encoding: UTF-8smtp:auth: truestarttls:enable: true
四、编写Service
编写service代码,实现邮件发送功能
@Service
public class EmailServiceImpl implements EmailService {@Autowiredprivate JavaMailSender mailSender;@Value("${spring.mail.username}")private String username;@Overridepublic void sendSimpleEmail(String to, String subject, String content) {SimpleMailMessage message = new SimpleMailMessage();message.setTo(to);message.setSubject(subject);message.setText(content);message