可以通过springboot官方文档中Sending Email,找到类似如下java mail的使用文档

https://docs.spring.io/spring/docs/5.1.9.RELEASE/spring-framework-reference/integration.html#mail

一、导入java mail相关starter

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

二、发送不带附件和带附件邮件

1、发送邮件逻辑,再上面的java mail文档中,可以找到不带附件的邮件和带附件的邮件使用的发送邮件的bean不同,前者为JavaMailSender,后者为JavaMailSenderImpl,但通过查看集成关系可知,JavaMailSender的实现类其实只有JavaMailSenderImpl,所以,不管两者使用的是哪个,最终发送邮件调用的都是JavaMailSenderImpl中的send方法。并且在JavaMailSenderImpl中添加了一些配置属性,比如host,username等,使用JavaMailSender是获取不到这些属性的,故我们直接注入了JavaMailSenderImpl。通过测试得知,在springboot配置中,spring.mail.properties.开头的配置都会被放入到JavaMailSenderImpl中的javaMailProperties属性中,故有些额外的邮件发送参数,我们都可以通过spring.mail.properties.additional_attribute的方式来设置,比如设置邮件接收人:spring.mail.properties.receiver=xxx@163.com,xxxx@qq.com

  1. package com.springbootdemo.mail;
  2.  
  3. import java.io.File;
  4. import java.util.Properties;
  5.  
  6. import javax.annotation.Resource;
  7. import javax.mail.internet.MimeMessage;
  8.  
  9. import org.springframework.core.io.FileSystemResource;
  10. import org.springframework.mail.SimpleMailMessage;
  11. import org.springframework.mail.javamail.JavaMailSenderImpl;
  12. import org.springframework.mail.javamail.MimeMessageHelper;
  13. import org.springframework.stereotype.Component;
  14.  
  15. @Component
  16. public class MailSenderBusi {
  17.  
  18. @Resource
  19. private JavaMailSenderImpl mailSender;
  20.  
  21. public void sendMail() {
  22. Properties mailProperties = mailSender.getJavaMailProperties();
  23. SimpleMailMessage msg = new SimpleMailMessage();
  24. msg.setFrom(mailSender.getUsername());
  25. msg.setTo(mailProperties.getProperty("receiver").split(",")); // 可以是一个数组
  26. msg.setText("nice day");
  27. msg.setSubject("测试");
  28.  
  29. this.mailSender.send(msg);
  30. }
  31.  
  32. public void sendMailAttach() throws Exception {
  33. Properties mailProperties = mailSender.getJavaMailProperties();
  34. MimeMessage message = mailSender.createMimeMessage();
  35.  
  36. MimeMessageHelper helper = new MimeMessageHelper(message, true);
  37. helper.setFrom(mailSender.getUsername());
  38. helper.setTo(mailProperties.getProperty("receiver").split(","));
  39. // 第二个参数为是否支持html,如果为true,则表示支持,此时如果写入<h2>nice</h2>,则nice会被加粗,默认为false
    helper.setText("nice day2", true);
  40. helper.setSubject("测试2");
  41.      // 或者使用new FiledataSource
  42. FileSystemResource file = new FileSystemResource(new File("F:\\learn\\workplace\\webtest.zip"));
  43. helper.addAttachment("webdemo.zip", file);
  44. mailSender.send(message);
  45.  
  46. }
  47.  
  48. }

2、触发邮件发送

这里我们只是简单做测试,故简单实现了CommandLineRunner,在程序启动后就开始发送邮件。

  1. package com.springbootdemo;
  2.  
  3. import javax.annotation.Resource;
  4.  
  5. import org.springframework.boot.CommandLineRunner;
  6. import org.springframework.stereotype.Component;
  7.  
  8. import com.springbootdemo.mail.MailSenderBusi;
  9.  
  10. @Component
  11. public class InitMailSender implements CommandLineRunner{
  12.  
  13. @Resource
  14. private MailSenderBusi mailSenderBusi;
  15.  
  16. @Override
  17. public void run(String... args) throws Exception {
  18.  
  19. mailSenderBusi.sendMail();
  20. mailSenderBusi.sendMailAttach();
  21. }
  22. }

三、添加springboot配置

  1. spring.mail.protocol=smtp
  2. spring.mail.host=smtp.mxhichina.com
  3. spring.mail.port=25
  4. spring.mail.username=xxxxx@example.com
  5. spring.mail.password=password
  6.  
  7. spring.mail.properties.receiver=xxxx@163.com,xxxxx@qq.com
  8. spring.mail.properties.mail.smtp.auth=true
  9.  
  10. spring.mail.default-encoding=UTF-8

可能对于有些情况,需要使用到如下两个参数,但在本例中是不需要的

  1. spring.mail.properties.mail.smtp.starttls.enable=true
  2. spring.mail.properties.mail.smtp.starttls.required=true

通过以上简单三步,就可正常发送邮件了,具体深入的其他参数设置,后续再研究

