Spring Boot 实现QQ邮箱发送验证码 自用

蔚落 2023-01-02 07:28 327阅读 0赞

Spring Boot的Starter模块中也为此提供了自动化配置

下面通过实例看看如何在Spring Boot中使用JavaMailSender发送邮件。

快速入门

在Spring Boot的工程中的pom.xml中引入spring-boot-starter-mail依赖:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>com.cicoding</groupId>
  5. <artifactId>cicoding-mail</artifactId>
  6. <version>1.0.0</version>
  7. <packaging>jar</packaging>
  8. <name>cicoding-mail</name>
  9. <description>Spring Boot project</description>
  10. <parent>
  11. <groupId>org.springframework.boot</groupId>
  12. <artifactId>spring-boot-starter-parent</artifactId>
  13. <version>1.3.2.RELEASE</version>
  14. <relativePath/> <!-- lookup parent from repository -->
  15. </parent>
  16. <properties>
  17. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  18. <java.version>1.8</java.version>
  19. </properties>
  20. <dependencies>
  21. <dependency>
  22. <groupId>org.springframework.boot</groupId>
  23. <artifactId>spring-boot-starter</artifactId>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.springframework.boot</groupId>
  27. <artifactId>spring-boot-starter-mail</artifactId>
  28. </dependency>
  29. <dependency>
  30. <groupId>org.springframework.boot</groupId>
  31. <artifactId>spring-boot-starter-velocity</artifactId>
  32. </dependency>
  33. <dependency>
  34. <groupId>org.springframework.boot</groupId>
  35. <artifactId>spring-boot-starter-test</artifactId>
  36. <scope>test</scope>
  37. </dependency>
  38. </dependencies>
  39. <build>
  40. <plugins>
  41. <plugin>
  42. <groupId>org.springframework.boot</groupId>
  43. <artifactId>spring-boot-maven-plugin</artifactId>
  44. </plugin>
  45. </plugins>
  46. </build>
  47. </project>

如其他自动化配置模块一样,在完成了依赖引入之后,只需要在application.properties中配置相应的属性内容。

下面我们以QQ邮箱为例,在application.properties中加入如下配置(注意替换自己的用户名和密码):

一种模版配置;

  1. spring.mail.host=smtp.qq.com
  2. spring.mail.username=用户名
  3. spring.mail.password=这里填邮箱的授权码
  4. spring.mail.properties.mail.smtp.auth=true
  5. spring.mail.properties.mail.smtp.starttls.enable=true
  6. spring.mail.properties.mail.smtp.starttls.required=true
  7. 另一种模版配置;
  8. spring.mail.host=smtp.qq.com
  9. spring.mail.username=邮箱名
  10. spring.mail.password=这里填邮箱的授权码
  11. spring.mail.default-encoding=UTF-8
  12. spring.mail.port=465
  13. spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
  14. spring.mail.properties.mail.debug=true

注意:
PO3/SMTP服务必须开启
在这里插入图片描述

QQ邮箱发送邮件服务器主机名为:smtp.qq.com,必须使用使用SSL(spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory),端口号465或587(spring.mail.port=465)

通过单元测试来实现一封简单邮件的发送:

  1. @RunWith(SpringJUnit4ClassRunner.class)
  2. @SpringApplicationConfiguration(classes = Application.class)
  3. public class ApplicationTests {
  4. @Autowired
  5. private JavaMailSender mailSender;
  6. @Autowired
  7. private VelocityEngine velocityEngine;
  8. @Test
  9. public void sendSimpleMail() throws Exception {
  10. SimpleMailMessage message = new SimpleMailMessage();
  11. message.setFrom("173788752@qq.com");
  12. message.setTo("173788752@qq.com");
  13. message.setSubject("主题:简单邮件");
  14. message.setText("测试邮件内容");
  15. mailSender.send(message);
  16. }
  17. }

好,我们到这里,一个简单的邮件发送就完成了,运行一下该单元测试,看看效果如何?

注:由于Spring Boot的starter模块提供了自动化配置,所以在引入了spring-boot-starter-mail依赖之后,会根据配置文件中的内容去创建JavaMailSender实例,因此我们可以直接在需要使用的地方直接@Autowired来引入邮件发送对象。

