java - 通过Java每10分钟轮询一次邮箱

标签 java javamail pop3 timertask

我有下面的程序充当邮箱侦听器,它是用POP3协议配置的,执行该程序时,它已连接到邮箱并下载了指定文件夹中的所有邮件和附件,现在我的查询是我如何才能以以下方式修改我的以下程序,使其应继续轮询邮箱列表器,每10分钟说一次并下载所有邮件,因此应每10分钟进行一次轮询,请告知我如何修改我的以下程序为达到这个..

import java.io.*;
import java.util.*;
import javax.mail.*;

import javax.mail.internet.MimeBodyPart;

public class ReceiveMailPOP3 {
  private static final String HOST = "pop.gmail.com";
  private static final String USERNAME = "myemail@gmail.com";
  private static final String PASSWORD = "******";

  public static void doit() throws MessagingException, IOException {
    Folder folder = null;
    Store store = null;
    try {
      Properties props = new Properties();
      props.put("mail.store.protocol", "pop3s"); // Google uses POP3S not POP3
      Session session = Session.getDefaultInstance(props);
      // session.setDebug(true);
      store = session.getStore();
      store.connect(HOST, USERNAME, PASSWORD);
      folder = store.getDefaultFolder().getFolder("INBOX");
      folder.open(Folder.READ_ONLY);
      Message[] messages = folder.getMessages();
      System.out.println("No of Messages : " + folder.getMessageCount());
      System.out.println("No of Unread Messages : " + folder.getUnreadMessageCount());
      for (int i=0; i < messages.length; ++i) {
        System.out.println("MESSAGE #" + (i + 1) + ":");
        Message msg = messages[i];
        String from = "unknown";
        if (msg.getReplyTo().length >= 1) {
          from = msg.getReplyTo()[0].toString();
        }
        else if (msg.getFrom().length >= 1) {
          from = msg.getFrom()[0].toString();
        }
        String subject = msg.getSubject();
        System.out.println("Saving ... " + subject +" " + from);
        // you may want to replace the spaces with "_"
        // the files will be saved into the TEMP directory
        String filename = "c:/temp/" +  subject;
        saveParts(msg.getContent(), filename);
      }
    }
    finally {
      if (folder != null) { folder.close(true); }
      if (store != null) { store.close(); }
    }
  }

  public static void saveParts(Object content, String filename)
  throws IOException, MessagingException
  {
    OutputStream out = null;
    InputStream in = null;
    try {
      if (content instanceof Multipart) {
        Multipart multi = ((Multipart)content);
        int parts = multi.getCount();
        for (int j=0; j < parts; ++j) {
          MimeBodyPart part = (MimeBodyPart)multi.getBodyPart(j);
          if (part.getContent() instanceof Multipart) {
            // part-within-a-part, do some recursion...
            saveParts(part.getContent(), filename);
          }
          else {
            String extension = "";
            if (part.isMimeType("text/html")) {
              extension = "html";
            }
            else {
              if (part.isMimeType("text/plain")) {
                extension = "txt";
              }
              else {
                //  Try to get the name of the attachment
                extension = part.getDataHandler().getName();
              }
              filename = filename + "." + extension;
              System.out.println("... " + filename);
              out = new FileOutputStream(new File(filename));
              in = part.getInputStream();
              int k;
              while ((k = in.read()) != -1) {
                out.write(k);
              }
            }
          }
        }
      }
    }
    finally {
      if (in != null) { in.close(); }
      if (out != null) { out.flush(); out.close(); }
    }
  }

  public static void main(String args[]) throws Exception {
    ReceiveMailPOP3.doit();
  }
} 

最佳答案

您可以使用Timer。通过使用Timer,您可以定期安排任务。

  Timer timer = new Timer();
  long interval = (10*60*1000) ; // 10 minutes

  timer.schedule( new TimerTask() {
       public void run() {
          ReceiveMailPOP3.doit();
       }
  }, 0, interval); 


如果要停止调度,可以使用timer.cancel();

还有一点值得注意:如果您的ReceiveMailPOP3.doit();12:00开始并需要9分钟的时间来执行,则ReceiveMailPOP3.doit();的下一次执行将是12:19,因为timer将等待interval数量每次执行之间的时间间隔。如果您想在10分钟后执行而不关心先前的执行,则必须使用timer.scheduleAtFixedRate

关于java - 通过Java每10分钟轮询一次邮箱,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31353806/

相关文章:

java - 无法获取 oracle-java8-installer 的 Java 包

java - 在 Swing GUI 中处理异常

java - 在Java中以Outlook邮件格式保存邮件

java - 使用 pop3 按日期降序获取邮件

java - 如何在java中使用imap或pop3获取特定数量的电子邮件?

c# - 适用于 Android 和 Windows Phone 7 的脚本语言

java - 如何在@PostConstruct之前调用@BeforeMethod block

java - 通过电子邮件错误以Java发送SMS文本

java - Javamail-android商用

email - 您能确定一封电子邮件是否仅从电子邮件标题中包含附件吗