标签:work pass ext simple framework one 邮箱 start 邮件
引入maven
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
yml配置
spring:
mail:
host: smtp.163.com #邮件服务器地址,邮箱不一样,服务器地址不一样
username: #邮箱用户名
password: #一般使用授权码
properties:
‘mail.smtp.ssl.enable‘: ‘true‘ #设置安全连接
发送邮件工具类
MailUtis.java
package cn.stylefeng.guns.utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
@Component
public class MailUtil {
@Autowired
private JavaMailSenderImpl javaMailSender;
public static MailUtil mailUtil;
@PostConstruct
public void init(){
mailUtil=this;
mailUtil.javaMailSender=this.javaMailSender;
}
public static void sendMail(){
SimpleMailMessage mailMessage=new SimpleMailMessage();
mailMessage.setSubject("测试"); #主题
mailMessage.setText("aaa"); #发送的内容
mailMessage.setTo("530@qq.com"); #发送给谁邮箱地址
mailMessage.setFrom("tfe@163.com"); #发送人,这里与yml配置的username一样
mailUtil.javaMailSender.send(mailMessage);
}
}
标签:work pass ext simple framework one 邮箱 start 邮件
原文地址:https://www.cnblogs.com/pxblog/p/12231127.html