springboot整合邮箱服务

你可以直接下载我的demo

链接:https://pan.baidu.com/s/1_vNIOcMVR-Iw16YPmLpQAw
提取码:6rb3

复制这段内容后打开百度网盘手机App,操作更方便哦
也可以跟着我搞一个

先开启pop服务:
https://jingyan.baidu.com/article/86fae3463d82ea3c49121a1d.html

项目结构图:
在这里插入图片描述1.导入依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.7.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.qf</groupId>
    <artifactId>my-springboot-email-demo</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <name>my-springboot-email-demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

2.配置application.yml

spring:
  mail:
    host: smtp.qq.com
    username: 1015910794@qq.com
    password: *********
    default-encoding: utf-8
image:
  path: D:\works\demo\my-springboot-email-demo\src\main\resources\static\03.jpg

3.在resources下新建static文件夹
下面存放一张图片

在resources下新建templates文件夹,下面新建email.html

在这里插入图片描述email.html:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

您注册的名字,<span th:text="${username}"></span>!点击以下内容,完成激活<a href='http://www.baidu.com'>http://www.baidu.com</a>
感谢您的关注!

</body>
</html>

4.编写启动类

package com.zh.my.springboot.email.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MySpringbootEmailDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(MySpringbootEmailDemoApplication.class, args);
    }

}

5.编写测试类

package com.zh.my.springboot.email.demo;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.test.context.junit4.SpringRunner;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

@RunWith(SpringRunner.class)
@SpringBootTest
public class MySpringbootEmailDemoApplicationTests {


    @Value("${image.path}")
    private String path;

    @Autowired
    private JavaMailSender sender;

    @Autowired
    private TemplateEngine templateEngine;


    @Test
    public void send01() {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("1015910794@qq.com");
        message.setTo("522441704@qq.com");
        message.setSubject("注册主题");
        message.setText("恭喜你,注册成功");
        sender.send(message);

    }
    @Test
    public void send02() throws MessagingException {
        MimeMessage mimeMessage = sender.createMimeMessage();
        MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true);
        message.setFrom("1015910794@qq.com");
        message.setTo("522441704@qq.com");
        message.setSubject("注册主题");
        message.setText("恭喜你,注册成功,点击链接完成激活<a href='http://www.baidu.com'>激活</a>",true);
        sender.send(mimeMessage);

    }
    @Test
    public void send03() throws MessagingException {
        MimeMessage mimeMessage = sender.createMimeMessage();
        MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true);
        message.setFrom("1015910794@qq.com");
        message.setTo("522441704@qq.com");
        message.setSubject("注册主题");
        message.setText("恭喜你,注册成功,点击链接完成激活<a href='http://www.baidu.com'>激活</a>",true);



        FileSystemResource fileSystemResource = new FileSystemResource(path);
        String fileName = path.substring(path.lastIndexOf(File.separator)+1);
        message.addAttachment(fileName,fileSystemResource);

        sender.send(mimeMessage);

    }
    @Test
    public void send04() throws MessagingException {
        MimeMessage mimeMessage = sender.createMimeMessage();
        MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true);
        message.setFrom("1015910794@qq.com");
        message.setTo("522441704@qq.com");

        //读取模板内容
        Context context = new Context();
        context.setVariable("username","zhangsan");
        String info = templateEngine.process("email.html", context);
        message.setSubject("注册主题");
        message.setText(info,true);
        sender.send(mimeMessage);

    }

}

ok,跑测试类测试吧

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值