通过gmail smtp在android中发送邮件时需要特定于应用程序的密码

时间:2014-09-30 10:53:25

标签: java android email

您好我正在我的Android应用程序中发送电子邮件,这里是我发送邮件的代码:

    static {   
    Security.addProvider(new JSSEProvider());   
}  

    // Recipient's email ID needs to be mentioned.
      String to = "abc@xyz.com";

      // Sender's email ID needs to be mentioned
      String from = "abc@def.com";

      // Assuming you are sending email from localhost
      String host = "smtp.gmail.com";

      // Get system properties
      Properties properties = System.getProperties();

      // Setup mail server
      properties.setProperty("mail.smtp.host", host);
      properties.put("mail.smtp.starttls.enable", "true");
      properties.setProperty("mail.user", "abc@xyz.com");
      properties.setProperty("mail.password", "xyz");
      properties.put("mail.debug", "true");

      // Get the default Session object.
      Session session = Session.getDefaultInstance(properties);

      try{
         // Create a default MimeMessage object.
         MimeMessage message = new MimeMessage(session);

         // Set From: header field of the header.
         message.setFrom(new InternetAddress(from));

         // Set To: header field of the header.
         message.addRecipient(Message.RecipientType.TO,
                                  new InternetAddress(to));

         // Set Subject: header field
         message.setSubject("This is the Subject Line!");

         // Now set the actual message
         message.setText("This is actual message");

         // Send message
         Transport t = session.getTransport("smtps");
         try {
             t.connect(host, "abc@xyz.com", "xyz");
             t.sendMessage(message, message.getAllRecipients());
         } finally {
             t.close();
         }
         System.out.println("Sent message successfully....");
      }catch (MessagingException mex) {
         mex.printStackTrace();
      }

当我尝试发送电子邮件时,我还添加了JSSEProvider类作为安全提供程序,我在调试跟踪中遇到以下错误:534-5.7.9需要特定于应用程序的密码。了解更多信息 534 5.7.9 http://support.google.com/accounts/bin/answer.py?answer=185833 fr7sm14810157pdb.79 - gsmtp javax.mail.AuthenticationFailedException我的用户名和密码是正确的我仍然得到同样的例外,请帮助我!

0 个答案:

没有答案