Spring Boot - 实现邮件发送

1. 简介

在项目开发中,用户注册、邮箱激活以及密码找回等功能的应用十分广泛。这些功能不仅提高了用户体验,还增强了系统的安全性。

帐号激活是用户注册流程中的一个重要环节。用户在注册完成后,系统会发送一封包含激活链接的邮件到用户的注册邮箱。用户点击链接后,账户状态会从未激活变为已激活。邮箱激活功能的意义在于:

  • 防止恶意注册:通过邮箱激活,系统可以筛选出真实用户,防止大量恶意注册和垃圾账号的产生。

  • 验证用户邮箱的有效性:确保用户提供的邮箱地址是真实有效的,方便后续的邮件通知和服务。

  • 提高用户信任度:用户通过激活流程,对系统的信任度会提高,更愿意使用系统提供的服务。

密码找回功能允许用户在忘记密码时,通过一定的验证流程重新设置密码。这一功能的意义在于:

  • 提高用户体验:当用户忘记密码时,可以通过密码找回功能快速恢复。

  • 增强系统安全性:密码找回功能通常包含验证码验证、邮箱验证等安全机制,确保只有真实的用户才能重置密码,防止密码被恶意篡改或盗用。

2. 实战案例

通过QQ邮件服务器进行邮件的发送,首先我们要在QQ邮箱中进行配置。

2.1 QQ邮箱配置

按如下流程进行设置

第一步:

第二步:

第三步:

开启服务功能,然后生成登录授权码

以上是在QQ邮箱中进行设置,设置完成后我们才可以在项目中使用QQ邮箱进行邮件的发送。

2.2 项目配置

引入依赖

java 复制代码
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-mail</artifactId>
</dependency>

邮件配置文件

yaml 复制代码
spring:
  mail:
    host: smtp.qq.com
    username: [email protected]
    #这里就是你在QQ邮箱中配置生成的登录授权码,这里可不是你QQ密码哦
    password: xxxoooxxxooo
    protocol: smtp
    port: 587
    #其它配置(下面超时的设置) 
    properties:
      '[mail.smtp.connectiontimeout]': 5000
      '[mail.smtp.timeout]': 3000
      '[mail.smtp.writetimeout]': 5000

2.3 发送模版邮件

配置模版

bash 复制代码
@Bean
SimpleMailMessage mailMessage(MailProperties mailProperties) {
  SimpleMailMessage mailMessage = new SimpleMailMessage() ;
  mailMessage.setSubject("帐号激活") ;
  mailMessage.setFrom(mailProperties.getUsername()) ;
  return mailMessage ;
}

发送模版邮件

bash 复制代码
// 该类SpringBoot已经帮我们自动配置好了,之间注入使用即可
@Resource
private JavaMailSender mailSender ;
@Resource
private SimpleMailMessage templateMessage ;

@Async
public void accountActivation(User user) {
  SimpleMailMessage msg = new SimpleMailMessage(this.templateMessage);
  msg.setTo(user.getMail()) ;
  msg.setText(
    "感谢 " + user.getName()
      + "注册,请点击此处激活帐号") ;
  try {
    this.mailSender.send(msg) ;
  } catch (MailException ex) {
    System.err.printf("邮件发送错误:%s%n", ex.getMessage()) ; 
  }
}

注意:邮件发送我们最好是异步发送,不要因为邮件发送而影响我们系统整体的性能。

测试发送

bash 复制代码
@Test
public void testAccountActivation() throws Exception {
  User user = new User() ;
  user.setMail("[email protected]") ;
  user.setName("Pack") ;
  user.setUsername("jaapack") ;
  this.mailService.accountActivation(user) ;
  System.in.read() ;
}

2.4 发送附件功能

bash 复制代码
@Async
public void sendAttachments(User user) throws Exception {
  MimeMessage message = this.mailSender.createMimeMessage();
  MimeMessageHelper helper = new MimeMessageHelper(message, true);
  
  helper.setFrom(mailProperties.getUsername()) ;
  helper.setTo(user.getMail()) ;
  helper.setSubject("附件") ;
  helper.setText(
    "感谢 " + user.getName()
      + ", 请接收附件") ;
  FileSystemResource file = new FileSystemResource(new File("f:/附件.pdf"));
  helper.addAttachment("附件.pdf", file) ;
  this.mailSender.send(message) ;
}

测试发送附件

2.5 内联资源

内联资源就是我们的邮件内容包含图片等HTML相关内容。

bash 复制代码
@Async
public void sendInlineResource(User user) throws Exception {
  MimeMessage message = this.mailSender.createMimeMessage();
  MimeMessageHelper helper = new MimeMessageHelper(message, true);
  
  helper.setFrom(mailProperties.getUsername()) ;
  helper.setTo(user.getMail()) ;
  helper.setSubject("我是内联资源") ;
  helper.setText(
    "感谢 <b>" + user.getName()
      + "</b>, 关注<br/><img src='cid:id_un_123'/>", true) ;
  FileSystemResource res = new FileSystemResource(new File("d:/images/1.png")) ;
  helper.addInline("id_un_123", res) ;
  this.mailSender.send(message) ;
}

注意:这里的img标签cid:id_un_123这里的id_un_123就是下面addInline指定的值,同时setText第二个参数需要设置为true,已告知是html内容。

这里还有一点非常重要,请确保先添加文本,然后再添加资源。

相关推荐
Uranus^1 小时前
使用Spring Boot与Spring Security构建安全的RESTful API
java·spring boot·spring security·jwt·restful api
蚂蚁在飞-1 小时前
Golang基础知识—cond
开发语言·后端·golang
Coding宇航员2 小时前
玩转 AI · 思考过程可视化
spring boot·ai·可视化
程序员爱钓鱼7 小时前
匿名函数与闭包(Anonymous Functions and Closures)-《Go语言实战指南》原创
后端·golang
Q_Q19632884758 小时前
python的漫画网站管理系统
开发语言·spring boot·python·django·flask·node.js·php
言之。8 小时前
Go 语言中接口类型转换为具体类型
开发语言·后端·golang
MaCa .BaKa8 小时前
38-日语学习小程序
java·vue.js·spring boot·学习·mysql·小程序·maven
diving deep9 小时前
XML简要介绍
xml·java·后端
Uranus^9 小时前
深入解析Spring Boot与Redis集成:高效缓存实践
java·spring boot·redis·缓存
椰椰椰耶9 小时前
【RabbitMQ】整合 SpringBoot,实现工作队列、发布/订阅、路由和通配符模式
spring boot·rabbitmq·java-rabbitmq