<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-mail</artifactId></dependency>
依赖
配置文件
spring.mail.username=1393087444@qq.com
spring.mail.password=*************
spring.mail.host=smtp.qq.com
#开启加密验证 表示是否自动开启安全性
spring.mail.properties.mail.smtl.ssl.enable=true
package com.qf.sping09test;import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;@SpringBootTest
class Sping09TestApplicationTests {@AutowiredJavaMailSenderImpl javaMailSender;@Testvoid contextLoads() {//一个简单的邮件~SimpleMailMessage mailMessage = new SimpleMailMessage();mailMessage.setSubject("基哥你好啊~~~");//邮件标题mailMessage.setText("谢谢你观看狂神说java课程");mailMessage.setTo("1393087444@qq.com");mailMessage.setFrom("1393087444@qq.com");javaMailSender.send(mailMessage);}@Testvoid contextLoad2() throws MessagingException {//一个复杂的邮件~MimeMessage mimeMessage = javaMailSender.createMimeMessage();//组装~MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);//正文helper.setSubject("基哥又见面了!!");helper.setText("<p style='color:red'>谢谢你看我的邮件~~~</p>",true);//附件helper.addAttachment("a.jpg",new File("F:\\JavaStudy\\sping-09-test\\src\\main\\resources\\static\\a.jpg"));helper.setTo("1393087444@qq.com");helper.setFrom("1393087444@qq.com");javaMailSender.send(mimeMessage);}
}
发送邮件