千家信息网

Springboot如何整合邮件服务

发表于:2025-02-14 作者:千家信息网编辑
千家信息网最后更新 2025年02月14日,本篇文章给大家分享的是有关Springboot如何整合邮件服务,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。介绍邮件服务是常用的服务之一
千家信息网最后更新 2025年02月14日Springboot如何整合邮件服务

本篇文章给大家分享的是有关Springboot如何整合邮件服务,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

介绍

邮件服务是常用的服务之一,作用很多,对外可以给用户发送活动、营销广告等;对内可以发送系统监控报告与告警。

本文将介绍Springboot如何整合邮件服务,并给出不同邮件服务商的整合配置。

如图所示:

开发过程

Springboot搭建

Springboot的搭建非常简单,我们使用 Spring Initializr来构建,十分方便,选择需要用到的模块,就能快速完成项目的搭建:

引入依赖

为了使用邮件服务,我们需要引入相关的依赖,对于Springboot加入下面的依赖即可:

  org.springframework.boot  spring-boot-starter-mail

配置文件

需要配置邮件服务提供商的相关参数,如服务地址、用户名及密码等。下面的例子是QQ的配置,其中密码并不是QQ密码,而是QQ授权码,后续我们再讲怎么获得。

Springboot的配置文件application.yml如下:

server:  port: 8080spring:  profiles:    active: qq---spring:  profiles: qq  mail:    host: smtp.qq.com    username: xxx@qq.com    password: xxx    properties:      mail:        smtp:          auth: true          starttls:            enable: true            required: true---spring:  profiles: netEase  mail:    host: smtp.163.com    username: xxx@163.com    password: xxx    properties:      mail:        smtp:          auth: true          starttls:            enable: true            required: true

实现发送服务

JavaMailSender注入,组装Message后,就可以发送最简单的文本邮件了。

@Autowiredprivate JavaMailSender emailSender;public void sendNormalText(String from, String to, String subject, String text) {  SimpleMailMessage message = new SimpleMailMessage();  message.setFrom(from);  message.setTo(to);  message.setSubject(subject);  message.setText(text);  emailSender.send(message);}

调用接口

服务调用实现后,通过Controller对外暴露REST接口,具体代码如下:

@Value("${spring.mail.username}")private String username;@Autowiredprivate MailService mailService;@GetMapping("/normalText")public Mono sendNormalText() {  mailService.sendNormalText(username, username,              "Springboot Mail(Normal Text)",              "This is a mail from Springboot!");  return Mono.just("sent");}

把实现的MailService注入到Controller里,调用对应的方法即可。本次的邮件发送人和收件人都是同一个帐户,实际实现可以灵活配置。

通过Postman调用接口来测试一下能不能正常发送: 成功返回"sent",并收到了邮件,测试通过。

多种类型邮件

简单文本邮件

简单文本邮件如何发送,刚刚已经讲解,不再赘述。

HTML邮件

纯文本虽然已经能满足很多需求,但很多时候也需要更加丰富的样式来提高邮件的表现力。这时HTML类型的邮件就非常有用。

Service代码如下:

public void sendHtml(String from, String to, String subject, String text) throws MessagingException {  MimeMessage message = emailSender.createMimeMessage();  MimeMessageHelper helper = new MimeMessageHelper(message, true);  helper.setFrom(from);  helper.setTo(to);  helper.setSubject(subject);  helper.setText(text, true);  emailSender.send(message);}

与简单的文本不同的是,本次用到了MimeMessageMimeMessageHelper,这是非常有用的类,后续我们经常会用到,组合使用能大大丰富邮件表现形式。

Controller的代码如下:

