引入依赖

  1. <!-- 父配置-->
  2. <parent>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-parent</artifactId>
  5. <version>2.4.3</version>
  6. <relativePath/>
  7. </parent>
  8. ......
  9. <!-- springboot-->
  10. <dependency>
  11. <groupId>org.springframework.boot</groupId>
  12. <artifactId>spring-boot-starter-web</artifactId>
  13. </dependency>
  14. <!-- 邮件-->
  15. <dependency>
  16. <groupId>org.springframework.boot</groupId>
  17. <artifactId>spring-boot-starter-mail</artifactId>
  18. </dependency>

配置文件:

  1. spring:
  2. mail:
  3. # 配置 SMTP 服务器地址
  4. host: smtp.qq.com
  5. # 发送者邮箱
  6. username: ooahz@qq.com
  7. # 邮箱密码(即邮件授权码)
  8. password: axxxxxxxxz
  9. # 邮箱端口号(QQ邮箱为465或587)
  10. port: 587
  11. # 默认的邮件编码为UTF-8
  12. default-encoding: UTF-8
  13. # 配置SSL 加密工厂
  14. properties:
  15. mail:
  16. smtp:
  17. socketFactoryClass: javax.net.ssl.SSLSocketFactory
  18. # 开启Debug
  19. debug: true

启动类:

  1. import org.springframework.boot.SpringApplication;
  2. import org.springframework.boot.autoconfigure.SpringBootApplication;
  3. import org.springframework.scheduling.annotation.EnableScheduling;
  4. @EnableScheduling
  5. @SpringBootApplication
  6. public class Application {
  7. public static void main(String[] args){
  8. SpringApplication.run(Application.class, args);
  9. }
  10. }

携带文字发送

接口类:

  1. import org.springframework.beans.factory.annotation.Autowired;
  2. import org.springframework.mail.SimpleMailMessage;
  3. import org.springframework.mail.javamail.JavaMailSender;
  4. import org.springframework.web.bind.annotation.PostMapping;
  5. import org.springframework.web.bind.annotation.RestController;
  6. import java.util.Date;
  7. /**
  8. * @author ahzoo
  9. * @create 2022/2/11
  10. * @desc 邮件发送
  11. */
  12. @RestController
  13. public class email {
  14. @Autowired
  15. JavaMailSender javaMailSender;
  16. /**
  17. * 发送简单邮件
  18. * @return success
  19. */
  20. @PostMapping("/send")
  21. public String sendEmail() {
  22. // 构建邮件发送对象
  23. SimpleMailMessage message = new SimpleMailMessage();
  24. // 设置邮件主题
  25. message.setSubject("这是一封测试邮件");
  26. // 设置邮件发送者,与配置文件中的邮箱保持一致
  27. message.setFrom("ooahz@qq.com");
  28. // 设置邮件接收者,可以有多个接收者,中间用逗号隔开,以下类似
  29. // message.setTo("123@qq.com","999qq.com");
  30. message.setTo("ooahz@qq.com");
  31. // 设置邮件抄送人,可以有多个抄送人
  32. // message.setCc("999@163.com");
  33. // // 设置隐秘抄送人,可以有多个
  34. // message.setBcc("999@outlook.com");
  35. // 设置邮件发送日期
  36. message.setSentDate(new Date());
  37. // 设置邮件的正文
  38. message.setText("这是一封测试邮件————from Ahzoo");
  39. // 发送邮件
  40. javaMailSender.send(message);
  41. return "发送成功";
  42. }
  43. }

因为开启了debug功能,所以邮件发送结果会在控制台打印:
image.png
测试发送:
image.png
image.png

