写在前面的话:
由于需要部署服务到阿里云ESC服务器,里面涉及到一个发送邮件的功能,在本地我用的是一般发送方式(163邮箱),端口号25
但是,阿里把这个端口关闭了,所以需要换其他端口,阿里建议用ssl方式发送,并使用相应的端口,我就用465端口号,但是代码还是要调整的,需要发送ssl方式
原有的参数配置:
JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
javaMailSender.setHost(mailConfig.getSmtp());
Properties javaMailProperties = new Properties();
javaMailProperties.put("mail.smtp.auth", "true");
if(mailConfig.getTimeout() > 0)
{
javaMailProperties.put("mail.smtp.connectiontimeout", mailConfig.getTimeout() * 1000);
javaMailProperties.put("mail.smtp.timeout", mailConfig.getTimeout() * 1000);
}else
{
javaMailProperties.put("mail.smtp.connectiontimeout", MailConfig.TIMEOUT);
javaMailProperties.put("mail.smtp.timeout", MailConfig.TIMEOUT);
}
javaMailSender.setJavaMailProperties(javaMailProperties);
javaMailSender.setDefaultEncoding("GBK");
javaMailSender.setPort(mailConfig.getServerPort());
javaMailSender.setUsername(mailConfig.getUserName());
javaMailSender.setPassword(mailConfig.getPassword());
修改后的参数配置:
JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
javaMailSender.setHost(mailConfig.getSmtp());
Properties javaMailProperties = new Properties();
javaMailProperties.put("mail.smtp.auth", "true");
if(mailConfig.getTimeout() > 0)
{
javaMailProperties.put("mail.smtp.connectiontimeout", mailConfig.getTimeout() * 1000);
javaMailProperties.put("mail.smtp.timeout", mailConfig.getTimeout() * 1000);
}else
{
javaMailProperties.put("mail.smtp.connectiontimeout", MailConfig.TIMEOUT);
javaMailProperties.put("mail.smtp.timeout", MailConfig.TIMEOUT);
}
//加认证机制
javaMailProperties.put("mail.smtp.starttls.enable", true);
javaMailProperties.put("mail.transport.protocol", "smtps"); // 启用smtps方式
javaMailSender.setJavaMailProperties(javaMailProperties);
javaMailSender.setDefaultEncoding("GBK");
javaMailSender.setPort(mailConfig.getServerPort());
javaMailSender.setUsername(mailConfig.getUserName());
javaMailSender.setPassword(mailConfig.getPassword());
javaMailSender.setProtocol("smtps");// 发送协议smtps