SpringBoot发送激活邮件

最近开发了一个社区网站,网站地址:https://developer.ppdxzz.org.cn, 其中在注册账号的时候用到了邮箱验证,特此记录一下,记录知识,同时方便大家查阅参考。

首先分析一下大体做法,用户使用邮箱注册时,需要向用户邮箱发送一个验证链接,用户点击链接对邮箱进行激活的这样一个操作,由此来完成邮箱的激活。

这里后台向用户发送的链接需要携带一个唯一值用来对用户进行标识,我这里选择用UUID,因为生成唯一也比较好管理,一般的话激活链接是有时效性的,就是说不是一直有效,比如设置24小时激活有效,超过24小时就得重新注册激活。这个可以使用redis来控制,通过UUID键是否存在于redis中来判断是否在有效期,存在即可激活,激活成功后删除这个UUID键,激活失败则是链接时效期可能过了24小时或者其他原因。

1.导入依赖
//发送邮箱依赖
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
</dependency>
//redis依赖
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
//thymeleaf模板引擎依赖,注册邮箱发送以及激活模板
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
//hutool工具包
<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-core</artifactId>
    <version>5.1.2</version>
</dependency>

2.邮件信息配置application.properties
#这里我使用163邮箱主机,其他同理
spring.mail.host=smtp.163.com
spring.mail.default-encoding=UTF-8
# 发送账号,用户名,也就是使用哪个邮箱发送
spring.mail.username=xxx@163.com
# 密码,这里密码一般指的是授权密码,开启POP3/SMTP服务
spring.mail.password=xxxxxx
# 邮箱验证链接前缀,到时候拼接uuid
my-config-mail.ip=http://127.0.0.1:8088/verifyMail/

3.发送邮件逻辑
@Autowired
private RedisTemplate<String, String> redisTemplate;

@Autowired
private JavaMailSender javaMailSender;

@Autowired
private TemplateEngine templateEngine;

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

@Value("${my-config-mail.ip}")
private String prefixIp;

//生成用户唯一标识uuid,redis设置过期时间24小时
String uuid = IdUtil.simpleUUID();
redisTemplate.opsForValue().set(uuid,request.getEmail(),24,TimeUnit.HOURS);
try {
    //获取邮箱接受者,根据你自己逻辑获取
    String email = request.getEmail();
    Context context = new Context();
    context.setVariable("email",email);
    context.setVariable("activeUrl",prefixIp+uuid);
    //调用邮件发送模板页面,渲染email接收者、activeUrl激活链接
    String emailTemplate = templateEngine.process("emailTemplate", context);
    //创建可以HTML发送的邮件
    MimeMessage message = javaMailSender.createMimeMessage();
    MimeMessageHelper helper = new MimeMessageHelper(message, true);
    helper.setFrom("Cloud云社区 <"&
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是使用SpringBoot发送邮件的示例代码,其中包括了如何发送带附件的邮件和如何开启TLS验证: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.mail.MailProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.core.io.FileSystemResource; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.MimeMessageHelper; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import java.io.File; @SpringBootApplication @EnableConfigurationProperties(MailProperties.class) public class MailApplication { @Autowired private JavaMailSender mailSender; @Autowired private MailProperties mailProperties; public static void main(String[] args) { SpringApplication.run(MailApplication.class, args); } public void sendMailWithAttachment() throws MessagingException { MimeMessage message = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(mailProperties.getUsername()); helper.setTo("recipient@example.com"); helper.setSubject("Test email with attachment"); // 添加附件 FileSystemResource file = new FileSystemResource(new File("attachment.txt")); helper.addAttachment("attachment.txt", file); // 发送邮件 mailSender.send(message); } } ``` 在application.properties文件中添加以下配置: ``` spring.mail.username=xxxxxxx@outlook.com spring.mail.password=xxxxxxxxx spring.mail.port=587 spring.mail.host=smtp-mail.outlook.com spring.mail.properties.mail.smtp.starttls.required=true ``` 注意:在使用Outlook发送邮件时,需要开启TLS验证,否则会显示匿名用户无法通过验证。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

皮皮的小猪仔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值