使用springboot发送邮件

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

特此记录。

适用人群通用各大网易系,腾讯QQ系,新浪系,阿里系等主流邮箱;同时也适用于企业开发的企业邮箱,进行收件和发件。课程概述通用各大网易系,腾讯QQ系,新浪系,阿里系等主流邮箱;同时也适用于企业开发的企业邮箱,进行收件和发件。POP3是Post Office Protocol 3的简称,即邮局协议的第3个版本,它规定怎样将个人计算机连接到Internet的邮件服务器和下载电子邮件的电子协议。它是因特网电子邮件的第一个离线协议标准,POP3允许用户从服务器上把邮件存储到本地主机(即自己的计算机)上,同时删除保存在邮件服务器上的邮件,而POP3服务器则是遵循POP3协议的接收邮件服务器,用来接收电子邮件的SMTP 的全称是“Simple Mail Transfer Protocol”,即简单邮件传输协议。它是一组用于从源地址到目的地址传输邮件的规范,通过它来控制邮件的中转方式。SMTP 协议属于 TCP/IP 协议簇,它帮助每台计算机在发送或中转信件时找到下一个目的地。SMTP 服务器就是遵循 SMTP 协议的发送邮件服务器。   SMTP 认证,简单地说就是要求必须在提供了账户名和密码之后才可以登录 SMTP 服务器,这就使得那些垃圾邮件的散播者无可乘之机。。【开发者如何进行快速开发邮件发送系统???本课程系统进行快速研发,项目实战】 部分截图如下:完整版请查看课件或者视频
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值