从 gmail 读取电子邮件不起作用

Reading email from gmail is not working

我正在使用 Java Mail API 以及以下代码从我的 gmail 帐户读取电子邮件。

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 CheckMails {

   public static void check(String host, String storeType, String user,
      String password) 
   {
      try {
      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 = "myemai@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 CheckMails.check(CheckMails.java:26)
at CheckMails.main(CheckMails.java:66)

但是我在我的收件箱中收到了一封来自 gmail 的电子邮件,上面写着 "We recently blocked-in a sign in attempt to your Google Account"。 我怎样才能使程序正常工作?

将您的 emailSession 变量更改为

Session emailSession = Session.getInstance(props, new javax.mail.Authenticator() {
    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(userName, password);
    }
});

正如 Gmail 所说

Some apps and devices use less secure sign-in technology, which makes your account more vulnerable. You can turn off access for these apps, which we recommend, or turn on access if you want to use them despite the risks. Learn more

只需单击下方 link 并禁用 Gmail 安全性 settings.It 即可。

Disable Security settings

这是一篇关于 Password Authentication in Java Mail Api

的好文章