目录
一、开启Outlook设置
二、依赖
三、配置文件
四、代码调用
一、开启Outlook设置
开启设置如图:
二、依赖
<!-- 邮箱依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
<version>2.3.2.RELEASE</version>
</dependency>
三、配置文件
spring:
mail:
username: ******@outlook.com
password: ****yours
host: smtp.office365.com
port: 587
properties:
mail:
default-encoding: UTF-8
smtp:
ssl:
socketFactory:
class: com.sun.mail.util.MailSSLSocketFactory
fallback: false
auth: true
starttls:
enable: true
四、代码调用
@Service
public class EmailServiceImpl implements EmailService {
@Autowired
private JavaMailSender mailSender;
@Value("${spring.mail.username}")
private String username;
@Override
public void sendSimpleEmail(String to, String subject, String content) {
SimpleMailMessage message = new SimpleMailMessage();
message.setTo(to);
message.setSubject(subject);
message.setText(content);
message.setFrom(username);
mailSender.send(message);
}
}
测试成功。