点击我的邮箱
邮箱设置
点击账户
获得秘钥
application.properties 配置
#邮件发送配置
spring.mail.default-encoding=UTF-8
spring.mail.host=smtp.qq.com
spring.mail.username=使用的邮箱地址
spring.mail.password=获得的秘钥
#spring.mail.properties.mail.smtp.auth=true
#spring.mail.properties.mail.smtp.starttls.enable=true
#spring.mail.properties.mail.smtp.starttls.required=true
添加的依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
@Test
public void test02() throws MessagingException {
//创建一个复杂的消息邮件
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
//设置邮件
helper.setSubject("十六的月亮依旧圆!");
helper.setText("<b style='color:red'>抬头仰望星空!</b>",true);
//邮件接收者
helper.setTo("565656@qq.com");
//邮件发送者
helper.setFrom("898989@qq.com");
//上传文件
helper.addAttachment("1.jpg",new File("D:\\personage\\图片\\微信图片_20200228205838.jpg"));
helper.addAttachment("2.jpg",new File("D:\\personage\\图片\\微信图片_20200228205821.jpg"));
mailSender.send(mimeMessage);