1.环境搭建
- 导入依赖
<!--邮件发送依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
- 可见,我们直接使用的是springBoot中集成的邮件发送的启动器,既然是springBoot启动器导入的依赖,那么在导入的依赖中,必然有一个mailautoconfig的类,我们可以搜索一下,来看看springBoot为邮件发送配置了哪些默认的参数
- 这个自动配置类果不其然有一个配置参数类MailProperties,我们可以点击进去查看配置参数详情
- 从上图可知,参数配置的就和原来javaweb中配置的参数一样,在配置参数类中我们可以发现,我们可以在application配置文件中使用"spring.mail"来配置邮件发送的参数
- 那么邮件发送的具体具体实现springBoot帮我们封装好了吗?当然,springBoot就是想让我们集中自己的注意力在核心功能的开发上,对于固定步骤的操作它都有封装
- 在MailSenderAutoConfiguration类中还导入了一个类MailSenderJndiConfiguration,我们可以点击去看看这个类
@Configuration(proxyBeanMethods = false) @ConditionalOnClass(Session.class) @ConditionalOnProperty(prefix = "spring.mail", name = "jndi-name") @ConditionalOnJndi class MailSenderJndiConfiguration { private final MailProperties properties; MailSenderJndiConfiguration(MailProperties properties) { this.properties = properties; } @Bean JavaMailSenderImpl mailSender(Session session) { JavaMailSenderImpl sender = new JavaMailSenderImpl(); sender.setDefaultEncoding(this.properties.getDefaultEncoding().name()); sender.setSession(session); return sender; } @Bean @ConditionalOnMissingBean Session session() { String jndiName = this.properties.getJndiName(); try { return JndiLocatorDelegate.createDefaultResourceRefLocator().lookup(jndiName, Session.class); } catch (NamingException ex) { throw new IllegalStateException(String.format("Unable to find Session in JNDI location %s", jndiName), ex); } } }
- 可见这个类的mailSender()已经帮我们实现了邮件发送的流程了,所以我们在使用的时候只需去application中配置参数,并从spring容器中将这个实例获取到,调用mailSender()即可发送邮件
2.简单邮件发送
- 这里还是使用QQ邮箱
- 获取邮箱登陆授权码
- 去application中配置参数
spring.mail.username=qq邮箱 spring.mail.password=授权码 spring.mail.host=smtp.qq.com #QQ邮箱需要开启加密验证 spring.mail.properties.mail.smtl.ssl.enable=true
- 代码实现
@SpringBootTest class Springboot09TestApplicationTests { @Autowired JavaMailSenderImpl mailSender; @Test void contextLoads() { mailSender.send(); } }
- mailSender.send()需要传入参数,具体参数我们可以查看一下send()的源码
public interface MailSender { void send(SimpleMailMessage var1) throws MailException; void send(SimpleMailMessage... var1) throws MailException; }
- 所以我们需要传入SimpleMailMessage对象作为参数,所以我们需要new一个SimpleMailMessage对象
@SpringBootTest class Springboot09TestApplicationTests { @Autowired JavaMailSenderImpl mailSender; @Test void contextLoads() { SimpleMailMessage mailMessage = new SimpleMailMessage(); mailSender.send(mailMessage); } }
- 那么我们邮件发送的信息、从哪里发送、发送给谁的信息怎么配置呢?当然是在SimpleMailMessage对象中配置了,我们可以查看该类的源码
public class SimpleMailMessage implements MailMessage, Serializable { @Nullable private String from;//发件方 @Nullable private String replyTo; @Nullable private String[] to;//收件方 @Nullable private String[] cc; @Nullable private String[] bcc; @Nullable private Date sentDate; @Nullable private String subject;//邮件主题 @Nullable private String text;//邮件内容 }
- 可见发送邮件常用的配置参数都可以在这个类里面配置,并且每一个成员属性都有对应的set(),所以我们可以直接在测试方法中调用
@SpringBootTest class Springboot09TestApplicationTests { @Autowired JavaMailSenderImpl mailSender; @Test void contextLoads() { SimpleMailMessage mailMessage = new SimpleMailMessage(); mailMessage.setFrom("发件方邮箱");//发件方 mailMessage.setTo("收件方邮箱");//收件方 mailMessage.setSubject("测试springBoot邮件发送");//邮件主题 mailMessage.setText("这是使用springBoot封装的对象JavaMailSenderImpl发送的邮件");//邮件信息 mailSender.send(mailMessage); } }
- 效果
测试成功!
3.复杂邮件发送
- 但是上面的邮件是简单邮件的发送,即邮件中只有文字信息,我们还可以使用springBoot发送复杂邮件,比如图片和附件;在原来发送复杂邮件的时候我们实现的方式为使用MIME(多用途互联网邮件扩展类型),复杂邮件发送回忆
- springBoot将发送复杂邮件的步骤也为我们进行了封装,我们需要使用封装好的类MimeMessage +MimeMessageHelper
- MimeMessage 表示这是一个复杂邮件对象MimeMessageHelper由于帮助复杂邮件对象组装各个组成部分,对于复杂邮件中简单邮件部分的操作和简单邮件调用的方法一样,而添加附件的时候需要调用MimeMessageHelper.addAttachment()
- 代码实现
@Test void contextLoads2() throws MessagingException { MimeMessage mimeMessage = mailSender.createMimeMessage(); //创建复杂邮件对象 MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true); //获取发送复杂邮件的帮助类,参数1为要发生的复杂邮件对象,参数2为multipart,即是否支持多文件/复杂邮件由多个部分拼装而成,一般复杂邮件都要设置为true helper.setFrom("发件方邮箱");//发件方 helper.setTo("收件方邮箱");//收件方 helper.setSubject("测试springBoot复杂邮件发送");//邮件主题 helper.setText("<p style='color:red'>这是使用springBoot封装的对象JavaMailSenderImpl发送的<strong>复杂邮件</strong></p>",true); //邮件信息,参数1为要发送的邮件消息,参数2表示是否支持解析前面邮件信息中的html标签 helper.addAttachment("附件1.png",new File("附件1的地址"));//添加一个附件,参数1为附件名称,参数2为附件在本地的file对象 helper.addAttachment("附件2.png",new File("附件2的地址"));//添加一个附件 mailSender.send(mimeMessage); }
- 效果
- 需要注意一点:附件名称后面必须带上附件的文件类型,否则接收之后不能正常打开
4.将邮件发送抽象成一个可以复用的工具方法
- 这里就直接封装发送复杂邮件的步骤
/** * 复杂邮件发送方法 * @param from 发送发邮箱 * @param to 接收方邮箱 * @param subject 邮件主题 * @param text 邮件正文 * @param html 是否支持解析邮件中的html标签 * @param attachmentName 附件名称 * @param attachmentUrl 附件地址 * @throws MessagingException * @author thhh */ @Test void contextLoads3(String from, String to, String subject, String text, Boolean html, String attachmentName, String attachmentUrl) throws MessagingException { MimeMessage mimeMessage = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true); helper.setFrom(from); helper.setTo(to); helper.setSubject(subject); helper.setText(text,html); helper.addAttachment(attachmentName,new File(attachmentUrl)); mailSender.send(mimeMessage); }