1、为什么做这个?
如果需要使用Gmail作为应用程序的邮件发送者,必须进行相关配置,否则不能直接使用
2、怎么做?
谷歌邮箱(@gmail.com):两步验证+应用专用密码登录 - 来发信 - 您的外贸拓客好帮手
2.1、登录谷歌账号:https://myaccount.google.com/u/6/signinoptions/two-step-verification/enroll-welcome?gar=1
2.2、应用专用密码设置入口:https://myaccount.google.com/u/6/apppasswords?gar=1
3、java 应用中如何使用Gmail可以参考如下:
SPRING BOOT发送邮件验证码(Gmail邮箱)_spring-boot-starter-mail gmail配置-CSDN博客
4、如何支持多个发送邮箱配置
4.1、定义一个配置类
import cn.hutool.core.text.CharSequenceUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import java.util.Properties;
/**
* @author : qinjinyuan
* @desc : TODO 请填写你的功能描述
* @date : 2024/06/03 13:36
*/
@Configuration
public class EmailSenderConfig {
@Value("${gmail.host}")
private String gmailHost;
@Value("${gmail.port}")
private Integer gmailPort;
@Value("${gmail.username}")
private String gmailUsername;
@Value("${gmail.password}")
private String gmailPassword;
@Value("${gmail.protocol}")
private String gmailProtocol;
@Autowired
private EmailNormalConfig emailNormalConfig;
@Bean("gmailSender")
public JavaMailSenderImpl gmailSender() {
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
mailSender.setHost(gmailHost);
mailSender.setPort(gmailPort);
mailSender.setUsername(gmailUsername);
mailSender.setPassword(gmailPassword);
mailSender.setProtocol(gmailProtocol);
Properties properties = new Properties();
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.starttls.enable", "true");
mailSender.setJavaMailProperties(properties);
return mailSender;
}
@Bean("otherSender")
public JavaMailSenderImpl otherSender() {
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
mailSender.setHost(emailNormalConfig.getHost());
mailSender.setPort(emailNormalConfig.getPort());
mailSender.setUsername(emailNormalConfig.getUsername());
mailSender.setPassword(emailNormalConfig.getPassword());
if (CharSequenceUtil.isNotBlank(emailNormalConfig.getProtocol())) {
mailSender.setProtocol(emailNormalConfig.getProtocol());
}
return mailSender;
}
4.2、定义原生配置 EmailNormalConfig
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Configuration;
/**
* @author : qinjinyuan
* @date : 2023/10/31 16:51
*/
@Configuration
@RefreshScope
@ConfigurationProperties(prefix = "spring.mail")
@Getter
@Setter
public class EmailNormalConfig {
private String host;
private Integer port;
private String username;
private String password;
private String from;
private String protocol;
}
4.3、如何用,也可以自己写策略模式进行实现
import org.springframework.mail.javamail.JavaMailSender;
@Resource(name = "otherSender")
private JavaMailSender mailSender;
或者
@Resource(name = "gmailSender")
private JavaMailSender mailSender;
4.4、设计类图
小结:
主要描述gmail 作为客户端发送者,关键性步骤的申请和设置 (找了好长时间,才找到具体的设置页面,官网都没有直接的入口);
另外分享了如何进行多个邮箱发送者之间的切换及设计思路。