进阶使用

在上例中,我们通过使用SimpleMailMessage实现了简单的邮件发送,但是实际使用过程中,我们还可能会带上附件、或是使用邮件模块等。这个时候我们就需要使用MimeMessage来设置复杂一些的邮件内容,下面我们就来依次实现一下。

发送附件

在上面单元测试中加入如下测试用例(通过MimeMessageHelper来发送一封带有附件的邮件):

  1. @Autowired
  2. private JavaMailSender mailSender;
  3. @Test
  4. public void sendAttachmentsMail() throws Exception {
  5. MimeMessage mimeMessage = mailSender.createMimeMessage();
  6. MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
  7. helper.setFrom("173788752@qq.com");
  8. helper.setTo("173788752@qq.com");
  9. helper.setSubject("主题:有附件");
  10. helper.setText("有附件的邮件");
  11. FileSystemResource file = new FileSystemResource(new File("weixin.jpg"));
  12. helper.addAttachment("附件-1.jpg", file);
  13. helper.addAttachment("附件-2.jpg", file);
  14. mailSender.send(mimeMessage);
  15. }

嵌入静态资源

除了发送附件之外,我们在邮件内容中可能希望通过嵌入图片等静态资源,让邮件获得更好的阅读体验,而不是从附件中查看具体图片,下面的测试用例演示了如何通过MimeMessageHelper实现在邮件正文中嵌入静态资源。

  1. @Test
  2. public void sendInlineMail() throws Exception {
  3. MimeMessage mimeMessage = mailSender.createMimeMessage();
  4. MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
  5. helper.setFrom("173788752@qq.com");
  6. helper.setTo("173788752@qq.com");
  7. helper.setSubject("主题:嵌入静态资源");
  8. helper.setText("<html><body><img src=\"cid:weixin\" ></body></html>", true);
  9. FileSystemResource file = new FileSystemResource(new File("weixin.jpg"));
  10. helper.addInline("weixin", file);
  11. mailSender.send(mimeMessage);
  12. }

这里需要注意的是addInline函数中资源名称weixin需要与正文中cid:weixin对应起来

模板邮件

通常我们使用邮件发送服务的时候,都会有一些固定的场景,比如重置密码、注册确认等,给每个用户发送的内容可能只有小部分是变化的。所以,很多时候我们会使用模板引擎来为各类邮件设置成模板,这样我们只需要在发送时去替换变化部分的参数即可。

在Spring Boot中使用模板引擎来实现模板化的邮件发送也是非常容易的,下面我们以velocity为例实现一下。

引入velocity模块的依赖:(在pom文件已经一如依赖)

  1. @Test
  2. public void sendTemplateMail() throws Exception {
  3. MimeMessage mimeMessage = mailSender.createMimeMessage();
  4. MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
  5. helper.setFrom("173788752@qq.com");
  6. helper.setTo("173788752@qq.com");
  7. helper.setSubject("主题:模板邮件");
  8. Map<String, Object> model = new HashedMap();
  9. model.put("username", "cicoding");
  10. String text = VelocityEngineUtils.mergeTemplateIntoString(
  11. velocityEngine, "template.vm", "UTF-8", model);
  12. helper.setText(text, true);
  13. mailSender.send(mimeMessage);
  14. }

在resources/templates/下,创建一个模板页面template.vm:

  1. <html>
  2. <body>
  3. <h3>你好, ${ username}, 这是一封模板邮件!</h3>
  4. </body>
  5. </html>

尝试运行一下,就可以收到内容为你好, cicoding, 这是一封模板邮件!的邮件。这里,我们通过传入username的参数,在邮件内容中替换了模板中的${username}变量。

https://blog.csdn.net/zhaokejin521/article/details/80859834 原文地址

发表评论

表情:
评论列表 (有 0 条评论,327人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Spring Boot实现发送QQ邮件

    简述 在日常工作开发中,项目中会使用到发送邮件功能,通过邮件实时通知,实现告警或预警功能,首先介绍以下与发送接受邮件相关的一些协议: 发送邮件:SMPT、MIME