Skip to content

Instantly share code, notes, and snippets.

@peanutpi
Created July 3, 2016 15:40
Show Gist options
  • Save peanutpi/b6bcc090d58a445de4e357deccdb40b9 to your computer and use it in GitHub Desktop.
Save peanutpi/b6bcc090d58a445de4e357deccdb40b9 to your computer and use it in GitHub Desktop.
import java.util.Properties;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Store;
public class MailReader {
private static final String FOLDER = "Inbox";
private static final String PASSWORD = "secretPassword";
private static final String EMAIL_ID = "user@domain.com";
private static final String IMAP_SERVER = "imap.mail.domain.com";
public static void main(String[] args) throws Exception {
Properties props = new Properties();
props.setProperty("mail.store.protocol", "imaps");
try {
Session session = Session.getInstance(props, null);
Store store = session.getStore();
store.connect(IMAP_SERVER, EMAIL_ID, PASSWORD);
Folder inbox = store.getFolder(FOLDER);
inbox.open(Folder.READ_ONLY);
Message[] msgs = inbox.getMessages();
int count = 0;
for (Message msg : msgs) {
System.out.println("MESSAGE FROM :: "+msg.getFrom());
System.out.println("MESSAGE SUBJECT :: "+msg.getSubject());
System.out.println("MESSAGE CONTECT :: \n "+msg.getContent());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment