引言:我这里用的是网易云邮箱先通过设置开启 POP3/SMTP服务
创建springboot程序
(1)以maven的方式或者已springboot快速生成都可以
(2)去maven仓库查询(https://mvnrepository.com/)搜索 Spring Boot Starter Mail
编写pom.xml文件
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
编写application.yml
spring:
mail:
# 配置 SMTP 服务器地址
host: smtp.163.com
# 发送者邮箱
username: xxx.com
# 配置密码,注意不是真正的密码,而是刚刚申请到的授权码
password: xxxx
# 端口号465或587
port: 465
# 默认的邮件编码为UTF-8
default-encoding: UTF-8
# 配置SSL 加密工厂
properties:
mail:
smtp:
starttls:
enable: true
auth: true
ssl:
enable: true
socketFactoryClass: javax.net.ssl.SSLSocketFactory
#表示开启 DEBUG 模式,这样,邮件发送过程的日志会在控制台打印出来,方便排查错误
debug: true
server:
port: 8999
编写发送简单的邮件
@RestController
@RequestMapping(value = "MaillController")
public class MaillController {
//我这里采用的是构造注入(推荐使用)/或者也可以不用
private final JavaMailSender javaMailSender;
@Autowired
public MaillController(JavaMailSender javaMailSender) {
this.javaMailSender = javaMailSender;
}
//导入配置文件中发送人的邮箱
@Value("${spring.mail.username}")
private String from;
/**
如果你闲麻烦也可以把参数包装成一个实体类发送post请求
参数配置
from 发送邮件人的邮箱账号
to 收件人的邮箱账号
subject 主题
context 内容
*/
@RequestMapping(value = "/send",method = RequestMethod.GET)
public String send(String to,String subject,String context){
//简单邮件发送(SimpleMailMessage)
SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
simpleMailMessage.setFrom(from);
simpleMailMessage.setTo(to);
simpleMailMessage.setSubject(subject);
simpleMailMessage.setText(context);
javaMailSender.send(simpleMailMessage);
return "success";
}
}
发送html邮件
//html邮件发送
@RequestMapping(value = "/send01",method = RequestMethod.GET)
public String send01(String to,String subject,String context) throws MessagingException {
context=" <p style=\"background-color: red;height: 100px;width: 50px\">this 测试</p>";
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage,true);
mimeMessageHelper.setTo(to);
mimeMessageHelper.setFrom(from);
mimeMessageHelper.setCc("xiaofeng500@qq.com");// 设置抄报人邮箱(可以不填写)
mimeMessageHelper.setBcc("575814158@qq.com");
mimeMessageHelper.setSubject(subject);
mimeMessageHelper.setText(context,true);
javaMailSender.send(mimeMessage);
return "success";
}
发送附件
//发送附件
@RequestMapping(value = "/send02",method = RequestMethod.GET)
public String send02(String to,String subject,String context) throws MessagingException {
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage,true);
mimeMessageHelper.setTo(to);
mimeMessageHelper.setFrom(from);
mimeMessageHelper.setSubject(subject);
mimeMessageHelper.setCc("xiaofeng500@qq.com");// 设置抄报人邮箱(可以不填写)
mimeMessageHelper.setBcc("575814158@qq.com");
mimeMessageHelper.setText(context);
//使用流自动获取本地资源
FileSystemResource resource = new FileSystemResource(new File("D:\\常卫国.pdf"));
//获取本地资源的名称
String filename = resource.getFilename();
//调用附件发送的方法(名称 附件)
mimeMessageHelper.addAttachment(filename,resource);
javaMailSender.send(mimeMessage);
return "success";
}
如果用在项目上面也简单自己把上面的这些个分成一个util包直接调用即可。