Get All Message On Server : Email Server « Email « Java Tutorial






import java.util.Properties;

import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.URLName;

public class MainClass {

  public static void main(String[] args) throws Exception {
    URLName server = new URLName("protocol://username:password@host/foldername");

    Session session = Session.getDefaultInstance(new Properties(), null);

    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++) {
      System.out.println("------------ Message " + (i + 1) + " ------------");
      messages[i].writeTo(System.out);
    }

    folder.close(false);
  }
}
  Download:  JavaMailClientGetAllMessageOnServer.zip( 2,325 k)








30.7.Email Server
30.7.1.Get All Message On Server
30.7.2.Search the given folder for messages matching the given criteria
30.7.3.List information about folders
30.7.4.Copy a specified number of messages from one folder to another folder
30.7.5.Copy folder hierarchies between different Stores
30.7.6.MOVE messages between mailboxes
30.7.7.Show the namespaces supported by a store