Java 邮件(问题)
- A secure connection is requiered(such as ssl).
问题详情:
javax.mail.AuthenticationFailedException: 530 Error: A secure connection is requiered(such as ssl). More information at http://service.mail.qq.com/cgi-bin/help?id=28
at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:826)
at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:761)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:685)
at javax.mail.Service.connect(Service.java:317)
at javax.mail.Service.connect(Service.java:176)
at javax.mail.Service.connect(Service.java:125)
at javax.mail.Transport.send0(Transport.java:194)
at javax.mail.Transport.send(Transport.java:124)
at com.mazaiting.mail.SimpleMailServer.sendTextMail(SimpleMailServer.java:59)
at com.mazaiting.Client.main(Client.java:22)
原因
QQ邮箱需要证书认证。
解决方法
在源代码中的Properties配置时加入
try {
// 进行证书认证
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
properties.put("mail.smtp.ssl.enable", "true");
properties.put("mail.smtp.ssl.socketFactory", sf);
} catch (GeneralSecurityException e) {
e.printStackTrace();
}
- Could not connect to SMTP host: smtp.qq.com, port: 25
问题详情:
javax.mail.MessagingException: Could not connect to SMTP host: smtp.qq.com, port: 25;
nested exception is:
javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1961)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:654)
at javax.mail.Service.connect(Service.java:317)
at javax.mail.Service.connect(Service.java:176)
at javax.mail.Service.connect(Service.java:125)
at javax.mail.Transport.send0(Transport.java:194)
at javax.mail.Transport.send(Transport.java:124)
at com.mazaiting.mail.SimpleMailServer.sendTextMail(SimpleMailServer.java:59)
at com.mazaiting.Client.main(Client.java:22)
Caused by: javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?
at sun.security.ssl.InputRecord.handleUnknownRecord(Unknown Source)
at sun.security.ssl.InputRecord.read(Unknown Source)
at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
at com.sun.mail.util.SocketFetcher.configureSSLSocket(SocketFetcher.java:549)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:354)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:237)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1927)
译文:无法连接到SMTP主机:smtp.qq.com,端口:25;
原因分析:
- 邮件服务器地址
- 邮件服务器端口后
- 账号
- 密码,不一定时登录密码可能时授权码
- QQ邮箱过程中出现错误,如果程序之前也可以运行成功的话,可以再次生成授权码,有这个原因的。还是不可以的话就请等一会,服务器也需要时间同步。
以上前四步骤存在错误,因为不同类型邮箱而不同,所以只要我们根据各个邮箱的规则去写。
QQ邮箱授权码获取链接