regex - Java Mail API: Convert Message to String? -
i using following code retrieve messages gmail account.
// import statements public class confirmemail { webdriver driver; folder inbox; string gmailid = "xxxxxxxxxxx@gmail.com"; string gmailpass = "xxxxxxxx"; string storemessage; public confirmemail() { } public void mailreader() { system.out.println("inside mailreader()..."); 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 { 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); } } public void printallmessages(message[] msgs) throws exception { (int = 0; < msgs.length; i++) { system.out.println("message #" + (i + 1) + ":"); printenvelope(msgs[i]); } } public void 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); getcontent(message); } public void getcontent(message msg) { try { string contenttype = msg.getcontenttype(); system.out.println("content type : " + contenttype); multipart mp = (multipart) msg.getcontent(); int count = mp.getcount(); (int = 0; < count; i++) { dumppart(mp.getbodypart(i)); } } catch (exception ex) { system.out.println("exception arise @ content"); ex.printstacktrace(); } } 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); } } }
with code able print messages console. works flawlessly 100% of time.
however, need store "bodypart" (i.e, actual message or body of message) in string search string using regex. need extract links begining http.
how can convert message string?
thanks
i'm not quite sure asking (because said print out messages ... when print them, why can't store them in string?)
if realy want bodypart stored in string variable:
multipart mp = (multipart) msg.getcontent(); bodypart bp = mp.getbodypart(0); string content = bp.getcontent().tostring();
Comments
Post a Comment