<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
/dependency>
spring:
mail:
username: 2437766749@qq.com
password: 邮箱校验码
host: smtp.qq.com
properties:
mail:
smtp:
ssl:
enable: true
package com.shi.snyc;
import org.junit.Test;
import org.junit.runner.RunWith;
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 org.springframework.test.context.junit4.SpringRunner;
import javax.mail.internet.MimeMessage;
import java.io.File;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SnycApplicationTests {
@Autowired
JavaMailSenderImpl mailSender;
@Test
public void contextLoads() {
//简单格式传输邮件
SimpleMailMessage message = new SimpleMailMessage();
message.setSubject("设置标题");
message.setText("设置内容");
message.setTo("307137783@qq.com");
message.setFrom("2437766749@qq.com");
mailSender.send(message);
}
@Test
public void test2() throws Exception{
//创建一个复杂的消息邮件
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
helper.setSubject("设置标题");
helper.setText("<b style='color:red'>设置内容</b>",true);//使用html
helper.setTo("307137783@qq.com");
helper.setFrom("2437766749@qq.com");
helper.addAttachment("1.jpg",new File("C:\\Users\\low\\Desktop\\1.png"));
mailSender.send(mimeMessage);
}
}