Java Tutorial/Email/Email Flags

Материал из Java эксперт
Версия от 15:23, 31 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Java Mail Flags

import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Flags;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.URLName;
import javax.mail.internet.InternetAddress;
public class MainClass {
  public static void main(String[] args) throws Exception {
    URLName server = new URLName("protocol://username@host/foldername");
    Session session = Session.getDefaultInstance(new Properties(), new MailAuthenticator());
    Folder folder = session.getFolder(server);
    if (folder == null) {
      System.out.println("Folder " + server.getFile() + " not found.");
      System.exit(1);
    }
    folder.open(Folder.READ_ONLY);
    // Get the messages from the server
    Message[] messages = folder.getMessages();
    for (int i = 0; i < messages.length; i++) {
      // Get the headers
      System.out.println("From: " + InternetAddress.toString(messages[i].getFrom()));
      System.out.println("Reply-to: " + InternetAddress.toString(messages[i].getReplyTo()));
      System.out.println("To: " + InternetAddress.toString(messages[i].getRecipients(Message.RecipientType.TO)));
      System.out.println("Cc: " + InternetAddress.toString(messages[i].getRecipients(Message.RecipientType.CC)));
      System.out.println("Bcc: " + InternetAddress.toString(messages[i].getRecipients(Message.RecipientType.BCC)));
      System.out.println("Subject: " + messages[i].getSubject());
      System.out.println("Sent: " + messages[i].getSentDate());
      System.out.println("Received: " + messages[i].getReceivedDate());
      if (messages[i].isSet(Flags.Flag.DELETED)) {
        System.out.println("Deleted");
      }
      if (messages[i].isSet(Flags.Flag.ANSWERED)) {
        System.out.println("Answered");
      }
      if (messages[i].isSet(Flags.Flag.DRAFT)) {
        System.out.println("Draft");
      }
      if (messages[i].isSet(Flags.Flag.FLAGGED)) {
        System.out.println("Marked");
      }
      if (messages[i].isSet(Flags.Flag.RECENT)) {
        System.out.println("Recent");
      }
      if (messages[i].isSet(Flags.Flag.SEEN)) {
        System.out.println("Read");
      }
      if (messages[i].isSet(Flags.Flag.USER)) {
        // We don"t know what the user flags might be in advance
        // so they"re returned as an array of strings
        String[] userFlags = messages[i].getFlags().getUserFlags();
        for (int j = 0; j < userFlags.length; j++) {
          System.out.println("User flag: " + userFlags[j]);
        }
      }
    }
    folder.close(false);
  }
}
class MailAuthenticator extends Authenticator {
  public MailAuthenticator() {
  }
  public PasswordAuthentication getPasswordAuthentication() {
    return new PasswordAuthentication("username", "password");
  }
}