springboot3——Email

举报
兔老大 发表于 2021/04/26 01:39:19 2021/04/26
【摘要】 maven导入包: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> <version>2.1.6.RELEASE</version> </dependency> 参数配置: # MailP...

maven导入包:


  
  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-mail</artifactId>
  4. <version>2.1.6.RELEASE</version>
  5. </dependency>

参数配置:


  
  1. # MailProperties
  2. spring.mail.host=smtp.sina.com
  3. spring.mail.port=465
  4. spring.mail.username=hebtu_ftz@sina.com
  5. spring.mail.password=c33ce75afaff8015
  6. spring.mail.protocol=smtps
  7. spring.mail.properties.mail.smtp.ssl.enable=true
  8. spring.mail.properties.mail.smtl.auth=true
  9. spring.mail.properties.mail.smtp.starttls.enable=true
  10. spring.mail.properties.mail.smtp.starttls.required=true

写发送工具类:


  
  1. package com.now.community.community.util;
  2. import org.slf4j.Logger;
  3. import org.slf4j.LoggerFactory;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.beans.factory.annotation.Value;
  6. import org.springframework.mail.javamail.JavaMailSender;
  7. import org.springframework.mail.javamail.MimeMessageHelper;
  8. import org.springframework.stereotype.Component;
  9. import javax.mail.MessagingException;
  10. import javax.mail.internet.MimeMessage;
  11. @Component
  12. public class MailClient {
  13. private static final Logger logger = LoggerFactory.getLogger(MailClient.class);
  14. @Autowired
  15. private JavaMailSender mailSender;
  16. @Value("${spring.mail.username}")
  17. private String from;
  18. public void sendMail(String to, String subject, String content) {
  19. try {
  20. MimeMessage message = mailSender.createMimeMessage();
  21. MimeMessageHelper helper = new MimeMessageHelper(message);
  22. helper.setFrom(from);
  23. helper.setTo(to);
  24. helper.setSubject(subject);
  25. helper.setText(content, true);
  26. mailSender.send(helper.getMimeMessage());
  27. } catch (MessagingException e) {
  28. logger.error("发送邮件失败:" + e.getMessage());
  29. }
  30. }
  31. }

测试类:


  
  1. package com.now.community.community;
  2. import com.now.community.community.util.MailClient;
  3. import org.junit.Test;
  4. import org.junit.runner.RunWith;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.boot.test.context.SpringBootTest;
  7. import org.springframework.test.context.ContextConfiguration;
  8. import org.springframework.test.context.junit4.SpringRunner;
  9. import org.thymeleaf.TemplateEngine;
  10. import org.thymeleaf.context.Context;
  11. @RunWith(SpringRunner.class)
  12. @SpringBootTest
  13. @ContextConfiguration(classes = CommunityApplication.class)
  14. public class MailTests {
  15. @Autowired
  16. private MailClient mailClient;
  17. @Autowired
  18. private TemplateEngine templateEngine;
  19. @Test
  20. public void testTextMail(){
  21. mailClient.sendMail("450427951@qq.com","TEST","welcome");
  22. }
  23. @Test
  24. public void testHtmlMail(){
  25. Context context=new Context();
  26. context.setVariable("username","fantianzuo");
  27. String content=templateEngine.process("/mail/demo",context);
  28. System.out.println(content);
  29. mailClient.sendMail("450427951@qq.com","HTML",content);
  30. }
  31. }

html:


  
  1. <!DOCTYPE html>
  2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>邮件示例</title>
  6. </head>
  7. <body>
  8. <p>欢迎你, <span style="color:red;" th:text="${username}"></span>!</p>
  9. </body>
  10. </html>

 

文章来源: fantianzuo.blog.csdn.net,作者:兔老大RabbitMQ,版权归原作者所有,如需转载,请联系作者。

原文链接:fantianzuo.blog.csdn.net/article/details/102689621

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。