i using following code download messages gmail.
import java.io.bufferedinputstream; import java.io.ioexception; import java.io.inputstream; import java.util.date; import java.util.properties; import javax.mail.address; import javax.mail.bodypart; import javax.mail.fetchprofile; import javax.mail.flags; 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.search.flagterm; import org.openqa.selenium.webdriver; public class mailreader { webdriver driver; folder inbox; string m; string gmailid = "xyz@gmail.com"; string gmailpass = "xyz"; string storemessage; public mailreader() { } public string readmail() { system.out.println("inside readmail()..."); final string ssl_factory = "javax.net.ssl.sslsocketfactory"; /* set mail properties */ properties props = system.getproperties(); // set manual properties props.setproperty("mail.pop3.socketfactory.class", ssl_factory); props.setproperty("mail.pop3.socketfactory.fallback", "false"); props.setproperty("mail.pop3.port", "995"); props.setproperty("mail.pop3.socketfactory.port", "995"); props.put("mail.pop3.host", "pop.gmail.com"); try { /* create session , store read mail. */ session session = session.getdefaultinstance( system.getproperties(), null); store store = session.getstore("pop3"); store.connect("pop.gmail.com", 995, gmailid, gmailpass); /* mention folder name want read. */ // inbox = store.getdefaultfolder(); // inbox = inbox.getfolder("inbox"); inbox = store.getfolder("inbox"); /* open inbox using store. */ inbox.open(folder.read_only); /* messages unread in inbox */ message messages[] = inbox.search(new flagterm(new flags( flags.flag.seen), false)); system.out.println("no. of unread messages : " + messages.length); /* use suitable fetchprofile */ fetchprofile fp = new fetchprofile(); fp.add(fetchprofile.item.envelope); fp.add(fetchprofile.item.content_info); inbox.fetch(messages, fp); try { m = printallmessages(messages); inbox.close(true); store.close(); } catch (exception ex) { system.out.println("exception arise @ time of read mail"); ex.printstacktrace(); } } catch (messagingexception e) { system.out.println("exception while connecting server: " + e.getlocalizedmessage()); e.printstacktrace(); system.exit(2); } return m; } public string printallmessages(message[] msgs) throws exception { string s = null; (int = 0; < msgs.length; i++) { //system.out.println("message #" + (i + 1) + ":"); s = printenvelope(msgs[i]); } return s; } public string printenvelope(message message) throws exception { address[] a; // if ((a = message.getfrom()) != null) { (int j = 0; j < a.length; j++) { system.out.println("from: " + a[j].tostring()); } } // if ((a = message.getrecipients(message.recipienttype.to)) != null) { (int j = 0; j < a.length; j++) { system.out.println("to: " + a[j].tostring()); } } string subject = message.getsubject(); date receiveddate = message.getreceiveddate(); date sentdate = message.getsentdate(); // receiveddate returning // null. used getsentdate() string content = message.getcontent().tostring(); system.out.println("subject : " + subject); if (receiveddate != null) { system.out.println("received date : " + receiveddate.tostring()); } system.out.println("sent date : " + sentdate.tostring()); system.out.println("content : " + content); return(getcontent(message)); } public string getcontent(message msg) { try { string contenttype = msg.getcontenttype(); system.out.println("content type : " + contenttype); multipart mp = (multipart) msg.getcontent(); bodypart bp = mp.getbodypart(0); int count = mp.getcount(); (int = 0; < count; i++) { string s = gettext(mp.getbodypart(i)); if(i == 1) { return s; } //dumppart(mp.getbodypar((i)); } } catch (exception ex) { system.out.println("exception arise @ content"); ex.printstacktrace(); } return m; } /* public void dumppart(part p) throws exception { // dump input stream .. inputstream = p.getinputstream(); // if "is" not buffered, wrap bufferedinputstream // around it. if (!(is instanceof bufferedinputstream)) { = new bufferedinputstream(is); } int c; system.out.println("message : "); while ((c = is.read()) != -1) { system.out.write(c); } }*/ boolean textishtml = false; /** * return primary text content of message. */ public string gettext(part p) throws messagingexception, ioexception { if (p.ismimetype("text/*")) { string s = (string)p.getcontent(); textishtml = p.ismimetype("text/html"); return s; } if (p.ismimetype("multipart/alternative")) { // prefer html text on plain text multipart mp = (multipart)p.getcontent(); string text = null; (int = 0; < mp.getcount(); i++) { part bp = mp.getbodypart(i); if (bp.ismimetype("text/plain")) { if (text == null) text = gettext(bp); continue; } else if (bp.ismimetype("text/html")) { string s = gettext(bp); if (s != null) return s; } else { return gettext(bp); } } return text; } else if (p.ismimetype("multipart/*")) { multipart mp = (multipart)p.getcontent(); (int = 0; < mp.getcount(); i++) { string s = gettext(mp.getbodypart(i)); if (s != null) return s; } } return null; } } i want modify code that:
- it downloads recent 10 unread messages
- it downloads messages sent specific address (for example, messages sent myemail@example.com
how can done?
thanks!
the 10 recent messages last 10 messages in inbox.
but of them read, , of them deleted. find 10 recent unread messages you'll need use flagterm search messages seen flag false. might want use andterm find messages deleted flag false.
note folder.search doesn't download of messages, tells messages match. can @ last 10 of messages , whatever need "download" them.
hopefully that's enough of hint started. if still can't work, show code you're using , results you're getting.
Comments
Post a Comment