springboot版本:2.1.6
前置条件:引入mail的依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
根据SpringBoot AutoConfig的特性,既然引入了starter-mail,自然应该有对应的AutoConfiguration类。
通过查找发现MailSenderAutoConfiguration
是其自动配置类
@Configuration
@ConditionalOnClass({ MimeMessage.class, MimeType.class, MailSender.class })
@ConditionalOnMissingBean(MailSender.class)
@Conditional(MailSenderCondition.class)
@EnableConfigurationProperties(MailProperties.class)
@Import({ MailSenderJndiConfiguration.class, MailSenderPropertiesConfiguration.class })
public class MailSenderAutoConfiguration {}
通过观察,得到几个主要信息:
- ConditionalOnMissingBean(MailSender.class) 说明只有当没有该Bean存在时,才会执行这个自动配置,也就是说我们可以在系统里自己构造该Bean(这是后话)
- 根据Import的机制发现,关于Mail的两种配置分别在两个配置类中实现的。
我们主要看MailSenderPropertiesConfiguration类
@Configuration
@ConditionalOnProperty(prefix = "spring.mail", name = "host")
class MailSenderPropertiesConfiguration {
...
@Bean
@ConditionalOnMissingBean
public JavaMailSenderImpl mailSender() {
//说明只有当spring容器中不存在该Bean才会创建
JavaMailSenderImpl sender = new JavaMailSenderImpl();
applyProperties(sender);
return sender;
}
private void applyProperties(JavaMailSenderImpl sender) {
sender.setHost(this.properties.getHost());
if (this.properties.getPort() != null) {
sender.setPort(this.properties.getPort());
}
sender.setUsername(this.properties.getUsername());
sender.setPassword(this.properties.getPassword());
sender.setProtocol(this.properties.getProtocol());
if (this.properties.getDefaultEncoding() != null) {
sender.setDefaultEncoding(this.properties.getDefaultEncoding().name());
}
if (!this.properties.getProperties().isEmpty()) {
sender.setJavaMailProperties(asProperties(this.properties.getProperties()));
}
}
...
}
所以通过上述代码可以得出一个结论,在系统中不存在JavaMailSenderImpl这个Bean的前提下,Spring会根据spring.mail前缀的配置数据 来构造这个Bean。这句话有两层含义:①spring需要这些配置项 ②这个Bean可以直接注入使用
以腾讯企业邮箱为例发送邮件
spring.mail.host=smtp.exmail.qq.com
spring.mail.username=邮箱
spring.mail.password=jGTtTtxgzDqeai3u
spring.mail.default-encoding=UTF-8
@Autowired
private JavaMailSenderImpl javaMailSender;
public void sendMessage(){
SimpleMailMessage message=new SimpleMailMessage();
message.setSubject("测试");
message.setText("测试");
message.setTo("收件人邮箱");
message.setFrom("发件人邮箱");
javaMailSender.send(message);
}
至此 简单的文本邮件就能够发送出去了(这里先不介绍其他格式邮件的发送情况)。
但是如果不想使用默认的这个Bean呢~~ 没关系 可以人为手动构建,代码如下:
问题一:
如果不想使用默认的这个Bean,该怎么办呢?没关系,可以手动人为构建,如下
@Configuration
public class Config{
@Bean
public JavaMailSenderImpl javaMailSender(){
JavaMailSenderImpl javaMailSender=new JavaMailSenderImpl();
javaMailSender.setProtocol("smtp");
javaMailSender.setUsername("发件人邮箱");
javaMailSender.setPassword("发件人密码或授权码");
javaMailSender.setDefaultEncoding("UTF-8");
javaMailSender.setHost("smtp.exmail.qq.com");
Properties properties=new Properties();
properties.setProperty("mail.smtp.port","465");
properties.setProperty("mail.smtp.auth","true");
properties.setProperty("mail.smtp.ssl.enable","true");
javaMailSender.setJavaMailProperties(properties);
return javaMailSender;
}
}
在使用时 正常注入该Bean即可
问题二:
上述的demo在本地正常运行,但是在服务器是便抛出异常信息:Could not connect to SMTP host: smtp.exmail.qq.com, port: 25;
经过查看,需要在配置文件中追加以下配置便可正常运行
spring.mail.properties.mail.smtp.port=465
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.ssl.enable=true
特此记录。