集册 Java实例教程 检查邮件

检查邮件

—— 检查E

欢马劈雪     最近更新时间:2020-01-02 10:19:05

540
检查电子邮件

import java.io.IOException;

import java.util.Properties;


import javax.mail.Flags;
/*来自 
 NowJava.com - 时  代  Java*/

import javax.mail.Folder;

import javax.mail.Message;

import javax.mail.MessagingException;

import javax.mail.Session;

import javax.mail.Store;


public class Main {

  public static void main(String[] args) {

    Properties properties = new Properties();

    String username = "username";

    String password = "password";/* from 时代Java公众号*/

    String folder = "Inbox";

    String host = "imap.host.com";


    try {

      Session session = Session.getDefaultInstance(properties, null);

      Store store = session.getStore("imap");

      store.connect(host, username, password);

      System.out.println(store);

      Folder inbox = store.getFolder(folder);

      inbox.open(Folder.READ_WRITE);

      int messageCount = inbox.getMessageCount();

      int startMessage = messageCount - 10;

      if (startMessage < 1)

        startMessage = 1;

      Message messages[] = inbox.getMessages(startMessage, messageCount);

      for (Message message : messages) {

        boolean hasBeenRead = false;

        for (Flags.Flag flag : message.getFlags().getSystemFlags()) {

          if (flag == Flags.Flag.SEEN) {

            hasBeenRead = true;

            break;

          }

        }

        message.setFlag(Flags.Flag.S
展开阅读全文