Read Mail Java

Sample Program to Read Mail

package com.ashwin;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Part;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.event.FolderListener;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class MailReaderUtility {

public static void main(String[] args) {
Authenticator auth = null;
Folder folder = null;
Store store = null;
String comment = "";
String subject = "";
String from = "";

try {

auth = new SMTPAuthenticator(args[0],args[1]);

// Create empty properties
Properties props = System.getProperties();
props.put("mail.pop3.host", args[2]);
// get session
Session session = Session.getDefaultInstance(props, auth);
// Get the store
store = session.getStore("pop3");
store.connect();
// Get folder
FolderListener arg0;
// store.addFolderListener();

// store.addFolderListener();
folder = store.getFolder("INBOX");
folder.open(Folder.READ_WRITE);
// Get directory\\
Message message[] = folder.getMessages();
for (int i = 0, n = message.length; i < n; i++) {
Map dataStore = new HashMap();
Address[] addresses = message[i].getFrom();
InternetAddress address = (InternetAddress) addresses[0];
from = address.getAddress();
subject = message[i].getSubject();
dataStore.put("from", from);
dataStore.put("subject", subject);

if (((MimeMessage) message[i]).getContent() instanceof String) {
// content is texText
// System.out.println(((MimeMessage)
// message[i]).getContent());
comment = ((MimeMessage) message[i]).getContent()
.toString();
dataStore.put("comment", comment);
} else if (((MimeMessage) message[i]).getContent() instanceof Multipart
|| ((MimeMessage) message[i]).getContent() instanceof Part) {
Multipart multiPartcnt = (Multipart) ((MimeMessage) message[i])
.getContent();

Object content = message[i].getContent();
if (content instanceof Multipart) {
handleMultipart((Multipart) content, dataStore);
} else {
handlePart(message[i], dataStore);
}

comment = multiPartcnt.getBodyPart(0).getContent()
.toString();
// recursively iterate over container's contents to
// retrieve
// attachments
} else if (((MimeMessage) message[i]).getContent() instanceof Message) {
// message content could be a message itself
}

System.out.println("From: "+dataStore.get("from"));
System.out.println("Subject: "+dataStore.get("subject"));
String testComment = (String)dataStore.get("comment");
if(testComment.length() < 15){
System.out.println("Data: "+testComment.substring(0, testComment.length()));
}else{
System.out.println("Data: "+testComment.substring(0, 14));
}
System.out.println("------------------------------------");
}
}catch(Exception e){
e.printStackTrace();
}

}

public static void handleMultipart(Multipart multipart,
Map dataStore) throws MessagingException,
IOException {
for (int i = 0, n = multipart.getCount(); i < n; i++) {
handlePart(multipart.getBodyPart(i), dataStore);
}
}

public static void handlePart(Part part, Map dataStore)
throws MessagingException, IOException {
String disposition = part.getDisposition();
String contentType = part.getContentType();
// System.out.println("In Handle Part : " + contentType);
if (disposition == null) { // When just body
// System.out.println("Null: " + contentType);
// Check if plain
if ((contentType.length() >= 10)
&& (contentType.toLowerCase().substring(0, 10)
.equals("text/plain"))) {
// part.writeTo(System.out);
dataStore.put("comment", part.getContent().toString());
} else { // Don't think this will happen
// System.out.println("Other body: " + contentType);
dataStore.put("comment", part.getContent().toString());
}
} else if (disposition.equalsIgnoreCase(Part.ATTACHMENT)) {
// System.out.println("Attachment: " + part.getFileName() + " : "+
// contentType);
List fileNames = (List) dataStore.get("attachment");
if (fileNames == null) {
fileNames = new ArrayList();
}
fileNames.add(part.getFileName());
dataStore.put("attachment", fileNames);
//saveFile(part.getFileName(), part.getInputStream());
} else if (disposition.equalsIgnoreCase(Part.INLINE)) {
// System.out.println("Inline: " + part.getFileName() + " : " +
// contentType);
//saveFile(part.getFileName(), part.getInputStream());
} else { // Should never happen
// System.out.println("Other: " + disposition);
}
}
}

SMTP Authenticator


/**
*
*/
package com.ashwin;

import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;

/**
* @author ashwin
*
*/
public class SMTPAuthenticator extends Authenticator
{
String masteruser = null;
String masterpswd = null;

SMTPAuthenticator(String user, String password)
{
masteruser = user;
masterpswd = password;
}
public PasswordAuthentication getPasswordAuthentication()
{

return new PasswordAuthentication(masteruser, masterpswd);
}
}