I have requirement to read user email box with user permissions and display contents in single inbox.
Program will ask user input userid/password and we also pass param to identify gmail or yahoo etc.
package com.java.test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;
import javax.mail.Flags;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.search.FlagTerm;
/**
*
* @author vaquar.khan@gmail.com
*
*/
public class EmailReader {
final private static String MAIL_STORE="mail.store.protocol";
final private static String IMAP="imaps";
//
final private static String GMAIL_IMAP="imap.gmail.com";
final private static String GMAIL_EMAIL_ID="";
final private static String GMAIL_EMAIL_PASSWORD="";
//
final private static String YAHOO_IMAP="imap.mail.yahoo.com";
final private static String YAHOO_EMAIL_ID="";
final private static String YAHOO_EMAIL_PASSWORD="";
//
final private static String MICROSOFT_IMAP="imap-mail.outlook.com";
final private static String MICROSOFT_EMAIL_ID="";
final private static String MICROSOFT_EMAIL_PASSWORD="";
public static void main(String args[]) {
readEamil("MICROSOFT") ;
}
public static void readEamil(String emailType) {
Properties props = System.getProperties();
props.setProperty(MAIL_STORE, IMAP);
try {
Session session = Session.getDefaultInstance(props, null);
Store store = session.getStore(IMAP);
if(emailType=="GMAIL"){
// IMAP host for gmail.
store.connect(GMAIL_IMAP, GMAIL_EMAIL_ID, GMAIL_EMAIL_PASSWORD);
}else if(emailType=="YAHOO"){
// IMAP host for yahoo.
store.connect(YAHOO_IMAP, YAHOO_EMAIL_ID, YAHOO_EMAIL_PASSWORD);
}else if(emailType=="MICROSOFT"){
// IMAP host for MICROSOFT.
store.connect(MICROSOFT_IMAP, MICROSOFT_EMAIL_ID, MICROSOFT_EMAIL_PASSWORD);
}
System.out.println(store);
Folder inbox = store.getFolder("Inbox");
inbox.open(Folder.READ_ONLY);
BufferedReader optionReader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Press (U) to get only unread mails OR Press (A) to get all mails:");
try {
char answer = (char) optionReader.read();
if(answer=='A' || answer=='a'){
showAllMails(inbox);
}else if(answer=='U' || answer=='u'){
showUnreadMails(inbox);
}
optionReader.close();
} catch (IOException e) {
System.out.println(e);
}
} catch (NoSuchProviderException e) {
System.out.println(e.toString());
System.exit(1);
} catch (MessagingException e) {
System.out.println(e.toString());
System.exit(2);
}
}
static public void showUnreadMails(Folder inbox){
try {
FlagTerm ft = new FlagTerm(new Flags(Flags.Flag.SEEN), false);
Message msg[] = inbox.search(ft);
System.out.println("MAILS: "+msg.length);
for(Message message:msg) {
try {
System.out.println("DATE: "+message.getSentDate().toString());
System.out.println("FROM: "+message.getFrom()[0].toString());
System.out.println("SUBJECT: "+message.getSubject().toString());
System.out.println("CONTENT: "+message.getContent().toString());
System.out.println("******************************************");
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("No Information");
}
}
} catch (MessagingException e) {
System.out.println(e.toString());
}
}
static public void showAllMails(Folder inbox){
try {
Message msg[] = inbox.getMessages();
System.out.println("MAILS: "+msg.length);
for(Message message:msg) {
try {
System.out.println("DATE: "+message.getSentDate().toString());
System.out.println("FROM: "+message.getFrom()[0].toString());
System.out.println("SUBJECT: "+message.getSubject().toString());
System.out.println("CONTENT: "+message.getContent().toString());
System.out.println("******************************************");
} catch (Exception e) {
System.out.println("No Information");
}
}
} catch (MessagingException e) {
System.out.println(e.toString());
}
}
}
Program will ask user input userid/password and we also pass param to identify gmail or yahoo etc.
package com.java.test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;
import javax.mail.Flags;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.search.FlagTerm;
/**
*
* @author vaquar.khan@gmail.com
*
*/
public class EmailReader {
final private static String MAIL_STORE="mail.store.protocol";
final private static String IMAP="imaps";
//
final private static String GMAIL_IMAP="imap.gmail.com";
final private static String GMAIL_EMAIL_ID="";
final private static String GMAIL_EMAIL_PASSWORD="";
//
final private static String YAHOO_IMAP="imap.mail.yahoo.com";
final private static String YAHOO_EMAIL_ID="";
final private static String YAHOO_EMAIL_PASSWORD="";
//
final private static String MICROSOFT_IMAP="imap-mail.outlook.com";
final private static String MICROSOFT_EMAIL_ID="";
final private static String MICROSOFT_EMAIL_PASSWORD="";
public static void main(String args[]) {
readEamil("MICROSOFT") ;
}
public static void readEamil(String emailType) {
Properties props = System.getProperties();
props.setProperty(MAIL_STORE, IMAP);
try {
Session session = Session.getDefaultInstance(props, null);
Store store = session.getStore(IMAP);
if(emailType=="GMAIL"){
// IMAP host for gmail.
store.connect(GMAIL_IMAP, GMAIL_EMAIL_ID, GMAIL_EMAIL_PASSWORD);
}else if(emailType=="YAHOO"){
// IMAP host for yahoo.
store.connect(YAHOO_IMAP, YAHOO_EMAIL_ID, YAHOO_EMAIL_PASSWORD);
}else if(emailType=="MICROSOFT"){
// IMAP host for MICROSOFT.
store.connect(MICROSOFT_IMAP, MICROSOFT_EMAIL_ID, MICROSOFT_EMAIL_PASSWORD);
}
System.out.println(store);
Folder inbox = store.getFolder("Inbox");
inbox.open(Folder.READ_ONLY);
BufferedReader optionReader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Press (U) to get only unread mails OR Press (A) to get all mails:");
try {
char answer = (char) optionReader.read();
if(answer=='A' || answer=='a'){
showAllMails(inbox);
}else if(answer=='U' || answer=='u'){
showUnreadMails(inbox);
}
optionReader.close();
} catch (IOException e) {
System.out.println(e);
}
} catch (NoSuchProviderException e) {
System.out.println(e.toString());
System.exit(1);
} catch (MessagingException e) {
System.out.println(e.toString());
System.exit(2);
}
}
static public void showUnreadMails(Folder inbox){
try {
FlagTerm ft = new FlagTerm(new Flags(Flags.Flag.SEEN), false);
Message msg[] = inbox.search(ft);
System.out.println("MAILS: "+msg.length);
for(Message message:msg) {
try {
System.out.println("DATE: "+message.getSentDate().toString());
System.out.println("FROM: "+message.getFrom()[0].toString());
System.out.println("SUBJECT: "+message.getSubject().toString());
System.out.println("CONTENT: "+message.getContent().toString());
System.out.println("******************************************");
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("No Information");
}
}
} catch (MessagingException e) {
System.out.println(e.toString());
}
}
static public void showAllMails(Folder inbox){
try {
Message msg[] = inbox.getMessages();
System.out.println("MAILS: "+msg.length);
for(Message message:msg) {
try {
System.out.println("DATE: "+message.getSentDate().toString());
System.out.println("FROM: "+message.getFrom()[0].toString());
System.out.println("SUBJECT: "+message.getSubject().toString());
System.out.println("CONTENT: "+message.getContent().toString());
System.out.println("******************************************");
} catch (Exception e) {
System.out.println("No Information");
}
}
} catch (MessagingException e) {
System.out.println(e.toString());
}
}
}