<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
1、QQ邮箱->设置->账户->POP3/SMTP服务:开启服务后会获得QQ的授权码.
2、设置->POP3/SMTP服务->勾选,之后保存,会弹出设置密码,这个密码和邮箱密码不一样哦
# JavaMailSender 邮件发送的配置
spring.mail.host=smtp.qq.com
spring.mail.username=用户qq邮箱
#QQ邮箱的授权码
spring.mail.password=qq邮箱授权码
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
spring.mail.default-encoding=UTF-8
spring.mail.host=smtp.163.com
spring.mail.username=用户163邮箱
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
spring.mail.default-encoding=UTF-8
spring:
#邮箱配置
mail:
host: smtp.qq.com
username: *******@qq.com
#QQ邮箱的授权码
password: 自己申请
default-encoding: UTF-8
properties:
mail:
smtp:
auth: true
starttls:
enable: true
required: true
#设置一个邮件发送人
lance:
mail:
sender: ********@qq.com
/**
* @author mhb(mhb)
* @function 发送邮件-封装接受者信息
*/
@Data
public class MailBean implements Serializable {
private String recipient; //邮件接收人
private String subject; //邮件主题
private String content; //邮件内容
}
@RunWith(SpringRunner.class)
@SpringBootTest(classes =QuartzSpringBootApplication.class )
public class client {
@Value("${spring.mail.username}")
private String MAIL_SENDER;
@Autowired
private JavaMailSender javaMailSender;
private static final Logger LOG = LoggerFactory.getLogger(Logger.class);
//发送简单的邮件
@Test
public void contextLoads() {
MailBean mailBean = new MailBean();
mailBean.setContent("测试");
mailBean.setRecipient("************@163.com");
mailBean.setSubject("测试邮件");
try {
SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
//邮件发送人
simpleMailMessage.setFrom(MAIL_SENDER);
//邮件接收人
simpleMailMessage.setTo(mailBean.getRecipient());
//邮件主题
simpleMailMessage.setSubject(mailBean.getSubject());
//邮件内容
simpleMailMessage.setText(mailBean.getContent());
javaMailSender.send(simpleMailMessage);
} catch (Exception e) {
LOG.error("邮件发送失败", e.getMessage());
}
}
}
//发送html格式的邮件
@Test
public void sendHTMLMail() {
MailBean mailBean = new MailBean();
mailBean.setContent("测试html格式的邮件");
mailBean.setRecipient("************@163.com");
mailBean.setSubject("测试html格式的邮件");
MimeMessage mimeMailMessage = null;
try {
mimeMailMessage = javaMailSender.createMimeMessage();
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMailMessage, true);
mimeMessageHelper.setFrom(MAIL_SENDER);
mimeMessageHelper.setTo(mailBean.getRecipient());
mimeMessageHelper.setSubject(mailBean.getSubject());
StringBuilder sb = new StringBuilder();
sb.append("SpirngBoot测试邮件HTML
")
.append("\"你是真的太棒了!
")
.append("右对齐
");
mimeMessageHelper.setText(sb.toString(), true);
javaMailSender.send(mimeMailMessage);
} catch (Exception e) {
LOG.error("邮件发送失败", e.getMessage());
}
}
//发送带附件格式的邮件
@Test
public void sendAttachmentMail() {
MailBean mailBean = new MailBean();
mailBean.setContent("测试附件格式的邮件");
mailBean.setRecipient("************@163.com");
mailBean.setSubject("测试附件格式的邮件");
MimeMessage mimeMailMessage = null;
try {
mimeMailMessage = javaMailSender.createMimeMessage();
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMailMessage, true);
mimeMessageHelper.setFrom(MAIL_SENDER);
mimeMessageHelper.setTo(mailBean.getRecipient());
mimeMessageHelper.setSubject(mailBean.getSubject());
mimeMessageHelper.setText(mailBean.getContent());
//文件路径
FileSystemResource file = new FileSystemResource(new File("src/main/resources/static/image/mail.jpg"));
mimeMessageHelper.addAttachment("mail.jpg", file);
javaMailSender.send(mimeMailMessage);
} catch (Exception e) {
LOG.error("邮件发送失败", e.getMessage());
}
}
1、pom中导入thymeleaf的包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2、在resorces/templates下创建emailTemplate.html
<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>Title</title>
</head>
<body>
您好,这是验证邮件,请点击下面的链接完成验证,<br/>
<a href="#" th:href="@{http://www.qinfeng.com/{id}(id=${id}) }">激活账号</a>
</body>
</html>
3、解析模板并发送
@Test
public void sendTemplateMail() {
//创建邮件正文
Context context = new Context();
context.setVariable("id", "006");
String emailContent = templateEngine.process("emailTemplate", context);
MimeMessage mimeMailMessage = null;
try {
mimeMailMessage = javaMailSender.createMimeMessage();
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMailMessage, true);
mimeMessageHelper.setFrom(MAIL_SENDER);
mimeMessageHelper.setTo("************@163.com");
mimeMessageHelper.setSubject("测试thymyleaf");
/* StringBuilder sb = new StringBuilder();
sb.append(emailContent);*/
mimeMessageHelper.setText(emailContent,true);
javaMailSender.send(mimeMailMessage);
} catch (Exception e) {
LOG.error("邮件发送失败", e.getMessage());
}
}