无法使用Java Mail API发送邮件[使用Gmail]

时间:2016-06-26 07:58:17

标签: java api email web 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); 
      }

       }

} 

我收到了谷歌这样的关于上述课程中使用的方法的电子邮件。

Email Received from Google

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:
  
    
      
        
          

现在该做什么,无法在任何地方找到任何有用的信息。

        
      
    
  

2 个答案:

答案 0 :(得分:1)

您可能需要在Gmail帐户中进行以下设置:

转到: myaccount - &gt;签到&amp;安全 - &gt;连接的应用程序&amp;网站 - &gt;允许安全性较低的应用:开启

答案 1 :(得分:0)

这里的主要问题是您使用用户的登录名和密码直接登录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...]

其他人提到的选项编号只需启用安全性较低的应用程序。您可以在Less secure apps & your Google Account上阅读有关它的所有信息,以及与启用它相关的危险。