javax.mail.MessagingException: Could not connect to SMTP host: smtp.qq.com, port: 465, response: -1

使用465端口发送邮件时报错

javax.mail.MessagingException: Could not connect to SMTP host: smtp.qq.com, port: 465, response: -1
	at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2042)
	at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:697)
	at javax.mail.Service.connect(Service.java:366)
	at javax.mail.Service.connect(Service.java:246)
	at com.capgemini.tools.emailTest.sendMail(emailTest.java:273)
	at com.capgemini.tools.emailTest.main(emailTest.java:313)

原因是465端口需要配置

Properties properties = new Properties();

		properties.put("mail.smtp.host", smtpServer);
		properties.put("mail.smtp.auth", "true");
		properties.put("mail.smtp.socketFactory.class", SSL_FACTORY); // 使用JSSE的SSL
																		// socketfactory来取代默认的socketfactory
		properties.put("mail.smtp.socketFactory.fallback", "false"); // 只处理SSL的连接,对于非SSL的连接不做处理
		properties.put("mail.smtp.port", port);
		properties.put("mail.smtp.socketFactory.port", port);
		Session session = Session.getInstance(properties);

除此之外还要配置一个

        properties.put("mail.smtp.ssl.enable", true);

之后成功

你可能感兴趣的:(Java,web)