2016-06-21 80 views
1

我正在製作一個可以發送電子郵件的web應用程序,在此代碼中我使用yahoo郵件發送。javax.mail.AuthenticationFailedException:如何通過java發送郵件失敗?

我嘗試了一些解決方案,但他們沒有我多少:

  1. Getting the "javax.mail.AuthenticationFailedException: failed to connect" Error
  2. javax.mail.AuthenticationFailedException: failed to connect, no password specified?

這裏是我的代碼:

try { 

     Properties props = new Properties(); 
     props.put("mail.smtp.host", "smtp.mail.yahoo.com"); // for gmail use smtp.gmail.com 
     props.put("mail.smtp.auth", "true"); 
     props.put("mail.debug", "true"); 
     props.put("mail.smtp.starttls.enable", "true"); 
     props.put("mail.smtp.port", "465"); 
     props.put("mail.smtp.socketFactory.port", "465"); 
     props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); 
     props.put("mail.smtp.socketFactory.fallback", "false"); 

     Session mailSession = Session.getInstance(props, new javax.mail.Authenticator() { 

      protected PasswordAuthentication getPasswordAuthentication() { 
       return new PasswordAuthentication("[email protected]", "my_pwd"); 
      } 
     }); 

     mailSession.setDebug(true); // Enable the debug mode 

     Message msg = new MimeMessage(mailSession); 

     //--[ Set the FROM, TO, DATE and SUBJECT fields 
     msg.setFrom(new InternetAddress("[email protected]")); 
     msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse("[email protected]")); 
     msg.setSentDate(new Date()); 
     msg.setSubject("Hello World!"); 

     //--[ Create the body of the mail 
     msg.setText("Hello from my first e-mail sent with JavaMail"); 

     //--[ Ask the Transport class to send our mail message 
     Transport.send(msg); 
     return true; 

    } catch (Exception E) { 
     System.out.println("Oops something has gone pearshaped!"); 
     System.out.println(E); 
     return false; 
    } 

這裏是調試代碼:

DEBUG: JavaMail version 1.4.2 
DEBUG: successfully loaded resource: /META-INF/javamail.default.providers 
DEBUG: Tables of loaded providers 
DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun Microsystems, Inc], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc]} 
DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun Microsystems, Inc], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]} 
DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map 
DEBUG: setDebug: JavaMail version 1.4.2 
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc] 
DEBUG SMTP: useEhlo true, useAuth true 
DEBUG SMTP: useEhlo true, useAuth true 
DEBUG SMTP: trying to connect to host "smtp.mail.yahoo.com", port 465, isSSL false 
220 smtp.mail.yahoo.com ESMTP ready 
DEBUG SMTP: connected to host "smtp.mail.yahoo.com", port: 465 

EHLO DESKTOP-132ABCD 
250-smtp.mail.yahoo.com 
250-PIPELINING 
250-SIZE 41697280 
250-8 BITMIME 
250 AUTH PLAIN LOGIN XOAUTH2 XYMCOOKIE 
DEBUG SMTP: Found extension "PIPELINING", arg "" 
DEBUG SMTP: Found extension "SIZE", arg "41697280" 
DEBUG SMTP: Found extension "8", arg "BITMIME" 
DEBUG SMTP: Found extension "AUTH", arg "PLAIN LOGIN XOAUTH2 XYMCOOKIE" 
DEBUG SMTP: Attempt to authenticate 
DEBUG SMTP: check mechanisms: LOGIN PLAIN DIGEST-MD5 
AUTH LOGIN 
//some code 
535 5.7.0 (#MBR1240) Please verify your account by going to https://login.yahoo.com 
Oops something has gone pearshaped! 
javax.mail.AuthenticationFailedException: failed to connect 

請幫助我,非常感謝!

+0

您正在使用的帳戶是否已通過驗證? –

+0

是的,我的帳戶是有效的,我正在使用它,發件人和收件人! – iamatsundere181

回答

5

我用你的java代碼發送電子郵件,而不是我用我的Gmail帳戶。 第一次嘗試時,我也得到了「javax.mail.AuthenticationFailedException:534-5.7.14」。

發生此異常是因爲谷歌關閉了訪問安全性較低的應用。之後,我去了這個link

在按照說明操作並打開安全性較低的應用程序後,我可以發送郵件。

由於您使用的是雅虎郵件,因此您可以嘗試使用link來啓用訪問安全性較低的應用,方法是啓用「允許使用安全性較低的登錄應用」選項。

+0

非常感謝你 – iamatsundere181

2

幾周前我遇到了與GMail相同的問題。事實證明,GMail不會允許您發送帶有「不太安全」應用程序的電子郵件,除非您在GMail的帳戶安全選項中激活它。
Yahoo!具有相同的安全標準,因此您需要訪問https://login.yahoo.com/account/security並啓用「允許使用安全性較低的應用登錄」,然後在該應用中使用該帳戶。