springBoot邮件推送

springBoot邮件推送

引用:https://blog.csdn.net/caychen/article/details/82887926

引用:https://www.cnblogs.com/mxwbq/p/10625612.html

1. 前言

发送邮件应该是网站的必备拓展功能之一,注册验证,忘记密码或者是给用户发送营销信息。正常我们会用JavaMail相关api来写发送邮件的相关代码,但现在springboot提供了一套更简易使用的封装。

2. Mail依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
    <version>${spring-boot-mail.version}</version>
</dependency>

3. appliction.yml 配置

spring:
    mail:
        default-encoding: utf-8
        from: 11111111@qq.com
        host: smtp.qq.com
        username: 11111111@qq.com
        password: wyjrfizyorgkbjcf
        properties:
            mail:
                smtp:
                    auth: true
                    port: 465
                    ssl:
                        enable: true
                    starttls:
                        enable: true
                        required: true
        protocol: smtp
注意:由于使用QQ邮箱的用户占多数,所以这里选择QQ邮箱作为测试。还有注意的是spring.mail.password这个值不是QQ邮箱的密码,而是QQ邮箱给第三方客户端邮箱生成的授权码。具体要登录QQ邮箱,点击设置,找到SMTP服务:开启POP3/SMTP服务,会生成授权码。

4. 模板推送

4.1 模板

<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8" />
    <title>邮件模板</title>
</head>

<body>
	您好,打开下面地址查看详情:<span th:text="${url}"></span>
  	<br>
  	<br>
	<span style="color:red">提示:不支持IE浏览器</span>
</body>
</html>

4.2 核心代码

package com.hollysys.tn.service;

import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

/**  
 * @Package      com.hollysys.tn.service
 * @Description: TODO
 * @author       LuShanyuan
 * @date         2019年8月29日 下午1:32:03
 * @version      V1.0  
 */
@Service
public class EmailService {

	@Autowired
    private JavaMailSender javaMailSender;

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

    @Autowired
    private TemplateEngine templateEngine;
	
    public String sendEmail(String toEmail, String name, String url) {
    	MimeMessage message = null;
        try {
            message = javaMailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            //设置自定义发件人昵称    
            helper.setFrom(new InternetAddress("系统邮箱"+" <"+ from +">")); 
            helper.setTo(toEmail); // 接收地址
            helper.setSubject(name); // 标题
            // 处理邮件模板
            Context context = new Context();
            context.setVariable("url", url);
            String template = templateEngine.process("emailTemplate", context);
            helper.setText(template, true);
            javaMailSender.send(message);
            return "发送成功";
        } catch (Exception e) {
            e.printStackTrace();
            return e.getMessage();
        }
    }
}

5. 测试结果

@Async
@Scheduled(cron = "0 0/2 * * * ? ")
public void emailAsync(){
	String toEmail = "88888888@qq.com";;
	String emailName = "单耗分析";
	String url = "http://localhost:85/material-usage?begin=2019-08-10&end=2019-10-10";
	emailService.sendEmail(toEmail, emailName, url);
}

测试的是定时每两分钟推送一次

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值