java 在java中使用JavaMail Api从gmail一次又一次地读取邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33514856/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 21:47:54  来源:igfitidea点击:

Read mails again and again from gmail using JavaMail Api in java

javagmail-api

提问by Khawaja M. Awais

I am using JavaMail Api to read mails from gmail account. But problem is that I can read it only once. Is there any way to read the mails again and again???
My Java Code is :

我正在使用 JavaMail Api 从 gmail 帐户读取邮件。但问题是我只能读一次。有什么办法可以一遍又一遍地阅读邮件吗???
我的 Java 代码是:

import java.util.Properties;
import javax.mail.BodyPart;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Store;

public class Main {

    // main function. Project run starts from main function...
   public static void main(String[] args) {

      String host = "pop.gmail.com";// change accordingly
      String mailStoreType = "pop3";
      String username = "[email protected]";// change accordingly
      String password = "your_password";// change accordingly

      check(host, mailStoreType, username, password);
   }


   // function to make connection and get mails from server known as "Pop" server
   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];


         Object obj = message.getContent();
         Multipart mp = (Multipart)obj;
         BodyPart bp = mp.getBodyPart(0);


         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("To: " + message.getAllRecipients().toString());
         System.out.println("Received Date:" + message.getReceivedDate());
         System.out.println("Text: " + bp.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();
      }
   }
} 

In this code I am using pop server to read emails from and now I give email and password in it and run. It works fine but it reads an email once only, next time if I run program , kit gives me number of messages equal to 0... I want to read messages again and again as many times as I want... Any help will be appreciated...

在这段代码中,我使用流行服务器读取电子邮件,现在我在其中提供电子邮件和密码并运行。它工作正常,但它只读取一次电子邮件,下次如果我运行程序,kit 给我的消息数等于 0...我想一次又一次地阅读消息,次数不限...任何帮助都会值得赞赏...

采纳答案by Awais Dar

If you want to get All Emails every time, IMAP sever will be best for it.

如果您想每次都收到所有电子邮件,IMAP 服务器将是最好的选择。

You can change the mail server to

您可以将邮件服务器更改为

IMAP.gmail.com

IMAP.gmail.com

and the port will be 993 (considering you are using gmail account).

端口将是 993(考虑到您使用的是 gmail 帐户)。

The Link sidgate provided will be best example for you.

提供的 Link sidgate 将是您的最佳示例。

回答by Thirdshift

Gmail has user settings for how pop3 mail requests are handled. I was having this same issue, and ended up checking out this page: https://javaee.github.io/javamail/FAQ#gmailsettings.

Gmail 有关于如何处理 pop3 邮件请求的用户设置。我遇到了同样的问题,最后查看了这个页面:https: //javaee.github.io/javamail/FAQ#gmailsettings

To see all email every time, you need to enable the flag (in the settings page): Enable POP for all mail (even mail that's already been downloaded)

要每次查看所有电子邮件,您需要启用该标志(在设置页面中):Enable POP for all mail(即使是已经下载的邮件)

Did the trick for me. Hope that helps.

对我有用。希望有帮助。