springboot发送邮件,以及携带邮件附件简单使用
可以通过springboot官方文档中Sending Email,找到类似如下java mail的使用文档
https://docs.spring.io/spring/docs/5.1.9.RELEASE/spring-framework-reference/integration.html#mail
一、导入java mail相关starter
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-mail</artifactId>
- </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
- package com.springbootdemo.mail;
- import java.io.File;
- import java.util.Properties;
- import javax.annotation.Resource;
- import javax.mail.internet.MimeMessage;
- import org.springframework.core.io.FileSystemResource;
- import org.springframework.mail.SimpleMailMessage;
- import org.springframework.mail.javamail.JavaMailSenderImpl;
- import org.springframework.mail.javamail.MimeMessageHelper;
- import org.springframework.stereotype.Component;
- @Component
- public class MailSenderBusi {
- @Resource
- private JavaMailSenderImpl mailSender;
- public void sendMail() {
- Properties mailProperties = mailSender.getJavaMailProperties();
- SimpleMailMessage msg = new SimpleMailMessage();
- msg.setFrom(mailSender.getUsername());
- msg.setTo(mailProperties.getProperty("receiver").split(",")); // 可以是一个数组
- msg.setText("nice day");
- msg.setSubject("测试");
- this.mailSender.send(msg);
- }
- public void sendMailAttach() throws Exception {
- Properties mailProperties = mailSender.getJavaMailProperties();
- MimeMessage message = mailSender.createMimeMessage();
- MimeMessageHelper helper = new MimeMessageHelper(message, true);
- helper.setFrom(mailSender.getUsername());
- helper.setTo(mailProperties.getProperty("receiver").split(","));
- // 第二个参数为是否支持html,如果为true,则表示支持,此时如果写入<h2>nice</h2>,则nice会被加粗,默认为false
helper.setText("nice day2", true);- helper.setSubject("测试2");
- // 或者使用new FiledataSource
- FileSystemResource file = new FileSystemResource(new File("F:\\learn\\workplace\\webtest.zip"));
- helper.addAttachment("webdemo.zip", file);
- mailSender.send(message);
- }
- }
2、触发邮件发送
这里我们只是简单做测试,故简单实现了CommandLineRunner,在程序启动后就开始发送邮件。
- package com.springbootdemo;
- import javax.annotation.Resource;
- import org.springframework.boot.CommandLineRunner;
- import org.springframework.stereotype.Component;
- import com.springbootdemo.mail.MailSenderBusi;
- @Component
- public class InitMailSender implements CommandLineRunner{
- @Resource
- private MailSenderBusi mailSenderBusi;
- @Override
- public void run(String... args) throws Exception {
- mailSenderBusi.sendMail();
- mailSenderBusi.sendMailAttach();
- }
- }
三、添加springboot配置
- spring.mail.protocol=smtp
- spring.mail.host=smtp.mxhichina.com
- spring.mail.port=25
- spring.mail.username=xxxxx@example.com
- spring.mail.password=password
- spring.mail.properties.receiver=xxxx@163.com,xxxxx@qq.com
- spring.mail.properties.mail.smtp.auth=true
- spring.mail.default-encoding=UTF-8
可能对于有些情况,需要使用到如下两个参数,但在本例中是不需要的
- spring.mail.properties.mail.smtp.starttls.enable=true
- spring.mail.properties.mail.smtp.starttls.required=true
通过以上简单三步,就可正常发送邮件了,具体深入的其他参数设置,后续再研究
springboot发送邮件,以及携带邮件附件简单使用的更多相关文章
- IntelliJ IDEA 2017版 spring-boot 2.0.5 邮件发送简单实例 (三)
一.搭建SpringBoot项目 详见此文:https://www.cnblogs.com/liuyangfirst/p/8298588.html 注意: 需要添加mail依赖的包,同时还添加了lom ...
- 记录一次简单的springboot发送邮件功能
场景:经常在我们系统中有通过邮件功能找回密码,或者发送生日祝福等功能,今天记录下springboot发送邮件的简单功能 1.引入maven <!-- 邮件开发--><dependen ...
- SpringBoot整合Mail发送邮件&发送模板邮件
整合mail发送邮件,其实就是通过代码来操作发送邮件的步骤,编辑收件人.邮件内容.邮件附件等等.通过邮件可以拓展出短信验证码.消息通知等业务. 一.pom文件引入依赖 <dependency&g ...
- SpringBoot 发送邮件和附件
作者:yizhiwaz 链接:www.jianshu.com/p/5eb000544dd7 源码:https://github.com/yizhiwazi/springboot-socks 其他文章: ...
- SpringBoot 发送邮件功能实现
背景 有个小伙伴问我你以前发邮件功能怎么弄的.然后我就给他找了个demo,正好在此也写一下,分享给大家. 理清痛点 发送邮件,大家可以想一下,坑的地方在哪? 我觉得是三个吧. 第一:邮件白名单问题. ...
- springboot集成mail实现邮件服务
1,新建mailconfig类,对邮件服务固定配置进行实体封装: import java.util.Properties; import org.springframework.beans.facto ...
- java发送邮件(一)--补充添加附件
今天来记录一下如何使用java来发送邮件 背景 之前项目有个需求,当产品出现故障时会把情况上送给服务器,服务器发送邮件将故障产品的位置以及故障信息等告知维修人员.发送邮件的接口不是我负责的,但是有兴趣 ...
- 1.使用javax.mail, spring的JavaMailSender,springboot发送邮件
一.java发邮件 电子邮件服务器:这些邮件服务器就类似于邮局,它主要负责接收用户投递过来的邮件,并把邮件投递到邮件接收者的电子邮箱中,按功能划分有两种类型 SMTP邮件服务器:用户替用户发送邮件和接 ...
- 使用JavaMail发送邮件和接受邮件
转载:http://blog.csdn.net/zdp072/article/details/30977213 一. 为什么要学习JavaMail 为什么要学习JavaMail开发? 现在很多WEB应 ...
随机推荐
- linux后台启动项目命令
在用xshell启动一个项目后,关闭了xshell后,项目又停止了 nohup python admin.py runserver & nohup ........ & 中间包含 ...
- inter® management engine interface黄色感叹号解决方法
win10今天安装电脑驱动时发现inter® management engine interface怎么装都是黄色感叹号,所以做了下以下得测试 1.inter® management engine ...
- PMM 监控 MySQL 使用钉钉告警
打开 PMM Server 页面,如图所示点进Alerting --> Notification channels 输入钉钉的信息,并且 Save Test 测试结果,没问题了 如何使用 gra ...
- 9. A Pythonic Object
Thanks to the Python data model, your user-defined types can behave as naturally as the built-in typ ...
- c#中打开视频,word
打开所有的文件的 代码,包括word/ppt/txt/xls/mp3/视频文件 添加using using System.Diagnostics; string fileName = @"D ...
- java--springmvc
springmvc请求图 SpringMVC内部的执行流程1.用户发起到达中央调度器DispatcherServlet2.中央调度器DispatcherServlet把请求(some.do)交给了处理 ...
- ACM-ICPC 2019南昌网络赛I题 Yukino With Subinterval
ACM-ICPC 2019南昌网络赛I题 Yukino With Subinterval 题目大意:给一个长度为n,值域为[1, n]的序列{a},要求支持m次操作: 单点修改 1 pos val 询 ...
- IntelliJ IDEA + Maven iml文件中依赖项的需求是什么?
在Maven中,项目的依赖关系在pom.xml文件中指定.在IntelliJ IDEA中,即使对于Maven项目,相同的信息也存储在iml文件中.在两个地方有相同的信息需要什么? 当导入Maven项目 ...
- 洛谷P2341 [HAOI2006]受欢迎的牛 (Tarjan,SCC缩点)
P2341 [HAOI2006]受欢迎的牛|[模板]强连通分量 https://www.luogu.org/problem/P2341 题目描述 每头奶牛都梦想成为牛棚里的明星.被所有奶牛喜欢的奶牛就 ...
- Introduction to Go Modules
转:https://roberto.selbach.ca/intro-to-go-modules/ git init git add * git commit -am "First comm ...