【问题标题】:Cant use authentiacate using Javamail unless "Access for less secured apps " enabled除非启用“访问不太安全的应用程序”,否则无法使用 Javamail 进行身份验证
【发布时间】:2014-12-07 06:39:53
【问题描述】:

我正在编写一个简单的 java 程序,它可以检查我的 gmail 收件箱。我按照 http://www.tutorialspoint.com/javamail_api/javamail_api_checking_emails.html 的教程进行操作。但是当我尝试运行这个程序时

import java.util.Properties;

import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Store;

public class CheckingMails {

   public static void check(String host, String storeType, String user,
      String password) 
   {
      try {

      //create properties field
      Properties properties = new Properties();

      properties.put("mail.pop3.host", host);
      properties.put("mail.pop3.port", "995");
      properties.put("mail.pop3.starttls.enable", "true");
      Session emailSession = Session.getDefaultInstance(properties);

      //create the POP3 store object and connect with the pop server
      Store store = emailSession.getStore("pop3s");

      store.connect(host, user, password);

      //create the folder object and open it
      Folder emailFolder = store.getFolder("INBOX");
      emailFolder.open(Folder.READ_ONLY);

      // retrieve the messages from the folder in an array and print it
      Message[] messages = emailFolder.getMessages();
      System.out.println("messages.length---" + messages.length);

      for (int i = 0, n = messages.length; i < n; i++) {
         Message message = messages[i];
         System.out.println("---------------------------------");
         System.out.println("Email Number " + (i + 1));
         System.out.println("Subject: " + message.getSubject());
         System.out.println("From: " + message.getFrom()[0]);
         System.out.println("Text: " + message.getContent().toString());

      }

      //close the store and folder objects
      emailFolder.close(false);
      store.close();

      } catch (NoSuchProviderException e) {
         e.printStackTrace();
      } catch (MessagingException e) {
         e.printStackTrace();
      } catch (Exception e) {
         e.printStackTrace();
      }
   }

   public static void main(String[] args) {

      String host = "pop.gmail.com";// change accordingly
      String mailStoreType = "pop3";
      String username = "yourmail@gmail.com";// change accordingly
      String password = "*****";// change accordingly

      check(host, mailStoreType, username, password);

   }

}

我收到以下错误

javax.mail.AuthenticationFailedException: [AUTH] Web login required: https://support.google.com/mail/bin/answer.py?answer=78754
    at com.sun.mail.pop3.POP3Store.protocolConnect(POP3Store.java:209)
    at javax.mail.Service.connect(Service.java:364)
    at javax.mail.Service.connect(Service.java:245)
    at CheckingMails.check(CheckingMails.java:38)
    at CheckingMails.main(CheckingMails.java:78)

但是,当我在我的 gmail 帐户中启用“访问安全性较低的应用程序”时,效果很好。我该如何解决这个问题。 这个问题类似于Can send emails through Gmail account only if account has "Access for less secure apps" enabled 但那里没有答案!

【问题讨论】:

  • 您在错误消息第一行提供的支持链接中发现了什么?

标签: java email smtp imap pop3


【解决方案1】:

“安全性较低的应用”是指不使用 OAuth2 的连接。
因为它们存储了您的密码,所以它们的安全性较低。

您可以切换到 AOuth2 或允许该访问。

【讨论】:

  • 但是如果我编写一个应用程序来检查我拥有的所有邮件的收件箱,我是否必须更改我在应用程序中提供的所有邮件的设置?我不能在程序中进行任何更改吗?它会变得更加安全吗?
  • 是的;切换到 IMAP 并使用 OAuth2。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-01
  • 1970-01-01
  • 2011-06-27
  • 1970-01-01
相关资源
最近更新 更多