赞
踩
发送邮件的功能有时在业务中还是需要使用到的,比如订单方面的业务,用户下单后需要给用户发送一个订单相关的邮件;又或者是某些系统在业务层来集成预警功能,出现问题时及时给开发和运维发送预警邮件。那么,接下来将讲一下在springboot项目中邮件发送功能的集成与其简单的使用。
首先,在POM文件中加入如下依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
springboot为我们提供了这个邮件相关的stater,旨在更方便引入依赖和进行配置。spring-boot-starter-mail
主要包含了如下与邮件相关的依赖包:
其中,spring的mail是在javax的mail上做了更抽象的封装,方便我们使用。仔细查看源码,就会观察到如下一些关键的类:
在application.yml中加入邮件相关的配置,项目启动后便会在spring的bean容器中注入MailSender和JavaMailSender(他们有共同的实现类JavaMailSenderImpl),之后便可以在代码中使用相关的方法实现发送邮件的功能了。配置信息如下:
spring:
mail:
host: smtp.163.com
port: 25
username: lazycece@163.com
password: 客户端授权码
依赖已引入,亦完成mail相关的配置设置,接下来就该来实现发送邮件的功能了。邮件的的发送涉及如下相关:
首先,来定义一个简单的实体(使用了Lombok,详见Lombok插件讲解),表示邮件相关的信息,如下:
@Data @Builder @NoArgsConstructor @AllArgsConstructor public class Message { /** * 收件人 */ private String to; /** * 发件人 */ private String from; /** * 邮件主题 */ private String subject; /** * 邮件内容 */ private String text; /** * 附件 */ private String attachment; /** * 嵌入资源 */ private String inlineResource; }
这里只定义了常规内容,抄送CC(Carbon Copy),加密抄送BCC(Blind Carbon Copy)就不再说明了。
邮件内容的包装可以是SimpleMailMessage
,又或者是MimeMailMessage
,顾名思义便能想到这两个类的用途,接下来便来说明简单邮件内容的发送、发送附件和内容内嵌资源(图片,音频)等功能。
使用SimpleMailMessage
来包装邮件信息,使用MailSender
来执行发送事件,代码如下:
public void sendSimpleMessage(Message message) {
SimpleMailMessage msg = new SimpleMailMessage();
msg.setFrom(message.getFrom());
msg.setTo(message.getTo());
msg.setSubject(message.getSubject());
msg.setText(message.getText());
mailSender.send(msg);
}
使用MimeMessagePreparator
来包装MimeMailMessage
邮件信息,使用JavaMailSender
来执行发送事件,代码如下:
public void sendPreparatorMimeMessage(Message message) {
MimeMessagePreparator preparator = mimeMessage -> {
mimeMessage.setFrom(message.getFrom());
mimeMessage.setRecipients(javax.mail.Message.RecipientType.TO, message.getTo());
mimeMessage.setSubject(message.getSubject());
mimeMessage.setText(message.getText());
};
javaMailSender.send(preparator);
}
使用MimeMessageHelper
来包装MimeMailMessage
邮件信息,使用JavaMailSender
来执行发送事件,代码如下:
public void sendBasicMimeMessage(Message message) {
MimeMailMessage mimeMailMessage = new MimeMailMessage(javaMailSender.createMimeMessage());
try {
MimeMessageHelper helper = mimeMailMessage.getMimeMessageHelper();
helper.setTo(message.getTo());
helper.setFrom(message.getFrom());
helper.setSubject(message.getSubject());
helper.setText(message.getText());
} catch (MessagingException e) {
log.error("build mail message error");
}
javaMailSender.send(mimeMailMessage.getMimeMessage());
}
附件功能只能使用MimeMailMessage
进行邮件信息包装,可以添加单个邮件,多个邮件,这里以单个邮件为例,代码如下:
public void sendAttachmentsMimeMessage(Message message) {
MimeMailMessage mimeMailMessage = new MimeMailMessage(javaMailSender.createMimeMessage());
try {
MimeMessageHelper helper = new MimeMessageHelper(mimeMailMessage.getMimeMessage(), true);
helper.setFrom(message.getFrom());
helper.setTo(message.getTo());
helper.setSubject(message.getSubject());
helper.setText(message.getText());
FileSystemResource file = new FileSystemResource(new File(message.getAttachment()));
helper.addAttachment(file.getFilename(), file);
} catch (MessagingException e) {
log.error("build mail message error");
}
javaMailSender.send(mimeMailMessage.getMimeMessage());
}
嵌入资源功能亦只能使用MimeMailMessage
进行邮件信息包装,这里以在邮件内容中嵌入一张图片为例,“resource1234”是资源的id,确保在邮件嵌入资源中唯一,才能正确引入到html。代码如下:
public void sendInlineImageMimeMessage(Message message) { MimeMailMessage mimeMailMessage = new MimeMailMessage(javaMailSender.createMimeMessage()); try { MimeMessageHelper helper = new MimeMessageHelper(mimeMailMessage.getMimeMessage(), true); helper.setFrom(message.getFrom()); helper.setTo(message.getTo()); helper.setSubject(message.getSubject()); String html = "<html><body><img src='cid:resource1234'></body></html>"; helper.setText(message.getText(), html); FileSystemResource resource = new FileSystemResource(new File(message.getInlineResource())); helper.addInline("resource1234", resource); } catch (MessagingException e) { log.error("build mail message error"); } javaMailSender.send(mimeMailMessage.getMimeMessage()); }
下面贴出单元测试代码:
package com.lazycece.sbac.mail.manager; import com.lazycece.sbac.mail.entity.Message; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import javax.annotation.Resource; /** * @author lazycece * @date 2019/5/21 */ @SpringBootTest @RunWith(SpringRunner.class) public class MailManagerTest { @Resource private MailManager mailManager; private Message.MessageBuilder commonMail() { return Message.builder() .from("lazycece@163.com") .to("lazycece@163.com") .subject("你好"); } @Test public void testSendSimpleMessage() { Message message = commonMail() .text("Hello, simple message ! ") .build(); mailManager.sendSimpleMessage(message); } @Test public void testSendPreparatorMimeMessage() { Message message = commonMail() .text("Hello, preparator mime message ! ") .build(); mailManager.sendPreparatorMimeMessage(message); } @Test public void sendBasicMimeMessage() { Message message = commonMail() .text("Hello, basic mime email ! ") .build(); mailManager.sendBasicMimeMessage(message); } @Test public void sendAttachmentsMimeMessage() { Message message = commonMail() .text("Hello, attachment mime email ! ") .attachment("/home/lazycece/下载/temp.png") .build(); mailManager.sendAttachmentsMimeMessage(message); } @Test public void sendInlineImageMimeMessage() { Message message = commonMail() .text("Hello, inline image mime email ! ") .inlineResource("/home/lazycece/下载/temp.png") .build(); mailManager.sendInlineImageMimeMessage(message); } }
案例源码地址:https://github.com/lazycece/springboot-actual-combat/tree/master/springboot-ac-email
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。