SpringBoot发送邮件,发送二维码,邮箱验证,图片,Html,附件等

1、准备工作

以刚刚创建的SpringBoot项目为主:
以QQ邮箱为例:(其实什么邮箱都差不多)

1.进入qq邮箱设置

在这里插入图片描述

2.找到账户

在这里插入图片描述

3.往下滑,找到pop3服务,点击开启,需要验证,得到授权码

在这里插入图片描述
在这里插入图片描述

2、配置代码

1导入依赖

在这里插入图片描述

		<!-- 发送邮箱所需依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>
2.配置配置文件,application.properties(重点)

在这里插入图片描述

#host 代表使用什么邮箱, QQ:smtp.qq.com  163:smtp.163.com
#default-encoding 字符集 默认utf-8
#username 邮箱账号
#password 第一步的授权码
spring.mail.host=smtp.qq.com
spring.mail.default-encoding=utf-8
spring.mail.username=xxxx
spring.mail.password=xxxx

username是填写邮箱账号
password不是填写密码,是填写第一步的授权码
在这里插入图片描述

3.创建发送邮件实现类接口(重点)

复制进去导入包即可
在这里插入图片描述

package com.lj.demo.mail;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.MailException;
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 MailServiceImpl {
    @Autowired
    private JavaMailSender mailSender;

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

    /**
     * 发送普通文本邮件
     */
    public void sendSimpleMail(String to, String subject, String content) throws MailException {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from); // 邮件发送者
        message.setTo(to); // 邮件接受者
        message.setSubject(subject); // 主题
        message.setText(content); // 内容

        mailSender.send(message);
    }

    /**
     * 发送带图片邮件邮件
     */
    public void sendInlineResourceMail(String to, String subject, String content, String rscPath, String rscId) throws MessagingException {
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom(from);
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(content, true);
        File file = new File(rscPath);
        FileSystemResource res = new FileSystemResource(file);
        helper.addInline(rscId, res);

        mailSender.send(message);
    }

    /**
     * 发送HTML邮件
     */
    public void sendHtmlMail(String to, String subject, String content) throws MessagingException {
        MimeMessage message = mailSender.createMimeMessage();
        //true 表⽰示需要创建⼀一个 multipart message
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom(from);
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(content, true);
        mailSender.send(message);

    }

    /**
     * 发送带附件的邮件
     */
    public void sendAttachmentsMail(String to, String subject, String content, String filePath) throws MessagingException {
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom(from);
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(content, true);
        FileSystemResource file = new FileSystemResource(new File(filePath));
        String fileName = file.getFilename();
        helper.addAttachment(fileName, file);

        mailSender.send(message);

    }

}

接下来我们测试,在测试类中进行测试,实际开发模仿我的示例

在这里插入图片描述

package com.lj.demo;

import com.lj.demo.mail.MailServiceImpl;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mail.MailException;

import javax.mail.MessagingException;

@SpringBootTest
class DemoApplicationTests {

    @Autowired
    MailServiceImpl mailService;

    /**
     * 普通文本邮件测试
     *
     * @throws Exception
     */
    @Test
    public void test() throws Exception {
        String to = "邮件名@qq.com";
        String subject = "主题";
        String content = "内容";
        try {
            mailService.sendSimpleMail(to, subject, content);
            System.out.println("成功了");

        } catch (MailException e) {
            System.out.println("失败了");
            e.printStackTrace();
        }

    }


    /**
     * 带图片的邮件测试
     */
    @Test
    public void test1() {
        String to = "邮件名@163.com";
        String subject = "主题";
        String rscId = "img";
        String content = "<html><body><img width='250px' src=\'cid:" + rscId + "\'></body></html>";
        // 此处为电脑系统路径
        String imgPath = "C:\\Users\\Administrator\\Desktop\\0f819bedc81e4447a79ebc0207a5704e.png";
        try {
            mailService.sendInlineResourceMail(to, subject, content, imgPath, rscId);
            System.out.println("成功了");
        } catch (MessagingException e) {
            System.out.println("失败了");
            e.printStackTrace();
        }
    }


    /**
     * 带HTML邮件测试
     */
    @Test
    public void test2() {
        String to = "邮件名@qq.com";
        String subject = "主题";
        String content = "<html><head></head><body><h3>哈哈,什么都没有</h3></body></html>";
        try {
            mailService.sendHtmlMail(to, subject, content);
            System.out.println("成功了");
        } catch (MessagingException e) {
            System.out.println("失败了");
            e.printStackTrace();
        }
    }

    /**
     * 带附件的邮件测试
     */
    @Test
    public void test3() {
        String to = "邮件名@163.com";
        String subject = "附件的邮件";
        String content = "内容";
        String imgPath = "C:\\Users\\Administrator\\Desktop\\0f819bedc81e4447a79ebc0207a5704e.png";
        try {
            mailService.sendAttachmentsMail(to, subject, content, imgPath);
            System.out.println("成功了");
        } catch (MessagingException e) {
            System.out.println("失败了");
            e.printStackTrace();
        }
    }


}

填写好接收的邮箱,全部测试
在这里插入图片描述
完成

有可能您还需要

SpringBoot发送短信验证码:https://blog.csdn.net/weixin_43122090/article/details/103555621
Java代码生成二维码:https://blog.csdn.net/weixin_43122090/article/details/103524097
如果需要邮箱发送二维码,可参考我Java代码生成二维码配合此文章可实现
在这里插入图片描述

发布了36 篇原创文章 · 获赞 36 · 访问量 9907

猜你喜欢

转载自blog.csdn.net/weixin_43122090/article/details/103560516