1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > SpringBoot整合邮件发送功能

SpringBoot整合邮件发送功能

时间:2019-05-29 01:27:54

相关推荐

SpringBoot整合邮件发送功能

说在前头:本人为大二在读学生,书写文章的目的是为了对自己掌握的知识和技术进行一定的记录,同时乐于与大家一起分享,因本人资历尚浅,发布的文章难免存在一些错漏之处,还请阅读此文章的大牛们见谅与斧正。若在阅读时有任何的问题,也可通过评论提出,本人将根据自身能力对问题进行一定的解答。

前言

此时文章我们将来讲述如何使用SpringBoot发送邮件。文章将从准备工作开始,解释邮件POP3/SMTP/IMAP功能和他们之间的区别,并使用其中一种方式来实现SpringBoot发送简单文本邮件、带有HTML属性的邮件、带有附件的邮件。

01—准备工作

在发送邮件前,用于发送邮件的邮箱需要开启POP3/SMTP/IMAP功能,开启步骤如下(使用网易163邮箱举例

02—什么是POP3、SMTP以及IMAP?

POP3是Post Office Protocol 3的简称,即邮局协议的第3个版本,它规定怎样将个人计算机连接到Internet的邮件服务器和下载电子邮件的电子协议。它是因特网电子邮件的第一个离线协议标准,POP3允许用户从服务器上把邮件存储到本地主机(即自己的计算机)上,同时删除保存在邮件服务器上的邮件,而POP3服务器则是遵循POP3协议的接收邮件服务器,用来接收电子邮件的。

SMTP的全称是“Simple Mail Transfer Protocol”,即简单邮件传输协议。它是一组用于从源地址到目的地址传输邮件的规范,通过它来控制邮件的中转方式。SMTP 协议属于 TCP/IP 协议簇,它帮助每台计算机在发送或中转信件时找到下一个目的地。SMTP 服务器就是遵循 SMTP 协议的发送邮件服务器。

SMTP 认证,简单地说就是要求必须在提供了账户名和密码之后才可以登录 SMTP 服务器,这就使得那些垃圾邮件的散播者无可乘之机。

增加 SMTP 认证的目的是为了使用户避免受到垃圾邮件的侵扰。

IMAP全称是Internet Mail Access Protocol,即交互式邮件存取协议,它是跟POP3类似邮件访问标准协议之一。不同的是,开启了IMAP后,您在电子邮件客户端收取的邮件仍然保留 在服务器上,同时在客户端上的操作都会反馈到服务器上,如:删除邮件,标记已读等,服务器上的邮件也会做相应的动作。所以无论从浏览器登录邮箱或者客户端 软件登录邮箱,看到的邮件以及状态都是一致的。

03—POP3与IMAP的区别

POP3协议允许电子邮件客户端下载服务器上的邮件,但是在客户端的操作(如移动邮件、标记已读等),不会反馈到服务器上,比如通过客户端收取了邮箱中的3封邮件并移动到其他文件夹,邮箱服务器上的这些邮件是没有同时被移动的 。

IMAP提供webmail 与电子邮件客户端之间的双向通信,客户端的操作都会反馈到服务器上,对邮件进行的操作,服务器上的邮件也会做相应的动作。

同时,IMAP像POP3那样提供了方便的邮件下载服务,让用户能进行离线阅读。IMAP提供的摘要浏览功能可以让你在阅读完所有的邮件到达时间、主题、发件人、大小等信息后才作出是否下载的决定。此外,IMAP 更好地支持了从多个不同设备中随时访问新邮件。

总之,IMAP 整体上为用户带来更为便捷和可靠的体验。POP3 更易丢失邮件或多次下载相同的邮件,但 IMAP 通过邮件客户端与webmail 之间的双向同步功能很好地避免了这些问题。

04—使用SpringBoot发送邮件

①引入依赖:(主要引入spring-boot-starter-mail依赖)

<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-mail</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies>

②配置文件:

spring:mail:host: # 邮箱服务器地址username: 你的邮箱password: 上面步骤保存的授权码default-encoding: UTF-8

③启动类:

package com.example.mailtest;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class MailTestApplication {public static void main(String[] args) {SpringApplication.run(MailTestApplication.class, args);}}

④编写邮箱工具类:

package com.example.mailtest.utils;​import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.core.io.FileSystemResource;import org.springframework.mail.SimpleMailMessage;import org.springframework.mail.javamail.JavaMailSender;import org.springframework.mail.javamail.MimeMessageHelper;import org.ponent;​import javax.mail.MessagingException;import javax.mail.internet.MimeMessage;import java.io.File;​/*** <p>邮件发送工具类</p>* @author Bosen /6/16 14:07*/@Componentpublic class MailUtil {​@Value("${spring.mail.username}")String from;​@AutowiredJavaMailSender sender;​/** 发送简单的文本邮件*/public void sendSimpleMail(String to, String subject, String content) {SimpleMailMessage message = new SimpleMailMessage();message.setFrom(from);message.setTo(to);message.setSubject(subject);message.setText(content);try {sender.send(message);System.out.println("简单文本邮件发送成功");} catch (Exception e) {e.printStackTrace();System.out.println("简单文本邮件发送失败");}}​/** 发送具有HTML属性的文本邮件*/public void sendHtmlMail(String to, String subject, String content) {MimeMessage message = sender.createMimeMessage();​try {MimeMessageHelper helper = new MimeMessageHelper(message, true);helper.setFrom(from);helper.setTo(to);helper.setSubject(subject);helper.setText(content, true);​sender.send(message);System.out.println("HTML文本邮件发送成功");} catch (Exception e) {e.printStackTrace();System.out.println("HTML文本邮件发送失败");}}​/** 发送具有静态资源的文件*/public void sendAttachmentsMail(String to, String subject, String content, String filePath) {MimeMessage message = sender.createMimeMessage();​try {MimeMessageHelper helper = new MimeMessageHelper(message, true);helper.setFrom(from);helper.setTo(to);helper.setSubject(subject);helper.setText(content, true);​FileSystemResource file = new FileSystemResource(new File(filePath));String fileName = filePath.substring(filePath.lastIndexOf(File.separator));helper.addAttachment(fileName, file);​sender.send(message);System.out.println("静态文件邮件发送成功");} catch (MessagingException e) {System.out.println("静态文件邮件发送失败");}}}

⑤测试类:

package com.example.mailtest;​import com.example.mailtest.utils.MailUtil;import org.junit.jupiter.api.Test;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;​@SpringBootTestclass MailTestApplicationTests {​@AutowiredMailUtil mailUtil;​private String to = "bosen_once@"; // 接收者的邮箱地址private String subject; // 邮件标题private String content; // 邮件内容​@Testpublic void testSimpleMail() {subject = "简单文本邮件发送测试标题";content = "简单文本邮件发送测试内容";mailUtil.sendSimpleMail(to, subject, content);}​@Testpublic void testHtmlMail() {subject = "HTML文本邮件发送测试标题";content = "<a href='/'>点击跳转到百度搜索</a>";mailUtil.sendHtmlMail(to, subject, content);}​@Testpublic void testAttachmentsMail() {subject = "静态文件邮件发送测试标题";content = "静态文件邮件发送测试内容";String file = "C:\\Users\\Administrator\\Pictures\\header.jpeg";mailUtil.sendAttachmentsMail(to, subject, content, file);}​}

测试

完成上述操作后我们就可以开始测试啦!!

测试简单文本邮件的发送:执行方法testSimpleMail(); 执行完成后打开收件邮箱发现多了一条邮件(内容如下)

测试带有HTML属性的邮件的发送:执行方法testHtmlMail();

测试带有静态文件的邮件:执行方法testAttachmentsMail();

总结

至此相信小伙伴们已经对POP3/SMTP/IMAP有了一定的了解,并且学会了使用SpringBoot来发送简单文本邮件、带有HTML属性的文本邮件、带有附件的邮件,三种邮件。

👇扫描二维码关注

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。