zoukankan      html  css  js  c++  java
  • 【SpringBoot系列】邮件发送

    1、application.yml配置

     1 spring:
     2   mail:
     3     host: smtp.qq.com
     4     username: xxxxxxxxx@qq.com
     5     password: ywbjioacvurubcjx
     6     properties:
     7       mail:
     8         smtp:
     9           ssl:
    10             enable: true

    2、发送简单邮件

     1 @SpringBootTest
     2 class MailApplicationTests {
     3 
     4     @Autowired
     5     JavaMailSenderImpl javaMailSender;  //用于发送邮件
     6 
     7     @Test
     8     void contextLoads() {
     9 
    10         //一个简单邮件
    11         SimpleMailMessage simpleMail = new SimpleMailMessage();
    12         simpleMail.setSubject("zym你好");              //邮件主题
    13         simpleMail.setText("are you OK?");            //邮件内容
    14         simpleMail.setTo("xxxxxxx@qq.com");         //接收者
    15         simpleMail.setFrom("xxxxxxxx@qq.com");        //发送者
    16         javaMailSender.send(simpleMail);
    17     }
    18 }

    3、发送html邮件

     1 @SpringBootTest
     2 class MailApplicationTests {
     3 
     4     @Autowired
     5     JavaMailSenderImpl javaMailSender;  //用于发送邮件
     6 
     7     @Test
     8     void contextLoads2() throws MessagingException {
     9 
    10         //一个复杂邮件
    11         MimeMessage mimeMessage = javaMailSender.createMimeMessage();
    12         //用MimeMessageHelper组装复杂邮件,第二个参数为true,可以发送附件
    13         MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
    14         //正文,可以发送html文本,需要第二个参数为true
    15         helper.setSubject("zym你好");
    16         helper.setText("<p style='color:red'>春暖花开</p>",true);
    17         //附件
    18         helper.addAttachment("1.png", new File("C:\Users\admin\Desktop\images\4fa869f4794c4d64918337a2d2def1f3.jpg"));
    19         helper.setTo("xxxxxxxxx@qq.com");           //接收者
    20         helper.setFrom("xxxxxxxx@qq.com");         //发送者
    21         javaMailSender.send(mimeMessage);
    22     }
    23 }

    4、整合thymeleaf

     1 @SpringBootTest
     2 class MailApplicationTests {
     3 
     4     @Autowired
     5     private MailService mailService;
     6 
     7     @Test
     8     public void testSend() {
     9         String to = "xxxxxxxx@qq.com";
    10         mailService.send(to, "模板邮件", UUID.randomUUID().toString().toUpperCase());
    11     }
    12 }
     1 @Service
     2 public class MailService {
     3     @Autowired
     4     private JavaMailSender mailSender;
     5 
     6     /**
     7      * 用来发送模版邮件
     8      */
     9     @Autowired
    10     private TemplateEngine templateEngine;
    11 
    12     @Value("${spring.mail.username}")
    13     private String from;
    14 
    15     public void send(String to, String subject, String text) {
    16 
    17         Context context = new Context();
    18         context.setVariable("project", "demo");
    19         context.setVariable("author", "yimcarson");
    20         context.setVariable("code", text);
    21         String emailContent = templateEngine.process("mail", context);
    22 
    23         MimeMessage message = mailSender.createMimeMessage();
    24         MimeMessageHelper helper = null;
    25         try {
    26             helper = new MimeMessageHelper(message, true);
    27             helper.setFrom(from);
    28             helper.setTo(to);
    29             helper.setSubject(subject);
    30             helper.setText(emailContent, true);
    31         } catch (MessagingException e) {
    32             e.printStackTrace();
    33         }
    34         mailSender.send(message);
    35     }
    36 }
     1 <!DOCTYPE html>
     2 <html lang="en" xmlns:th="http://www.thymeleaf.org">
     3 <head>
     4     <meta charset="UTF-8">
     5     <title>yimcarson</title>
     6     <style>
     7         body {
     8             text-align: center;
     9             margin-left: auto;
    10             margin-right: auto;
    11         }
    12         #main {
    13             text-align: center;
    14             position: absolute;
    15         }
    16     </style>
    17 </head>
    18 <body>
    19 <div id="main">
    20     <h3>Welcome <span th:text="${project}"></span> -By <span th:text=" ${author}"></span></h3>
    21     Your Verification Code is
    22     <h2><span th:text="${code}"></span></h2>
    23 </div>
    24 </body>
    25 </html>
  • 相关阅读:
    postgresql导入及导出
    高效构建Web应用 教你玩转Play框架 http://www.anool.net/?p=577
    强制远程桌面
    js对日期操作 获取两个日期的相差是否在几月之内
    ACM 擅长排列的小明
    路由vue-router
    小村系列之十五:倒了(修订版)
    小村系列之十三:次贷危机
    小村系列之十:民族主义的兴衰
    小村系列之八:村长开会
  • 原文地址:https://www.cnblogs.com/sheep9527/p/14586723.html
Copyright © 2011-2022 走看看