0
点赞
收藏
分享

微信扫一扫

spring-boot集成e-mail


1、注入依赖

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

2、添加e-mail配置

spring.mail.host=smtp.163.com
spring.mail.username=xxx@163.com
spring.mail.password=xxx
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true

3、书写工具类

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

@Component
public class EmailUtils {

@Autowired
private JavaMailSender mailSender; //自动注入的Bean

@Value("${spring.mail.username}")
private String Sender; //读取配置文件中的参数

public void sendSimpleMail(String who,String title,String content){
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(Sender);
message.setTo(who); //自己给自己发送邮件
message.setSubject(title);
message.setText(content);
mailSender.send(message);
}

}



举报

相关推荐

0 条评论