携带附件发送

  1. import org.springframework.beans.factory.annotation.Autowired;
  2. import org.springframework.mail.SimpleMailMessage;
  3. import org.springframework.mail.javamail.JavaMailSender;
  4. import org.springframework.mail.javamail.MimeMessageHelper;
  5. import org.springframework.web.bind.annotation.PostMapping;
  6. import org.springframework.web.bind.annotation.RestController;
  7. import javax.mail.MessagingException;
  8. import javax.mail.internet.MimeMessage;
  9. import java.io.File;
  10. import java.util.Date;
  11. /**
  12. * @author ahzoo
  13. * @create 2022/2/11
  14. * @desc 邮件发送
  15. */
  16. @RestController
  17. public class email {
  18. @Autowired
  19. JavaMailSender javaMailSender;
  20. /**
  21. * 携带附件发送
  22. *
  23. * @return result
  24. */
  25. @PostMapping("/sendAttachment")
  26. public String sendFile() {
  27. // 使用MimeMessage对象发送复杂邮件
  28. MimeMessage mimeMessage = javaMailSender.createMimeMessage();
  29. // 使用MimeMessageHelper对邮件进行配置,true表示构建一个可以带附件的邮件对象
  30. MimeMessageHelper helper = null;
  31. try {
  32. helper = new MimeMessageHelper(mimeMessage, true);
  33. helper.setSubject("测试附件");
  34. helper.setFrom("ooahz@qq.com");
  35. helper.setTo("ooahz@qq.com");
  36. // helper.setCc("999@163.com");
  37. // helper.setBcc("999@outlook.com");
  38. helper.setSentDate(new Date());
  39. helper.setText("这是一封测试邮件————from Ahzoo");
  40. File path = new File(ResourceUtils.getURL("classpath:").getPath());
  41. // 第一个参数是自定义的名称,后缀需要加上,第二个参数是文件的位置
  42. helper.addAttachment("文件.txt", new File(path + "/static/z.txt"));
  43. javaMailSender.send(mimeMessage);
  44. return "发送成功";
  45. } catch (Exception e) {
  46. e.printStackTrace();
  47. return "发送失败" + e.getMessage();
  48. }
  49. }
  50. }

image.png

携带静态资源发送

  1. import org.springframework.beans.factory.annotation.Autowired;
  2. import org.springframework.mail.SimpleMailMessage;
  3. import org.springframework.mail.javamail.JavaMailSender;
  4. import org.springframework.mail.javamail.MimeMessageHelper;
  5. import org.springframework.web.bind.annotation.PostMapping;
  6. import org.springframework.web.bind.annotation.RestController;
  7. import javax.mail.MessagingException;
  8. import javax.mail.internet.MimeMessage;
  9. import java.io.File;
  10. import java.util.Date;
  11. /**
  12. * @author ahzoo
  13. * @create 2022/2/11
  14. * @desc 邮件发送
  15. */
  16. @RestController
  17. public class email {
  18. @Autowired
  19. JavaMailSender javaMailSender;
  20. /**
  21. * 携带静态资源发送
  22. *
  23. * @return
  24. */
  25. @PostMapping("sendResource")
  26. public String sendImg() {
  27. MimeMessage mimeMessage = javaMailSender.createMimeMessage();
  28. MimeMessageHelper helper = null;
  29. try {
  30. helper = new MimeMessageHelper(mimeMessage, true);
  31. helper.setSubject("测试静态资源");
  32. helper.setFrom("ooahz@qq.com");
  33. helper.setTo("ooahz@qq.com");
  34. // helper.setCc("999@163.com");
  35. // helper.setBcc("999@outlook.com");
  36. helper.setSentDate(new Date());
  37. // 设置邮件正文内容。cid为占位符,对应下面的contentId。true表示正文为Html
  38. helper.setText("<p>图片1:</p><img src='cid:image1'/><p>图片2:</p><img src='cid:image1'/>", true);
  39. // contentId:对应上面占位符中的cid。file:静态资源路径
  40. File path = new File(ResourceUtils.getURL("classpath:").getPath());
  41. helper.addInline("image1", new File(path + "/static/1.png"));
  42. helper.addInline("image2", new File(path + "/static/ahzoo.jpg"));
  43. javaMailSender.send(mimeMessage);
  44. return "发送成功";
  45. } catch (Exception e) {
  46. e.printStackTrace();
  47. return "发送失败" + e.getMessage();
  48. }
  49. }
  50. }

image.png

使用模板发送

常见的模板引擎主要有:ThymeleafFreemaker,这里不做演示

后记

项目结构:
image.png

报错:

  1. Description:
  2. Field javaMailSender in com.music.demo.Controller.UserController required a bean of type 'org.springframework.mail.javamail.JavaMailSender' that could not be found.
  3. The injection point has the following annotations:
  4. - @org.springframework.beans.factory.annotation.Autowired(required=true)

原因:配置文件未配置正确

项目源码