Java郵件(問題解決)

凌浩雨發表於2017-09-12

Java 郵件(問題)

  1. 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();  
        }
  1. 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郵箱授權碼獲取連結


相關文章