spring boot 发送邮箱

spring boot发送邮件

一.简单文本邮件

1.引入相关jar包

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

        <!--引入spring boot mail的jar包-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>

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

2.配置邮箱参数

在项目的application.properties配置文件中配置邮箱信息

# 配置邮箱信息
spring.mail.host=smtp.qq.com
spring.mail.username=1260580035@qq.com
# 配置的是授权码,不是登录密码
spring.mail.password=nejvdivlanmobaef
spring.mail.default-encoding=utf-8

3.封装SimpleMailMessage

@Service
public class MailService {

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

    public void sayHello() {
        System.out.println("Hello World!");
    }

    /**
     * 发送简单邮件
     * @param to 接收人邮箱地址
     * @param subject 邮件主题
     * @param context 邮件内容
     */
    public void sendSimpleMail(String to, String subject, String context) {
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        simpleMailMessage.setTo(to);
        simpleMailMessage.setSubject(subject);
        simpleMailMessage.setText(context);
        simpleMailMessage.setFrom(from);
        
    }
}

4.JavaMailSender进行发送

@Service
public class MailService {

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

    /**
     * 邮件发送类
     */
    @Resource
    private JavaMailSender javaMailSender;

    public void sayHello() {
        System.out.println("Hello World!");
    }

    /**
     * 发送简单邮件
     * @param to 接收人邮箱地址
     * @param subject 邮件主题
     * @param context 邮件内容
     */
    public void sendSimpleMail(String to, String subject, String context) {
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        simpleMailMessage.setTo(to);
        simpleMailMessage.setSubject(subject);
        simpleMailMessage.setText(context);
        simpleMailMessage.setFrom(from);

        //发送邮件
        javaMailSender.send(simpleMailMessage);
    }
}

5.简单邮件发送测试

@SpringBootTest
class MailServiceTest {

    @Resource
    MailService mailService;

    @Test
    void sayHello() {
        mailService.sayHello();
    }

    @Test
    void sendSimpleMail() {
        mailService.sendSimpleMail("1260580035@qq.com", "这个我的第一份邮件", 
                "大家好啊,这个邮箱内容,好好加油哦!");
    }
}

发送成功,打开邮箱查看
在这里插入图片描述

二.发送HTML邮件

编写sendHtmlMail方法

/**
     * 发送HTML邮件
     * @param to 接收人邮箱地址
     * @param subject 邮件主题
     * @param context 邮件内容
     */
    public void sendHtmlMail(String to, String subject, String context) throws MessagingException {
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom(from);
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(context, true);

        mailSender.send(message);
    }

编写测试方法

@Test
    void sendHtmlMail() throws MessagingException {
        String content = "<html>\n" +
                "<body>\n" +
                    "<h3>这是一份html邮件</h3>\n" +
                    "<a href='http://www.baidu.com'>百度一下</a>\n" +
                "</body>\n" +
                "</html>";
        mailService.sendHtmlMail("1260580035@qq.com", "这是一份html邮件", content);
    }	

发送成功,邮箱查看结果如下
在这里插入图片描述

三.发送带有附件邮件

编写sendAttachmentsMail方法

/**
     * 发送附件邮件
     * @param to 接收人邮箱地址
     * @param subject 邮件主题
     * @param context 邮件内容
     * @param filePath 附件文件路径
     */
    public void sendAttachmentsMail(String to, String subject, String context, 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(context);

        /*
            给发送邮件添加附件
         */
        FileSystemResource file = new FileSystemResource(new File(filePath));
        String filename = file.getFilename();
        helper.addAttachment(filename, file);
        //helper.addAttachment(filename, file);  //如需添加多个附件,可以多次调用addAttachment()

        mailSender.send(message);
    }

编写测试方法

@Test
    void sendAttachmentsMail() throws MessagingException {
        String filePath = "D:\\01.jpg";
        mailService.sendAttachmentsMail("1260580035@qq.com", "这个一封带附件的邮件",
                "这是内容,带有附件", filePath);
    }

发送成功,查看邮箱如下,带有附件
在这里插入图片描述

四.发送内嵌资源(图片)邮件

编写**sendInlineMail**方法

/**
     * 发送资源邮件
     * @param to 接收人邮箱地址
     * @param subject 邮件主题
     * @param context 邮件内容
     * @param rscPath 资源路径
     * @param rscId 资源id
     * @throws MessagingException
     */
    public void sendInlineMail(String to, String subject, String context, 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(context, true);

        /*
            给发送邮件添加资源
         */
        FileSystemResource res = new FileSystemResource(new File(rscPath));
        helper.addInline(rscId, res);

        mailSender.send(message);
    }

编写测试方法

@Test
    void sendInlineMail() throws MessagingException {
        String rscPath = "D:\\01.jpg";
        String rscId = "img001";
        String content = "<html><body><img src=\'cid:" + rscId + "\'></img>" + "</body></html>";
        mailService.sendInlineMail("1260580035@qq.com", "这个一封带资源的邮件",
                content, rscPath, rscId);
    }

发送成功,查看结果如下
在这里插入图片描述

**如果是qq邮箱,图片显示不出来,操作如下:

1,标记邮件为未读
在这里插入图片描述

2,点击邮箱首页,打开未读邮件
在这里插入图片描述

3,点击全文,即可查看
在这里插入图片描述

4,查看图片
在这里插入图片描述

五.邮件模板

1,引入模板jar包

在pom.xml文件中引入依赖包

<!--引入spring boot邮件模板jar包-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

2,配置html模板

在资源目录下templates下新建邮件模板emailTemplate.html文件

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8" />
    <title>邮件模板</title>
</head>
<body>
    这是一封注册验证邮件,请点击下面的链接完成注册,感谢您的支持!<br/>
    <a href="#" th:href="@{http://www.ityouknow.com/register/{id}(id=${id}})">激活账号</a>
</body>
</html>

3,编写测试方法

	/**
     * 注入邮件模板引擎
     */
    @Resource
    TemplateEngine templateEngine;

	/**
     * 模板邮件测试
     * @throws MessagingException
     */
	@Test
    void sendTemplateMail() throws MessagingException {
        Context context = new Context();
        //属性id对应邮件模板中的属性
        context.setVariable("id", "006");

        String emailContext = templateEngine.process("emailTemplate", context);
        mailService.sendHtmlMail("1260580035@qq.com", "这个一封模板邮件", emailContext);
    }

查看邮箱,
在这里插入图片描述

点击激活账号链接,跳转页面,可以看到id属性值已经传过来了
在这里插入图片描述

六,代码地址

https://gitee.com/sheng_git/springboot-mail.git

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值