250_邮件任务


创建项目

image.png
image.png

导入依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<?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 https://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.6.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.qing</groupId>
    <artifactId>springboot-10-task</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot-10-task</name>
    <description>springboot-10-task</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <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>

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

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

</project>

源码查看

image.png
image.png
image.png

开启POP3/SMTP服务,获取授权码

image.png
image.png
image.png

配置参数

image.png

spring.mail.username=123456@qq.com
spring.mail.password=abdckljdsfklsdf
spring.mail.host=smtp.qq.com
# 开启加密验证,qq特有,其他邮箱可能没有
spring.mail.properties.mail.smtp.ssl.enable=true

使用测试

发送简单邮件

image.png

package com.qing;

import org.junit.jupiter.api.Test;
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;

@SpringBootTest
class Springboot10TaskApplicationTests {

    @Autowired
    private JavaMailSenderImpl mailSender;

    @Test
    void contextLoads() {
        // 发送简单邮件
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        mailMessage.setFrom("send@qq.com");
        mailMessage.setTo("receive@qq.com");
        mailMessage.setSubject("我是主题");
        mailMessage.setText("我是正文");
        mailSender.send(mailMessage);
    }

}

发送成功
image.png

发送复杂邮件

package com.qing;

import org.junit.jupiter.api.Test;
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 javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

@SpringBootTest
class Springboot10TaskApplicationTests {

    @Autowired
    private JavaMailSenderImpl mailSender;

    @Test
    void contextLoads() {
        // 发送简单邮件
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        mailMessage.setFrom("send@qq.com");
        mailMessage.setTo("receive@qq.com");
        mailMessage.setSubject("我是主题");
        mailMessage.setText("我是正文");
        mailSender.send(mailMessage);
    }

    @Test
    void contextLoads2() throws MessagingException {
        // 发送复杂邮件
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        // 组装
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
        helper.setFrom("send@qq.com");
        helper.setTo("receive@qq.com");
        helper.setSubject("我是主题");
        helper.setText("<p style='color:red'>我是正文</p>", true);
        // 附件
        helper.addAttachment("1.jpg", new File("C:\\Users\\Public\\Pictures\\Sample Pictures\\1.jpg"));
        helper.addAttachment("2.jpg", new File("C:\\Users\\Public\\Pictures\\Sample Pictures\\2.jpg"));
        mailSender.send(mimeMessage);
    }

}

发送成功
image.png

posted @ 2022-02-09 17:10  清风(学习-踏实)  阅读(41)  评论(0)    收藏  举报