package com.timers;
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 Vinay Guntaka
* mail.jar is required to execute this program (download from java2s.com)
* And your Gmail Credentails.
* This Program reads only unread mails of your box
*
*/
public class ReadGmail {
public static void main(String args[]) {
while (true) {
try {
System.out.println("Started.......");
Start();
System.out.println("...Read completed.......");
try {
Thread.sleep(1000 * 5);
} catch (InterruptedException e1) {
}
} catch (Exception e) {
try {
e.printStackTrace();
System.out.println("..Error in connection Sleeping...");
} catch (Exception e1) {
}
}
}
}
public static void Start() throws Exception {
Properties props = System.getProperties();
props.setProperty("mail.store.protocol", "imaps");
try {
Session session = Session.getDefaultInstance(props, null);
Store store = session.getStore("imaps");
//simply username dnt include @gmail.com
store.connect("imap.gmail.com", "username", "password");
System.out.println(store);
int cout = 0;
Folder inbox = store.getFolder("Inbox");
inbox.open(Folder.READ_ONLY);
// / to read only unread mails
FlagTerm ft = new FlagTerm(new Flags(Flags.Flag.SEEN), false);
Message messages[] = inbox.search(ft);
// To read all mails from inbox
//Message messages[] = inbox.getMessages();
for (Message message : messages) {
System.out.println("Reading:" + (messages.length - cout));
System.out.println("MEssage Content :: --"
+ message.getSubject() + message.getReceivedDate());
cout++;
}
} catch (NoSuchProviderException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
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 Vinay Guntaka
* mail.jar is required to execute this program (download from java2s.com)
* And your Gmail Credentails.
* This Program reads only unread mails of your box
*
*/
public class ReadGmail {
public static void main(String args[]) {
while (true) {
try {
System.out.println("Started.......");
Start();
System.out.println("...Read completed.......");
try {
Thread.sleep(1000 * 5);
} catch (InterruptedException e1) {
}
} catch (Exception e) {
try {
e.printStackTrace();
System.out.println("..Error in connection Sleeping...");
} catch (Exception e1) {
}
}
}
}
public static void Start() throws Exception {
Properties props = System.getProperties();
props.setProperty("mail.store.protocol", "imaps");
try {
Session session = Session.getDefaultInstance(props, null);
Store store = session.getStore("imaps");
//simply username dnt include @gmail.com
store.connect("imap.gmail.com", "username", "password");
System.out.println(store);
int cout = 0;
Folder inbox = store.getFolder("Inbox");
inbox.open(Folder.READ_ONLY);
// / to read only unread mails
FlagTerm ft = new FlagTerm(new Flags(Flags.Flag.SEEN), false);
Message messages[] = inbox.search(ft);
// To read all mails from inbox
//Message messages[] = inbox.getMessages();
for (Message message : messages) {
System.out.println("Reading:" + (messages.length - cout));
System.out.println("MEssage Content :: --"
+ message.getSubject() + message.getReceivedDate());
cout++;
}
} catch (NoSuchProviderException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
No comments:
Post a Comment