spring boot 发送邮件

朴灿烈づ我的快乐病毒、 2022-06-16 14:54 311阅读 0赞

配置application.properties 文件

  1. spring.mail.host=smtp.qq.com
  2. spring.mail.username=770960436@qq.com
  3. spring.mail.password=xxxxx
  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

注意密码不是qq密码,也不是邮箱密码是授权码

授权码生成规则:

  1. 1.登录qq邮箱
  2. 2.设置
  3. 3.POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务
  4. 4.开启服务(POP3SMTP...服务)
  5. 5.点击生成授权码
  6. 6.发送短信
  7. 7.获取授权码

pom.xml 配置

  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.didispace</groupId>
  5. <artifactId>Chapter4-5-1</artifactId>
  6. <version>1.0.0</version>
  7. <packaging>jar</packaging>
  8. <name>Chapter4-5-1</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>

发送邮件测试

  1. package com.didispace;
  2. import org.apache.commons.collections.map.HashedMap;
  3. import org.apache.velocity.app.VelocityEngine;
  4. import org.junit.Test;
  5. import org.junit.runner.RunWith;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.boot.test.SpringApplicationConfiguration;
  8. import org.springframework.core.io.FileSystemResource;
  9. import org.springframework.mail.SimpleMailMessage;
  10. import org.springframework.mail.javamail.JavaMailSender;
  11. import org.springframework.mail.javamail.MimeMessageHelper;
  12. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  13. import org.springframework.ui.velocity.VelocityEngineUtils;
  14. import javax.mail.internet.MimeMessage;
  15. import java.io.File;
  16. import java.util.Map;
  17. @RunWith(SpringJUnit4ClassRunner.class)
  18. @SpringApplicationConfiguration(classes = Application.class)
  19. public class ApplicationTests {
  20. public static String from = "770960436@qq.com";
  21. public static String to = "borntofight@sina.com";
  22. @Autowired
  23. private JavaMailSender mailSender;
  24. @Autowired
  25. private VelocityEngine velocityEngine;
  26. @Test
  27. public void sendSimpleMail() throws Exception {
  28. SimpleMailMessage message = new SimpleMailMessage();
  29. message.setFrom(from);
  30. message.setTo(to);
  31. message.setSubject("主题:简单邮件");
  32. message.setText("测试邮件内容");
  33. mailSender.send(message);
  34. }
  35. @Test
  36. public void sendAttachmentsMail() throws Exception {
  37. MimeMessage mimeMessage = mailSender.createMimeMessage();
  38. MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
  39. helper.setFrom(from);
  40. helper.setTo(to);
  41. helper.setSubject("主题:有附件");
  42. helper.setText("有附件的邮件");
  43. FileSystemResource file = new FileSystemResource(new File("weixin.jpg"));
  44. helper.addAttachment("附件-1.jpg", file);
  45. helper.addAttachment("附件-2.jpg", file);
  46. mailSender.send(mimeMessage);
  47. }
  48. @Test
  49. public void sendInlineMail() throws Exception {
  50. MimeMessage mimeMessage = mailSender.createMimeMessage();
  51. MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
  52. helper.setFrom(from);
  53. helper.setTo(to);
  54. helper.setSubject("主题:嵌入静态资源");
  55. helper.setText("<html><body><img src=\"cid:weixin\" ></body></html>", true);
  56. FileSystemResource file = new FileSystemResource(new File("weixin.jpg"));
  57. helper.addInline("weixin", file);
  58. mailSender.send(mimeMessage);
  59. }
  60. @Test
  61. public void sendTemplateMail() throws Exception {
  62. MimeMessage mimeMessage = mailSender.createMimeMessage();
  63. MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
  64. helper.setFrom(from);
  65. helper.setTo(to);
  66. helper.setSubject("主题:模板邮件");
  67. Map<String, Object> model = new HashedMap();
  68. model.put("username", "didi");
  69. String text = VelocityEngineUtils.mergeTemplateIntoString(
  70. velocityEngine, "template.vm", "UTF-8", model);
  71. helper.setText(text, true);
  72. mailSender.send(mimeMessage);
  73. }
  74. }

发表评论

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

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

相关阅读