背景: 之前的是用的http通道,所以没有mail.smtp.socketFactory.class那个属性。但是切换https后就发送失败了。所以https通道,要加上那个属性,否则发送会失败:
@Bean
public JavaMailSender innerJavaMailSender() {
JavaMailSenderImpl jms=new JavaMailSenderImpl();
jms.setHost(innerEmailProperties.getHost());
jms.setUsername(innerEmailProperties.getUsername());
jms.setPassword(innerEmailProperties.getPassword());
Properties javaMailProperties = new Properties();
javaMailProperties.setProperty("mail.smtp.auth", innerEmailProperties.getSmtpAuth());
javaMailProperties.setProperty("mail.transport.protocol", innerEmailProperties.getTransportProtocol());
//端口为465因为使用的https,所以这个属性必须要加上,否则发送不成功;
javaMailProperties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
// 设置端口为465
javaMailProperties.setProperty("mail.smtp.port", "465");
// 将属性应用
jms.setJavaMailProperties(javaMailProperties);
return jms;
}