使用Java中的JavaMail Api从gmail中反复读取邮件

时间:2015-11-04 06:11:33

标签: java gmail-api

我正在使用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 = "your_email@gmail.com";// 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();
      }
   }
} 

在这段代码中我使用pop服务器来阅读电子邮件,现在我在其中提供电子邮件和密码并运行。它运行正常但它只读了一次电子邮件,下次如果我运行程序,套件给我的消息数等于0 ... 我想多次一次又一次地阅读消息......     任何帮助将不胜感激......

2 个答案:

答案 0 :(得分:4)

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

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

IMAP.gmail.com

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

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

答案 1 :(得分:2)

Gmail具有用于处理pop3邮件请求的用户设置。我遇到了同样的问题,并最终查看了以下页面:https://javaee.github.io/javamail/FAQ#gmailsettings

要每次查看所有电子邮件,您需要启用该标志(在设置页面中):对所有邮件(甚至是已下载的邮件)启用POP

帮了我大忙。希望能有所帮助。