美文网首页SpringBoot
SpringBoot实现发送邮件

SpringBoot实现发送邮件

作者: WebGiser | 来源:发表于2019-12-01 17:12 被阅读0次

1、QQ邮箱开通POP3/SMTP服务或IMAP/SMTP服务

登录QQ邮箱,依次点击顶部的设置按钮和账户按钮,在下方找到POP3/SMTP服务,点击开启,按照引导步骤操作成功后会获取一个授权码


image.png

2、新建SpringBoot项目

image.png

3、pom.xml

<?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.2.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.wzf</groupId>
    <artifactId>email</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>email</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-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
            <version>2.2.0.RELEASE</version>
        </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>

4、application.properties

spring.mail.host = smtp.qq.com
spring.mail.port = 465
#QQ邮箱地址
spring.mail.username = 123@qq.com
#获取的QQ邮箱授权码
spring.mail.password = 123
spring.mail.default-encoding = UTF-8
spring.mail.properties.mail.smtp.socketFactory.class = javax.net.ssl.SSLSocketFactory
spring.mail.properties.mail.debug = true

5、MailService.java

package com.wzf.email;

import org.springframework.beans.factory.annotation.Autowired;
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.stereotype.Component;

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

@Component
public class MailService {
    @Autowired
    JavaMailSender javaMailSender;

    /**
     * 发送简单邮件
     * @param from 邮件发送者
     * @param to 收件人
     * @param cc 抄送人
     * @param subject 邮件主题
     * @param content 邮件内容
     */
    public void sendSimpleMail(String from, String to, String cc, String subject, String content){
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        simpleMailMessage.setFrom(from);
        simpleMailMessage.setTo(to);
        simpleMailMessage.setCc(cc);
        simpleMailMessage.setSubject(subject);
        simpleMailMessage.setText(content);
        javaMailSender.send(simpleMailMessage);
    }

    /**
     * 发送带附件的邮件
     * @param from 邮件发送者
     * @param to 收件人
     * @param subject 邮件主题
     * @param content 邮件内容
     * @param file 文件附件
     */
    public void sendAttachFileMail(String from, String to, String subject, String content, File file){
        try{
            MimeMessage message = javaMailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(from);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(content);
            helper.addAttachment(file.getName(), file);
            javaMailSender.send(message);
        }catch (MessagingException e){
            e.printStackTrace();
        }
    }

    /**
     *发送带图片的邮件
     * @param from 邮件发送者
     * @param to 收件人
     * @param subject 邮件主题
     * @param content 邮件内容
     * @param srcPath 图片资源路径
     * @param resIds 资源id
     */
    public void sendMailWithImg(String from, String to, String subject, String content, String[] srcPath, String[] resIds){
        if(srcPath.length != resIds.length){
            System.out.println("发送失败");
            return;
        }
        try{
            MimeMessage message = javaMailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(from);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(content, true);
            for(int i=0; i<srcPath.length; i++){
                FileSystemResource res = new FileSystemResource(new File(srcPath[i]));
                helper.addInline(resIds[i], res);
            }
            javaMailSender.send(message);
        }catch (MessagingException e){
            System.out.println("发送失败");
            e.printStackTrace();
        }
    }
}

6、测试

package com.wzf.email;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.File;

@SpringBootTest
class MailServiceTest {

    @Autowired
    MailService mailService;

    @Test
    void sendSimpleMail() {
        String from = "123@qq.com";
        String to = "111@qq.com";
        String cc = "222@qq.com";
        String subject = "简单邮件";
        String content = "测试简单邮件,可以忽略。";
        mailService.sendSimpleMail(from, to, cc, subject, content);
    }

    @Test
    void sendAttachFileMail(){
        String from = "123@qq.com";
        String to = "111@qq.com";
        String subject = "附件邮件";
        String content = "测试带附件的邮件,可以忽略。";
        File file = new File("E:\\数据.txt");
        mailService.sendAttachFileMail(from, to, subject, content, file);
    }

    @Test
    void sendMailWithImg(){
        String from = "123@qq.com";
        String to = "111@qq.com";
        String subject = "带图片的邮件";
        String content = "<h1>测试带图片的邮件,可以忽略。</h1>" +
                        "图片1:<div><img src='cid:p01'/></div>" +
                        "图片2:<div><img src='cid:p02'/></div>";
        String[] srcPath = new String[]{"E:\\temp\\locate.png","E:\\temp\\name.png"};
        String[] resIds = new String[]{"p01","p02"};
        mailService.sendMailWithImg(from, to, subject, content, srcPath, resIds);
    }
}

相关文章

网友评论

    本文标题:SpringBoot实现发送邮件

    本文链接:https://www.haomeiwen.com/subject/qkazwctx.html