@GetMapping("/html")public Mono sendHtml() throws MessagingException {  mailService.sendHtml(username, username,              "Springboot Mail(HTML)",              "

This is a mail from Springboot!

"); return Mono.just("sent");}

带附件邮件

邮件发送文件再正常不过,发送附件需要使用MimeMessageHelper.addAttachment(String attachmentFilename, InputStreamSource inputStreamSource)方法,第一个参数为附件名,第二参数为文件流资源。Service代码如下:

public void sendAttachment(String from, String to, String subject, String text, String filePath) throws MessagingException {  MimeMessage message = emailSender.createMimeMessage();  MimeMessageHelper helper = new MimeMessageHelper(message, true);  helper.setFrom(from);  helper.setTo(to);  helper.setSubject(subject);  helper.setText(text, true);  FileSystemResource file = new FileSystemResource(new File(filePath));  helper.addAttachment(filePath, file);  emailSender.send(message);}

Controller代码如下:

@GetMapping("/attachment")public Mono sendAttachment() throws MessagingException {  mailService.sendAttachment(username, username,              "Springboot Mail(Attachment)",              "

Please check the attachment!

", "/Pictures/postman.png"); return Mono.just("sent");}

带静态资源邮件

我们访问的网页其实也是一个HTML,是可以带很多静态资源的,如图片、视频等。Service代码如下:

public void sendStaticResource(String from, String to, String subject, String text, String filePath, String contentId) throws MessagingException {  MimeMessage message = emailSender.createMimeMessage();  MimeMessageHelper helper = new MimeMessageHelper(message, true);  helper.setFrom(from);  helper.setTo(to);  helper.setSubject(subject);  helper.setText(text, true);  FileSystemResource file = new FileSystemResource(new File(filePath));  helper.addInline(contentId, file);  emailSender.send(message);}

其中,contentId为HTML里静态资源的ID,需要对应好。

Controller代码如下:

@GetMapping("/inlinePicture")public Mono sendStaticResource() throws MessagingException {  mailService.sendStaticResource(username, username,             "Springboot Mail(Static Resource)",             "With inline picture",             "/Pictures/postman.png",             "picture");  return Mono.just("sent");}

模板邮件

Java的模板引擎很多,著名的有Freemarker、Thymeleaf、Velocity等,这不是本点的重点,所以只以Freemarker为例使用。

Service代码如下:

@Autowiredprivate FreeMarkerConfigurer freeMarkerConfigurer;public void sendTemplateFreemarker(String from, String to, String subject, Map model, String templateFile) throws Exception {  MimeMessage message = emailSender.createMimeMessage();  MimeMessageHelper helper = new MimeMessageHelper(message, true);  helper.setFrom(from);  helper.setTo(to);  helper.setSubject(subject);  Template template = freeMarkerConfigurer.getConfiguration().getTemplate(templateFile);  String html = FreeMarkerTemplateUtils.processTemplateIntoString(template, model);  helper.setText(html, true);  emailSender.send(message);}

注意需要注入FreeMarkerConfigurer,然后使用FreeMarkerTemplateUtils解析模板,返回String,就可以作为内容发送了。

Controller代码如下:

@GetMapping("/template")public Mono sendTemplateFreemarker() throws Exception {  Map model = new HashMap<>();  model.put("username", username);  model.put("templateType", "Freemarker");  mailService.sendTemplateFreemarker(username, username,                                     "Springboot Mail(Template)",                                     model,                                     "template.html");  return Mono.just("sent");}

注意模板文件template.html要放在**resources/templates/**目录下面,这样才能找得到。

模板内容如下:

        Title

Hello ${username}

This is a mail from Springboot using ${templateType}

其中${username}${templateType}为需要替换的变量名,Freemarker提供了很多丰富的变量表达式,这里不展开讲了。

集成不同邮件服务商

邮件服务的提供商很多,国内最常用的应该是QQ邮箱和网易163邮箱了。

QQ

集成QQ邮件需要有必备的账号,还要开通授权码。开通授权码后配置一下就可以使用了,官方的文档如下:

什么是授权码,它又是如何设置?

需要注意的是,开通授权码是需要使用绑定的手机号发短信到特定号码的,如果没有绑定手机或者绑定手机不可用,那都会影响开通。

开通之后,授权码就要以作为密码配置到文件中。

163

网易的开通方式与QQ没有太大差别,具体的指导可以看如下官方文档:

如何开启客户端授权码?

同样也是需要绑定手机进行操作。

总结

本次例子发送后收到邮件如图所示: 邮件功能强大,Springboot也非常容易整合。技术利器,善用而不滥用。

以上就是Springboot如何整合邮件服务,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注行业资讯频道。

邮件 服务 代码 配置 邮件服务 文件 整合 文本 模板 密码 手机 资源 不同 参数 接口 附件 静态 例子 内容 变量 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 数据库如何查看传奇所有装备 扬州海航软件开发诚信服务 小学网络安全教育工作方案 第一个app软件开发者 黄浦区软件开发项目 idea连数据库打不开 数据库处理技术 贝拉嘉信网络技术有限公司 软件开发文档都不愿意写 工业网络技术专转本 数据库如何建表 靠谱的pda软件开发服务 福斯近红外光谱分析仪数据库 绿色上网共建网络安全手抄报 国家基础地理信息中心数据库 烽火众智软件开发面试 网吧服务器清零后不开机 数据库编程需要什么软件 深圳net软件开发定制费用 眼镜行业软件开发有哪些原因 mc直连服务器地址 咸宁专业的软件开发方案 服务器硬件指标说明怎么写 国家计算机三级网络技术难吗 运城网络安全技术部 软件开发做到多大合适 计算机网络安全培训大纲 前端node怎么做成临时服务器 河北餐饮软件开发常用指南 java软件开发公司职位
0