springboot发送邮件,以及携带邮件附件简单使用的更多相关文章

  1. IntelliJ IDEA 2017版 spring-boot 2.0.5 邮件发送简单实例 (三)

    一.搭建SpringBoot项目 详见此文:https://www.cnblogs.com/liuyangfirst/p/8298588.html 注意: 需要添加mail依赖的包,同时还添加了lom ...

  2. 记录一次简单的springboot发送邮件功能

    场景:经常在我们系统中有通过邮件功能找回密码,或者发送生日祝福等功能,今天记录下springboot发送邮件的简单功能 1.引入maven <!-- 邮件开发--><dependen ...

  3. SpringBoot整合Mail发送邮件&发送模板邮件

    整合mail发送邮件,其实就是通过代码来操作发送邮件的步骤,编辑收件人.邮件内容.邮件附件等等.通过邮件可以拓展出短信验证码.消息通知等业务. 一.pom文件引入依赖 <dependency&g ...

  4. SpringBoot 发送邮件和附件

    作者:yizhiwaz 链接:www.jianshu.com/p/5eb000544dd7 源码:https://github.com/yizhiwazi/springboot-socks 其他文章: ...

  5. SpringBoot 发送邮件功能实现

    背景 有个小伙伴问我你以前发邮件功能怎么弄的.然后我就给他找了个demo,正好在此也写一下,分享给大家. 理清痛点 发送邮件,大家可以想一下,坑的地方在哪? 我觉得是三个吧. 第一:邮件白名单问题. ...

  6. springboot集成mail实现邮件服务

    1,新建mailconfig类,对邮件服务固定配置进行实体封装: import java.util.Properties; import org.springframework.beans.facto ...

  7. java发送邮件(一)--补充添加附件

    今天来记录一下如何使用java来发送邮件 背景 之前项目有个需求,当产品出现故障时会把情况上送给服务器,服务器发送邮件将故障产品的位置以及故障信息等告知维修人员.发送邮件的接口不是我负责的,但是有兴趣 ...

  8. 1.使用javax.mail, spring的JavaMailSender,springboot发送邮件

    一.java发邮件 电子邮件服务器:这些邮件服务器就类似于邮局,它主要负责接收用户投递过来的邮件,并把邮件投递到邮件接收者的电子邮箱中,按功能划分有两种类型 SMTP邮件服务器:用户替用户发送邮件和接 ...

  9. 使用JavaMail发送邮件和接受邮件

    转载:http://blog.csdn.net/zdp072/article/details/30977213 一. 为什么要学习JavaMail 为什么要学习JavaMail开发? 现在很多WEB应 ...

随机推荐

  1. linux后台启动项目命令

    在用xshell启动一个项目后,关闭了xshell后,项目又停止了 nohup python admin.py runserver & nohup ........  &   中间包含 ...

  2. inter® management engine interface黄色感叹号解决方法

    win10今天安装电脑驱动时发现inter®  management engine interface怎么装都是黄色感叹号,所以做了下以下得测试 1.inter®  management engine ...

  3. PMM 监控 MySQL 使用钉钉告警

    打开 PMM Server 页面,如图所示点进Alerting --> Notification channels 输入钉钉的信息,并且 Save Test 测试结果,没问题了 如何使用 gra ...

  4. 9. A Pythonic Object

    Thanks to the Python data model, your user-defined types can behave as naturally as the built-in typ ...

  5. c#中打开视频,word

    打开所有的文件的 代码,包括word/ppt/txt/xls/mp3/视频文件 添加using using System.Diagnostics; string fileName = @"D ...

  6. java--springmvc

    springmvc请求图 SpringMVC内部的执行流程1.用户发起到达中央调度器DispatcherServlet2.中央调度器DispatcherServlet把请求(some.do)交给了处理 ...

  7. ACM-ICPC 2019南昌网络赛I题 Yukino With Subinterval

    ACM-ICPC 2019南昌网络赛I题 Yukino With Subinterval 题目大意:给一个长度为n,值域为[1, n]的序列{a},要求支持m次操作: 单点修改 1 pos val 询 ...

  8. IntelliJ IDEA + Maven iml文件中依赖项的需求是什么?

    在Maven中,项目的依赖关系在pom.xml文件中指定.在IntelliJ IDEA中,即使对于Maven项目,相同的信息也存储在iml文件中.在两个地方有相同的信息需要什么? 当导入Maven项目 ...

  9. 洛谷P2341 [HAOI2006]受欢迎的牛 (Tarjan,SCC缩点)

    P2341 [HAOI2006]受欢迎的牛|[模板]强连通分量 https://www.luogu.org/problem/P2341 题目描述 每头奶牛都梦想成为牛棚里的明星.被所有奶牛喜欢的奶牛就 ...

  10. Introduction to Go Modules

    转:https://roberto.selbach.ca/intro-to-go-modules/ git init git add * git commit -am "First comm ...