spring boot发送邮件,使用的velocity模板(html)

本文介绍了如何在Spring Boot应用中利用Velocity模板发送HTML邮件。首先,添加相关依赖并配置application.properties,如163邮箱的客户端授权密码。然后,展示了QQ邮箱的配置差异。接着,创建邮件发送类,并提供了邮件模板(mail.vm)和控制器的编写示例。
摘要由CSDN通过智能技术生成

添加依赖:

  <!--邮件依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>

        <!--velocity模板-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-velocity</artifactId>
            <version>1.4.7.RELEASE</version>
        </dependency>

application.properties的配置:注意,我使用的是163邮箱发送邮件,如果使用qq邮箱发送,配置会稍有不同

# JavaMailSender 邮件发送的配置
spring.mail.host=smtp.163.com
#邮件发送者
spring.mail.username=xxx@163.com
#邮件的接收者
spring.mail.tomail=xxx@qq.com
#设置的是客户端授权密码,不是邮箱的登录密码
spring.mail.password=xxx
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true

qq邮箱发送邮件发的配置:

######qq邮箱########
spring.mail.host=smtp.qq.com
spring.mail.username=******@qq.com
#QQ邮箱授权码
spring.mail.password=xuojxtkdojvzbhjj
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true

注意密码的配置,设置的是客户端授权密码,不是邮箱的登录密码,以163邮箱为例,需要保证如下的设置

登录163邮箱,修改如下的操作

需要开启如下的授权码,在properties中配置的密码就是这个开启的授权码,不能是密码

 

编写邮件发送类

package com.tencent.springbootsendmail.service.impl;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tencent.springbootsendmail.model.ProductInfo;
import com.tencent.springbootsendmail.service.SendMailService;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;
import org.apache.velocity.tools.generic.NumberTool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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 javax.annotation.PostConstruct
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值