发送邮件的功能有时在业务中还是需要使用到的,比如订单方面的业务,用户下单后需要给用户发送一个订单相关的邮件;又或者是某些系统在业务层来集成预警功能,出现问题时及时给开发和运维发送预警邮件。那么,接下来将讲一下在springboot项目中邮件发送功能的集成与其简单的使用。
依赖引入
首先,在POM文件中加入如下依赖:
1 2 3 4
| <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
|
springboot为我们提供了这个邮件相关的stater,旨在更方便引入依赖和进行配置。spring-boot-starter-mail
主要包含了如下与邮件相关的依赖包:
- org.springframework:spring-context-support
- com.sun.mail.javax.email
其中,spring的mail是在javax的mail上做了更抽象的封装,方便我们使用。仔细查看源码,就会观察到如下一些关键的类:
- 邮件发送:MailSender -> JavaMailSender -> JavaMailSenderImpl
- 邮件消息包装:MailMessasge, SimpleMailMessage, MimeMailMessage
- 邮件消息的helper:MimeMessageHelper
配置设置
在application.yml中加入邮件相关的配置,项目启动后便会在spring的bean容器中注入MailSender和JavaMailSender(他们有共同的实现类JavaMailSenderImpl),之后便可以在代码中使用相关的方法实现发送邮件的功能了。配置信息如下:
1 2 3 4 5 6
| spring: mail: host: smtp.163.com port: 25 username: lazycece@163.com password: 客户端授权码
|
使用案例
依赖已引入,亦完成mail相关的配置设置,接下来就该来实现发送邮件的功能了。邮件的的发送涉及如下相关:
- 简单邮件内容发送
- MIME(Multipurpose Internet Mail Extensions)多用途互联网邮件扩展类型的邮件类型
首先,来定义一个简单的实体(使用了Lombok,详见Lombok插件讲解),表示邮件相关的信息,如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| @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
来执行发送事件,代码如下:
1 2 3 4 5 6 7 8
| 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
来执行发送事件,代码如下:
1 2 3 4 5 6 7 8 9
| 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
来执行发送事件,代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| 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
进行邮件信息包装,可以添加单个邮件,多个邮件,这里以单个邮件为例,代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| 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。代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| 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()); }
|
测试验证
下面贴出单元测试代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
| 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;
@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