This section defines the syntax and lists the order in which a client application calls some JavaMail methods in order to access and open a message located in a folder:
1.A JavaMail client typically begins a mail handling task by obtaining a JavaMail Session object.
Session session = Session.getInstance(props, authenticator);
2.The client uses the Session object’s getStore method to connect to the default store. The getStore method returns a Store object subclass that supports the access protocol defined in the user properties object, which will typically contain per-user preferences.
Store store = session.getStore();
store.connect();
3.If the connection is successful, the client can list available folders in the Store, and then fetch and view specific Message objects.
// get the INBOX folder
Folder inbox = store.getFolder("INBOX");
// open the INBOX folder
inbox.open(Folder.READ_WRITE);
Message m = inbox.getMessage(1); // get Message # 1
String subject = m.getSubject(); // get Subject
Object content = m.getContent(); // get content
...
...
4.Finally, the client closes all open folders, and then closes the store.
inbox.close(); // Close the INBOX
store.close(); // Close the Store
5.example:
public class JavaMailSyntax {
@Test
public void testStore() {
Session session = Session.getInstance(this.getProperties(), this.getAuthenticator());
Store store = null;
POP3Folder inbox = null;
try {
store = session.getStore();
store.connect();
if ((inbox = (POP3Folder) store.getFolder("INBOX")).exists()) {
inbox.open(Folder.READ_WRITE);
Message msg = inbox.getMessage(3); // get Message #3
String subject = msg.getSubject(); // get Subject
Object content = ((Part) msg).getContent(); // get content
Address[] froms = msg.getFrom();
Address[] recipients = msg.getAllRecipients();
Date sentDate = msg.getSentDate();
System.out.println("getMessageNumber : " + msg.getMessageNumber());
System.out.println("subject : " + subject);
System.out.println("content : " + content.toString());
System.out.println("froms : " + Arrays.toString(froms));
System.out.println("recipients : " + Arrays.toString(recipients));
System.out.println("sentDate : " + sentDate.toString());
} // end if-condition
} catch (NoSuchProviderException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {// Close the INBOX
if (null != inbox) {
inbox.close(false);
}
} catch (MessagingException e) {
e.printStackTrace();
}
try {// Close the Store
if (null != store) {
store.close();
}
} catch (MessagingException e) {
e.printStackTrace();
}
}// end method-testStore
private Properties getProperties() {
Properties props = System.getProperties();
props.setProperty("mail.pop3.auth", "true");
props.setProperty("mail.store.protocol", "pop3");
props.setProperty("mail.host", "localhost");
return props;
}// end method-getProperties
private Authenticator getAuthenticator() {
return new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("admin", "admin");
}
};
}// end method-getAuthenticator
}// end class-JavaMailSyntax
/**possible output
getMessageNumber : 3
subject : subject-1146
content : javax.mail.internet.MimeMultipart@71bc1ae4
froms : [admin <admin@mike.com>]
recipients : [admin@mike.com]
sentDate : Mon Mar 13 11:47:15 CST 2017
*/