【问题标题】:Read mails again and again from gmail using JavaMail Api in java在java中使用JavaMail Api一次又一次地从gmail读取邮件
【发布时间】:2016-02-04 12:49:33
【问题描述】:

我正在使用 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... 我想一遍又一遍地阅读消息... 任何帮助将不胜感激...

【问题讨论】:

  • 有会话问题吗?
  • 我注意到它会使用 gmail APi 读取尚未读取的电子邮件。如果我收到新邮件,那么它正在阅读它,但在那之后我再次给出等于 0 的邮件数。

标签: java gmail-api


【解决方案1】:

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

你可以把邮件服务器改成

IMAP.gmail.com

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

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

【讨论】:

    【解决方案2】:

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

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

    为我做了诀窍。希望有帮助。

    【讨论】:

      猜你喜欢
      • 2013-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-22
      • 1970-01-01
      相关资源
      最近更新 更多