Spring Boot 发送邮件功能案例分析

网友投稿 294 2023-03-20


Spring Boot 发送邮件功能案例分析

邮件服务简介

邮件服务在互联网早期就已经出现,如今已成为人们互联网生活中必不可少的一项服务。那么邮件服务是怎么工作的呢?如下给出邮件发送与接收的典型过程:

1、发件人使用SMTP协议传输邮件到邮件服务器A;

2、邮件服务器A根据邮件中指定的接收者,投送邮件至相应的邮件服务器B;

3、收件人使用POP3协议从邮件服务器B接收邮件。

SMTP(Simple Mail Transfer Protocol)是电子邮件(email)传输的互联网标准,定义在RFC5321,默认使用端口25;

POP3(Post Office Protocol - Version 3)主要用于支持使用客户端远程管理在服务器上的电子邮件。定义在RFC 1939,为POP协议的第三版(最新版)。

这两个协议均属于TCP/IP协议族的应用层协议,运行在TCP层之上。

我们日常收发邮件使用的客户端、Web Mail的背后都在运行着这两个协议,完成收发邮件的过程。而现在我们需要使用

SMTP协议来把发送给用户的邮件传输到邮件服务器。

从客户端传输邮件到服务器需要双方的配合,而规则就定义在SMTP协议中。我们现在需要做的是找一个SMTP服务器,再实现一个SMTP客户端,然后让客户端发送邮件到服务器。

正文如下

Spring框架使用javaMailSender接口为发送邮件提供了一个简单的抽象,并且Spring Boot也为它提供了自动配置和一个starter模块。

如果spring.mail.host和相关的库(通过spring-boot-starter-mail定义)都存在,一个默认的JavaMailSender将被创建。该sender可以通过spring.mail命名空间下的配置项进一步自定义,下面本站素文宅博客具体讲述一下Spring Boot如何实现发送邮件。

引入spring-boot-starter-mail依赖,在pom.xml配置文件中增加如下内容(基于之前章节“Spring Boot 构建框架”中的pom.xml文件):

org.springframework.boot

spring-boot-starter-mail

应用发送邮件案例

在 application.properties 配置文件中加入如下配置(注意替换自己的用户名和密码):

spring.mail.host=smtp.qq.com

spring.mail.username=用户名 //发送方的邮箱

spring.mail.password=密码 //对于qq邮箱而言 密码指的就是发送方的授权码

spring.mail.properties.mail.smtp.auth=true

spring.mail.properties.mail.smtp.starttls.enable=true

spring.mail.properties.mail.smtp.starttls.required=true

邮件service服务代码,具体如下:

@Service

public class MailService {

private final Logger logger = LoggerFactory.getLogger(this.getClass());

@Autowired

private JavaMailSender sender;

@Value("${spring.mail.username}")

private String from;

/**

* 发送纯文本的简单邮件

* @param to

* @param subject

* @param content

*/

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);

logger.info("简单邮件已经发送。");

} catch (Exception e) {

logger.error("发送简单邮件时发生异常!", e);

}

}

/**

* 发送html格式的邮件

* @param to

* @param subject

* @param content

*/

public void sendHtmlMail(String to, String subject, String content){

MimeMessage message = sender.createMimeMessage();

try { hagQmAIO

//true表示需要创建一个multipart message

MimeMessageHelper helper = new MimeMessageHelper(message, true);

helper.setFrom(from);

helper.setTo(to);

helper.setSubject(subject);

helper.setText(content, true);

senhttp://der.send(message);

logger.info("html邮件已经发送。");

} catch (MessagingException e) {

logger.error("发送html邮件时发生异常!", e);

}

}

/**

* 发送带附件的邮件

* @param to

* @param subject

* @param content

* @param filePath

*/

public void sendAttachmentsMail(String to, String subject, String content, String filePath){

MimeMessage message = sender.createMimeMessage();

try {

//true表示需要创建一个multipart message

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);

logger.info("带附件的邮件已经发送。");

} catch (MessagingException e) {

logger.error("发送带附件的邮件时发生异常!", e);

}

}

/**

* 发送嵌入静态资源(一般是图片)的邮件

* @param to

* @param subject

* @param content 邮件内容,需要包括一个静态资源的id,比如:

* @param rscPath 静态资源路径和文件名

* @param rscId 静态资源id

*/

public void sendInlineResourceMail(String to, String subject, String content, String rscPath, String rscId){

MimeMessage message = sender.createMimeMessage();

try {

//true表示需要创建一个multipart message

MimeMessageHelper helper = new MimeMessageHelper(message, true);

helper.setFrom(from);

helper.setTo(to);

helper.setSubject(subject);

helper.setText(content, true);

FileSystemResource res = new FileSystemResource(new File(rscPath));

helper.addInline(rscId, res);

sender.send(message);

logger.info("嵌入静态资源的邮件已经发送。");

} catch (MessagingException e) {

logger.error("发送嵌入静态资源的邮件时发生异常!", e);

}

}

}

简单测试代码如下:

public class MailTests extends BasicUtClass{

@Autowired

private MailService mailService;

private String to = "xujijun@mail.cn";

@Test

public void sendSimpleMail() {

mailService.sendSimpleMail(to, "主题:简单邮件", "测试邮件内容");

}

}

总结

以上所述是给大家介绍的Spring Boot 发送邮件功能案例分析,希望对大家有所帮助,如果大家有任何疑问请给我留言,会及时回复大家的。在此也非常感谢大家对我们网站的支持!


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Bootstrap 树控件使用经验分享(图文解说)
下一篇:接口管理平台建设方案(接口服务管理)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~