簡體   English   中英

無法使用 Java Mail API 發送郵件 [使用 Gmail]

[英]Unable to send Mails using Java Mail API [Using Gmail]

我使用了以下代碼,該代碼在 2014 年運行良好,但目前無法正常工作。

此代碼中使用的憑據也是正確的。

public class SendMail 
{  
public void SendMailToTheUserWhoHaveForgotThePassword(String MailTo,String Password)
  {      
      String to = MailTo;  
      String from = "chatna06062016@gmail.com";
      final String username = "chatna06062016";     
      final String password = "xxxxxxxx";  

      String host = "smtp.gmail.com";  
      Properties props = new Properties();
      props.put("mail.smtp.auth", "true");
      props.put("mail.smtp.starttls.enable", "true"); 
      props.put("mail.smtp.host", host);   
      props.put("mail.smtp.port", "25");  
      Session session = Session.getInstance(props,new javax.mail.Authenticator() 
                    {             
                        protected PasswordAuthentication getPasswordAuthentication() 
                        {                
                            return new PasswordAuthentication(username, password);     
                        }          
                    }
                                            );  

      try 
      {   
          Message message = new MimeMessage(session);            
          message.setFrom(new InternetAddress(from));       
          message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to));        
          message.setSubject("FORGOTTEN PASSWORD"); 


          message.setText("Dear User The Password that you have forgotten is <b>"+Password +"</b>"+ 
                "This email is sended you by using JavaMailAPI "
                  + "HAVE A NICE DAY"
                  + "DO USE THIS SERVICE WHENEVER YOU NEED IT");  

    Transport.send(message);  
      System.out.println("Sent message successfully...."); 

      }
      catch (MessagingException e) { 
         throw new RuntimeException(e); 
      }

       }

} 

我收到了來自谷歌的電子郵件,內容涉及上述課程中使用的方法的使用。

從谷歌收到的電子郵件

Hi ChatNa,
Someone just tried to sign in to your Google Account chatna06062016@gmail.com from an app that doesn't meet modern security standards.
    Details:
Sunday, June 26, 2016 12:57 PM (India Standard Time)
Noida, Uttar Pradesh, India*
We strongly recommend that you use a secure app, like Gmail, to access your account. All apps made by Google meet these security standards. Using a less secure app, on the other hand, could leave your account vulnerable. Learn more.

Google stopped this sign-in attempt, but you should review your recently used devices:

現在該怎么辦 無法在任何地方找到任何有用的信息。

您可能需要在 Gmail 帳戶中進行以下設置:

轉到:我的帳戶 -> 登錄和安全 -> 連接的應用程序和站點 -> 允許不太安全的應用程序:開啟

這里的主要問題是您使用用戶登錄名和密碼直接登錄到 SMTP/IMAP 郵件服務器。 這是不安全的。

您應該考慮切換到使用XOauth2,然后您可以驗證您的應用程序,它不再被視為不安全。

XOAUTH2 機制允許客戶端向服務器發送 OAuth 2.0 訪問令牌。 該協議使用以下部分中顯示的編碼值。

    [connection begins]
    C: C01 CAPABILITY
    S: * CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA XLIST
    CHILDREN XYZZY SASL-IR AUTH=XOAUTH2 AUTH=XOAUTH
    S: C01 OK Completed
    C: A01 AUTHENTICATE XOAUTH2 dXNlcj1zb21ldXNlckBleGFtcGxlLmNvb
    QFhdXRoPUJlYXJlciB5YTI5LnZGOWRmdDRxbVRjMk52YjNSbGNrQmhkSFJoZG
    1semRHRXVZMjl0Q2cBAQ==
    S: A01 OK Success
    [connection continues...]

其他人提到的選項編號只啟用不太安全的應用程序。 您可以在此處閱讀所有相關信息不太安全的應用程序和您的 Google 帳戶以及與啟用它相關的危險。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM