作者:追梦1819
原文:https://www.cnblogs.com/yanfei1819/p/11118340.html
版权声明:本文为博主原创文章,转载请附上博文链接!
引言
邮件的重要性也无需多说了,例如注册验证,消息通知,系统异常提醒等,都离不开邮件的发送。
版本信息
- JDK:1.8
- SpringBoot :2.1.4.RELEASE
- maven:3.3.9
- IDEA:2019.1.1
- mail:2.1.4.RELEASE
使用
本示例演示的是 SpringBoot 项目发送邮件。
首先,创建项目,引入 maven 依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
<version>2.1.4.RELEASE</version>
</dependency>
然后,在 application.properties 中配置相关信息:
spring.mail.username=xxx@xyz.com
spring.mail.password=xxxxxx
spring.mail.host=smtp.xx.com
spring.mail.properties.mail.smtp.ssl.enable=true
最后,测试代码:
package com.yanfei1819.mail;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.test.context.junit4.SpringRunner;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
@RunWith(SpringRunner.class)
@SpringBootTest
public class MailApplicationTests {
@Autowired
private JavaMailSenderImpl sender;
@Test
public void contextLoads() {
SimpleMailMessage message = new SimpleMailMessage();
message.setSubject("开会");
message.setText("今晚6点开会");
message.setTo("xxx@163.com");
message.setFrom("xxx@xyz.com");
sender.send(message);
}
}
在收件箱中可以查看到对应结果。
上述是发送一封简单的邮件。如何发送一封复杂的邮件呢,可以发送附件的那种?
其实也很简单,其余的配置都相同。重新写一个测试方法:
@Test
public void test() throws MessagingException {
// 创建邮件
MimeMessage message = sender.createMimeMessage();
final MimeMessageHelper hepler = new MimeMessageHelper(message,true);
// 邮件设置
hepler.setSubject("开会");
hepler.setText("<b>今晚6点开会</b>",true);
hepler.setTo("xxx@163.com");
hepler.setFrom("xxx@xyz.com");
// 发送附件
hepler.addAttachment("mybatis源码",new File("C:\Users\Administrator\Desktop\MyBatis源码分析.md"));
// 邮件发送
sender.send(message);
}
运行,可以查看结果。
相关源码
SpringBoot 邮件发送是不是很简单?只需要开发者注意几个需要配置的属性和组件即可。没有太多的复杂的代码。
下面我们看看在源码中是如何实现的。
先找到 spring-boot-autoconfigure-2.1.4.RELEASE.jar 下面的 mail 包,仔细查看 MailSenderPropertiesConfiguration
类,其中
@Bean
@ConditionalOnMissingBean
public JavaMailSenderImpl mailSender() {
JavaMailSenderImpl sender = new JavaMailSenderImpl();
applyProperties(sender);
return sender;
}
即为邮件发送组件。读者可以深入了解自动配置原理。
关于邮件的配置属性,都在 MailProperties
中:
package org.springframework.boot.autoconfigure.mail;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "spring.mail")
public class MailProperties {
private static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;
/**
* SMTP server host. For instance, `smtp.example.com`.
*/
private String host;
/**
* SMTP server port.
*/
private Integer port;
/**
* Login user of the SMTP server.
*/
private String username;
/**
* Login password of the SMTP server.
*/
private String password;
/**
* Protocol used by the SMTP server.
*/
private String protocol = "smtp";
/**
* Default MimeMessage encoding.
*/
private Charset defaultEncoding = DEFAULT_CHARSET;
/**
* Additional JavaMail Session properties.
*/
private Map<String, String> properties = new HashMap<>();
/**
* Session JNDI name. When set, takes precedence over other Session settings.
*/
private String jndiName;
// set/get 省略
}
![](https://img2018.cnblogs.com/blog/1183871/201907/1183871-20190702085337959-2050866187.png)