spring boot发送邮件

spring boot发送邮件

1.准备工作

获取发送邮箱授权码
spring boot发送邮件_第1张图片
生成的授权码如下:
spring boot发送邮件_第2张图片

引入依赖

<!-- mail -->
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-mail</artifactId>
</dependency>

基本配置

spring.mail.host=smtp.qq.com
spring.mail.username=644*****2@qq.com
# 授权码
spring.mail.password=aphqdxdsniaobcga
spring.mail.port=25
spring.mail.properties.mail.smtp.auth=true 
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.stattls.required=true

2.发送简单邮件

package com.mail;

import com.DemoApplicationTests;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;

@Slf4j
public class MailTest extends DemoApplicationTests {

    @Value("${spring.mail.username}")
    private  String fromAddress;

    @Autowired
    private JavaMailSender javaMailSender;

    //这里为了方便接收方和发送方设置是同一个
    @Value("${spring.mail.username}")
    private  String  toAddress;
    @Value("${spring.mail.username}")
    private String   cc=fromAddress;

    @Test
    public void sendSimpleMail(){
        String subject="hello world";
        String content="测试简单邮件发送";
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(fromAddress); //发件人
        message.setTo(toAddress); //接受人
        message.setSubject(subject); //主题
        message.setText(content);  //内容
        message.setCc(cc);//抄送对象
        javaMailSender.send(message);
        log.info("{}->{}发送成功",fromAddress,toAddress);

    }

}

3.发送HTML形式的邮件

@Test
public void sendHtmlMail() throws MessagingException {
    String subject="hello world";
    String content="

测试html邮件发送

"
; boolean isHtml=true; MimeMessage mimeMessage = javaMailSender.createMimeMessage(); MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage); mimeMessageHelper.setFrom(fromAddress); mimeMessageHelper.setTo(toAddress); //接受人 mimeMessageHelper.setSubject(subject); //设置主题 mimeMessageHelper.setText(content,isHtml); //发送内容 mimeMessageHelper.setCc(cc);//抄送对象 javaMailSender.send(mimeMessage); log.info("{}->{}发送成功",fromAddress,toAddress); }

4.发送带有附件的邮件

  @Test
    public void sendHtmlAddAttachmentMail() throws MessagingException {
    String subject="hello world";
    String content="

测试附件邮件发送

"
; boolean isHtml=true; boolean mulipart=true; File file=new File("C:\\Users\\TP\\Desktop\\IMG_20180905_213354_HHT.jpg"); MimeMessage mimeMessage = javaMailSender.createMimeMessage(); MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage,mulipart);//重点在于这个构造方法 mimeMessageHelper.setFrom(fromAddress); mimeMessageHelper.setTo(toAddress); //接受人 mimeMessageHelper.setSubject(subject); //设置主题 mimeMessageHelper.setText(content,isHtml); //发送内容 mimeMessageHelper.setCc(cc);//抄送对象 mimeMessageHelper.addAttachment("test",file); javaMailSender.send(mimeMessage); log.info("{}->{}发送成功",fromAddress,toAddress); }

5.发送模版邮件

这里使用thymelef,引入依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

定义模板


<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>Titletitle>
head>
<body>
    <h1>测试模版邮件的发送h1>
    <span>Hello,span><span th:text="${user}">span><br>
    发送邮件的时间为<span th:text="${#dates.format(date, 'yyyy-MM-dd')}">span>
body>
html>

发送邮件

@Autowired
private TemplateEngine templateEngine;
@Test
public void sendModelMail() throws MessagingException {
    String subject="hello world";
    boolean isHtml=true;
    Context context = new Context();//定义一个Thymelf上下文
    context.setVariable("user","GAY");
    context.setVariable("date",new Date());
    //通过模版引擎将数据渲染到指定模板中
    String content = templateEngine.process("mailTemple", context);
    MimeMessage mimeMessage = javaMailSender.createMimeMessage();
    MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage);
    mimeMessageHelper.setFrom(fromAddress);
    mimeMessageHelper.setTo(toAddress); //接受人
    mimeMessageHelper.setSubject(subject); //设置主题
    mimeMessageHelper.setText(content,isHtml);  //发送内容
    mimeMessageHelper.setCc(cc);//抄送对象
    javaMailSender.send(mimeMessage);
    log.info("{}->{}发送成功",fromAddress,toAddress);
    }

正文效果如下
spring boot发送邮件_第3张图片

你可能感兴趣的:(笔记,spring,boot,mail)