之前在本地windows上用javax-mail写发送邮件时运行的好好的,
但是程序部署到Linux服务器上后发送邮件就会报如下错误:

Could not connect to SMTP host: smtp.qq.com, port: 25

我还以为是防火墙的问题,折腾了半天,把防火墙关了也没弄好。

之后看了别人的文章才知道阿里禁用了服务器的25端口:用户对阿里的投诉,以及阿里的回复。

那么问题来了SMTP协议的端口就是25,那该怎么办呢?

阿里的回复中给出了两种解决办法:
1、使用加密465端口 2、使用阿里的邮箱

 

我这里是用的465端口: Properties properties = System.getProperties(); //使用465端口和25端口就只有配置文件不一样 properties.put(“mail.smtp.socketFactory.class”, “javax.net.ssl.SSLSocketFactory”); properties.put(“mail.smtp.port”, “465”); properties.put(“mail.smtp.socketFactory.port”, “465”); properties.put(“mail.smtp.host”, “smtp.qq.com”); properties.put(“mail.smtp.auth”, “true”); properties.put(“mail.user”, “1223814404@qq.com”); properties.put(“mail.password”, “填你邮箱的密钥”); //smtp邮箱需要验证 Authenticator authenticator = new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { String userName = properties.getProperty(“mail.user”); String password = properties.getProperty(“mail.password”); return new PasswordAuthentication(userName, password); } }; Session session = Session.getDefaultInstance(properties, authenticator); MimeMessage mimeMessage = new MimeMessage(session); try { mimeMessage.setFrom(new InternetAddress(emailConfig.user)); mimeMessage.setRecipients(MimeMessage.RecipientType.TO, new Address[]{new InternetAddress(email)}); mimeMessage.setSubject(emailConfig.subject); mimeMessage.setText(String.format(“验证码:%s”, code), “utf-8”); Transport.send(mimeMessage); return ResultVoUtil.success(); } catch (MessagingException e) { e.printStackTrace(); }