spring-boot集成e-mail
2017-12-05 14:26
429 查看
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=true3、书写工具类
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);
}
}
<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=true3、书写工具类
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);
}
}
相关文章推荐
- SpringBoot实践之---集成mail邮件服务
- Spring Boot(十二)集成spring-boot-starter-mail发送邮件
- 第五章:SpringBoot集成spring-boot-starter-mail发送邮件
- springboot 集成mybatis Invalid bound statement (not found)异常
- spring-boot集成ehcache实现缓存机制
- 微服务,微架构[四]之springboot集成Redis缓存
- 使用Spring Boot集成FastDFS的示例代码
- Spring Boot + Spring Cloud 实现权限管理系统 后端篇(十一):集成 Shiro 框架
- spring boot & mybatis 集成二
- Spring Boot(六)集成 MyBatis 操作 MySQL 8
- SpringBoot集成Quartz实现定时器
- 35. Spring Boot集成Redis实现缓存机制【从零开始学Spring Boot】
- Spring boot集成Redis实现sessions共享时,sessions过期时间问题分析
- 搭建spring boot集成mybatis项目入门
- Spring boot 集成通用mapper
- spring boot 集成shiro记住我实现
- CAS之5.2x版本之客户端集成(Springboot)-yellowcong
- Spring Boot 系列(九)数据层-集成Spring-data-jpa
- Spring Boot 集成 log4j2
- SpringBoot | 第三十三章:Spring web Servcies集成和使用