一、引入jar
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
二、添加mail配置
# 邮箱
spring:
mail:
host: smtp.126.com
password: GJZKGIOYUGWDZMUF
username: cnycdata@126.com
properties:
mail:
smtp:
auth: true
starttls:
enable: true
required: true
三、简单的发送邮件
public void sendSimpleMail(){
SimpleMailMessage message = new SimpleMailMessage();
message.setSubject("这是测试邮件");
message.setText("这是正文");
message.setTo("xxxx@qq.com");
message.setFrom("xxxxx@126.com");
javaMailSender.send(message);
}
四、发送带有附件的
public void sendMimeMail(){
MimeMessage message = javaMailSender.createMimeMessage();
message.addHeader("X-Mailer",
"Microsoft Outlook Express 6.00.2900.2869");//防止发送到垃圾邮箱
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom("发送方Mail");
helper.setSubject("激光码信息");
helper.setText("激光码信息");
FileSystemResource ss = new FileSystemResource(new File("C:/jgm.xlsx"));
helper.addAttachment("激光码信息.xlsx", ss);
helper.setTo("接收方Mail");
javaMailSender.send(message);
}