Cabinetman Posted September 6, 2012 Posted September 6, 2012 I don't want to spend $100+ for a plugin. I want to just get the emails subject field/line and either : Import into a field for parsing or Possibly append all to 1 field/file with new line separation. If a file I'm thinking to maybe stream the output as a file for import afterward ... Something like : Online Auction of property .... Important information about ... Welcome to the ... Whatever ... Whatever ... Here's some code I found import java.util.Properties; // added import javax.mail.*; import javax.mail.internet.*; // added import javax.mail.Folder; import javax.mail.Message; import javax.mail.Session; import javax.mail.Store; Properties props = new Properties(); String host = "mail.newwavecomm.net"; // My actual server String username = ""; String password = ""; String provider = "pop3"; Session session = Session.getDefaultInstance(props, null); Store store = session.getStore(provider); store.connect(host, username, password); /************************************************************************ * My attempt to import into a string ************************************************************************/ StringBuffer subjects = new StringBuffer(); Folder inbox = store.getFolder("INBOX"); if (inbox == null) { subjects.append("No INBOX"); System.exit(1); } inbox.open(Folder.READ_ONLY); Message[] messages = inbox.getMessages(); for (int i = 0; i < messages.length; i++) { subjects.append("Message " + (i + 1)); messages[i].writeTo(subjects); } inbox.close(false); store.close();
fseipel Posted September 13, 2012 Posted September 13, 2012 Did you add a JAR comprising the latest version of javamail, e.g. http://download.oracle.com/otn-pub/java/javamail/1.4.5/javamail1_4_5.zip?AuthParam=1347496355_671e69710f95bc985284db1df79a383d? Then use this example, e.g. http://www.roseindia.net/javamail/POP3Clint.shtml Below is a very basic example. Modify as appropriate to obtain new messages only, multipart MIME, HTML, etc. import java.util.*; import javax.mail.*; import javax.mail.internet.*; String host = "POP3HOST"; String user = "USERNAME"; String password = "PASSWORD"; String results = ""; String Body=""; // Get system properties Properties properties = System.getProperties(); // Get the default Session object. Session session = Session.getDefaultInstance(properties); // Get a Store object that implements the specified protocol. Store store = session.getStore("pop3"); //Connect to the current host using the specified username and password. store.connect(host, user, password); //Create a Folder object corresponding to the given name. Folder folder = store.getFolder("inbox"); // Open the Folder. folder.open(Folder.READ_ONLY); // Get the messages from the server Message[] messages = folder.getMessages(); // Display message. for (int i = 0; i < messages.length; i++) { results = results + "------------ Message " + (i + 1) + " ------------" + "rn"; // Here's the big change... String from = InternetAddress.toString(messages.getFrom()); if (from != null) { results = results + "From: " + from + "rn"; } String replyTo = InternetAddress.toString( messages.getReplyTo()); if (replyTo != null) { results = results + "Reply-to: " + replyTo + "rn"; } String to = InternetAddress.toString( messages.getRecipients(Message.RecipientType.TO)); if (to != null) { results = results + "To: " + to + "rn"; } String cc = InternetAddress.toString( messages.getRecipients(Message.RecipientType.CC)); if (cc != null) { results = results + "Cc: " + cc + "rn"; } String bcc = InternetAddress.toString( messages.getRecipients(Message.RecipientType.BCC)); if (bcc != null) { results = results + "Bcc: " + to + "rn"; } String subject = messages.getSubject(); if (subject != null) { results = results + "Subject: " + subject + "rn"; } Date sent = messages.getSentDate(); if (sent != null) { results = results + "Sent: " + sent + "rn"; } Date received = messages.getReceivedDate(); if (received != null) { results = results + "Received: " + received + "rn"; } if (messages.getContent() != null) { Body = messages.getContent() ; results = results + Body.replaceAll("<.*?>","") + "rn"; } } folder.close(true); store.close(); return results;
Cabinetman Posted September 22, 2012 Author Posted September 22, 2012 Sorry it's taken so long to get back on this but thanks Frank ! I was close ... just not quite there.
Cabinetman Posted September 23, 2012 Author Posted September 23, 2012 I spoke to soon ........ I have a dilemma. Dilemma - replaceAll("<.*?>","") doesn't work in/on .... Body.replaceAll("<.*?>","") .... because it's an object. I've got to many new lines to any importing. I've been trying and searching for several days now for an answer but so far no luck. Any Ideas ??
john renfrew Posted September 24, 2012 Posted September 24, 2012 Body.toString().replaceAll("<.*?>","") ?? but the content may be multipart this might help http://www.roseindia...ipartMail.shtml
Cabinetman Posted September 25, 2012 Author Posted September 25, 2012 Yeah I know that and normally you would want that file attachment ... however this isn't a normal use of the javamail to read mail. Overall I have a special email address I use to send a text email to (no attachments) and I need to import the subject and body into Filemaker. Then I parse the body for certain info in it. At the moment I'm getting an error : "org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object ' " Here's my adapted code : /* * Java Mail for FMP specialized */ import java.io.*; import java.lang.*; import java.util.*; import javax.mail.*; import javax.mail.internet.*; import javax.activation.DataHandler; String results = ""; // Get system properties Properties properties = System.getProperties(); // Get the default Session object. Session session = Session.getDefaultInstance(properties); // Get a Store object that implements the specified protocol. Store store = session.getStore("pop3"); //Connect to the current host using the specified username and password. store.connect(host, userName, password); //Create a Folder object corresponding to the given name. Folder folder = store.getFolder("inbox"); // Open the Folder. folder.open(Folder.READ_ONLY); // Get the messages from the server Message[] messages = folder.getMessages(); // Display message. for (int i = 0; i < messages.length; i++) { String subject = messages[i].getSubject(); if (subject != null) { results = results + subject + "\t"; } // Get date sent Date sent = messages[i].getSentDate(); if (sent != null) { results = results + sent + "\t"; } // Get message body which I need without and new lines if (messages[i].getContent() != null) { Multipart multipart = (Multipart) messages[i].getContent(); for (int x = 0; x < multipart.getCount(); x++) { BodyPart bodyPart = multipart.getBodyPart(x); String disposition = bodyPart.getDisposition(); if (disposition != null && (disposition.equals(BodyPart.ATTACHMENT))) { results = results + "Mail have some attachment : \t"; DataHandler handler = bodyPart.getDataHandler(); results = results + "file name : " + handler.getName() + "\n"; } else { // Needs to be converted to String ? String Body = bodyPart.getContent(); results = results + Body + "\n"; } } } } folder.close(true); store.close(); //return results; //This simple version uses utf-8 //new File( filePath ).write( textToWrite ); //This more advanced version allows you to specify the character encoding OutputStreamWriter writer = new OutputStreamWriter( new FileOutputStream( filePath ), "utf-8" ); writer.write( results ); writer.close(); return true;
john renfrew Posted September 26, 2012 Posted September 26, 2012 Not in any way expert but there are a couple of place to look results = results + sent + "t" sent is a Date object and needs converting to a string before you can add it to the result string - see inline for SimpleDateFormat bodyPart.getContent() there is no guarantee this is actually a text string either - using type = bodyPart.getContentType() will ascertain what it is being automatically read as. I also wrapped the whole thing in a try..catch to see if you can get more extensive error reporting which might give more clues.. try these amendments /* * Java Mail for FMP specialized */ import java.io.* import java.lang.* import java.util.* import javax.mail.* import javax.mail.internet.* import javax.activation.DataHandler results = '' //ADDED //change to suit - look up sdf for what characters mean df = new SimpleDateFormat('yyyyMMdd') // Get system properties properties = System.getProperties() // Get the default Session object. session = Session.getDefaultInstance(properties) // Get a Store object that implements the specified protocol. store = session.getStore("pop3") //Connect to the current host using the specified username and password. store.connect(host, userName, password) //Create a Folder object corresponding to the given name. folder = store.getFolder('inbox') // Open the Folder. folder.open(Folder.READ_ONLY) try{ // Get the messages from the server Message[] messages = folder.getMessages() // Display message. for (i in 0..< messages.length ) { //is this getSubject a string?? or is there a method for it? String subject = messages[i].getSubject() if (subject != null) { results = results + subject + 't' } // Get date sent Date sent = messages[i].getSentDate() if (sent != null) { //ADDED sent = df.format(sent) results = results + sent + 't' } // Get message body which I need without and new lines if (messages[i].getContent() != null) { Multipart multipart = (Multipart) messages[i].getContent() for (x in 0..< multipart.getCount()) { bodyPart = multipart.getBodyPart(x) disposition = bodyPart.getDisposition() if (disposition != null && (disposition.equals(BodyPart.ATTACHMENT))) { results = results + 'Mail have some attachment : t' handler = bodyPart.getDataHandler() results = results + 'file name : ' + handler.getName() + 'n' } else { // Needs to be converted to String ? type = bodyPart.getContentType() results = results + type + 'n' //this will only work if this is text/plain // comment out for the moment //Body = bodyPart.getContent() //results = results + Body + 'n' } //end if } //end for } //end if } //end for folder.close(true) store.close() } catch (e){ return e } //end try //return results //This simple version uses utf-8 //new File( filePath ).write( textToWrite ) //This more advanced version allows you to specify the character encoding writer = new OutputStreamWriter( new FileOutputStream( filePath ), "utf-8" ) writer.write( results ) writer.close() return true instead of the for construction you could use messages.each(){ then instead of messages you just have it john
Cabinetman Posted September 27, 2012 Author Posted September 27, 2012 First let me add this comment ..... I don't care about attachments in the slightest. I only need to get the subject, date sent and text message body for each email. Anyway ... using your code : I don't need the date format as it's a string. I did try but got : org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '20120923' with class 'java.lang.String' to class 'java.util.Date' So I commented it out but still get an object error ..... Note: the object is actually the full message body which I replaced with just 'text body ...' to save space. org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'text body of the email ' with class 'java.lang.String' to class 'javax.mail.Multipart' I really can't believe this is so hard. I'm sure I'm missing something simple .......
john renfrew Posted September 27, 2012 Posted September 27, 2012 That's a challenge then... Here it is working. At least on an inbox of 158 mixed emails I have Part of the issue is where there really is no header or body part information so I have added a way of getting round that. TheSDF issues was forgetting to put the import at the top This requires the latest mail.jar from Oracle.. and I left most of my tests in. //amended 12_09_27 JR /* * Java Mail for FMP specialized */ import java.io.* import java.lang.* import java.util.* import javax.mail.* import javax.mail.internet.* import javax.activation.DataHandler import javax.mail.Session import java.text.SimpleDateFormat results = '' //ADDED //change to suit - look up sdf for what characters mean df = new SimpleDateFormat('yyyyMMdd') // Get system properties properties = System.getProperties() // Get the default Session object. session = Session.getDefaultInstance(properties) // Get a Store object that implements the specified protocol. store = session.getStore("pop3") //Connect to the current host using the specified username and password. store.connect(host, userName, password) //Create a Folder object corresponding to the given name. folder = store.getFolder('inbox') // Open the Folder. folder.open(Folder.READ_ONLY) try{ // Get the messages from the server messages = folder.getMessages() //return messages.size() // Display message. //return messages.length for (i in 0..< messages.size() ) { //for (i in 0..2 ) { results = results + (!results? '': 'n') //is this getSubject a string?? or is there a method for it? subject = messages[i].getSubject() //return subject if (subject != null) { results = results + subject + 't' } // Get date sent Date sent = messages[i].getSentDate() if (sent != null) { //ADDED ss = df.format(sent) results = results + ss + 't' } //return results // Get message body which I need without and new lines if (messages[i].getContent() != null) { multipart = messages[i].getContent() //return multipart.class.getName() //return multipart.getCount() if (multipart.class.getName() != 'javax.mail.internet.MimeMultipart'){ results = results + multipart } else { z = multipart.getCount() zz = !z? 0:z for (x in 0..< zz) { bodyPart = multipart.getBodyPart(x) //return bodyPart disposition = bodyPart.getDisposition() if (disposition != null && (disposition.equals(BodyPart.ATTACHMENT))) { results = results + 'Mail has an attachment : t' handler = bodyPart.getDataHandler() results = results + 'file name : ' + handler.getName() } else { type = bodyPart.getContentType() //return type if ( type.contains('plain')){ //results = results + type } } //this will only work if this is text/plain if( type.contains('text/plain')){ Body = bodyPart.getContent() results = results + Body } } //end if } //end for } //end if } //end for folder.close(true) store.close() } catch (e){ return e } //end try //return results //This simple version uses utf-8 //new File( filePath ).write( textToWrite ) //This more advanced version allows you to specify the character encoding writer = new OutputStreamWriter( new FileOutputStream( filePath ), "utf-8" ) writer.write( results ) writer.close() return true
Cabinetman Posted September 28, 2012 Author Posted September 28, 2012 That's a challenge then... It is .... apparently. I can get my file as: Subject SentDate Body text first line Body text 2nd line Body text 3rd line Body text 4th line Subject SentDate Body text first line Body text 2nd line Body text 3rd line Body text 4th line Subject SentDate Body text first line Body text 2nd line Body text 3rd line Body text 4th line When I need : Subject SentDate Body text first line Body text 2nd line Body text 3rd line Body text 4th line Subject SentDate Body text first line Body text 2nd line Body text 3rd line Body text 4th line Subject SentDate Body text first line Body text 2nd line Body text 3rd line Body text 4th line Nothing I seem to try will remove the carriage returns from the body .............. My addition to the code was : if( type.contains('text/plain')){ Body = bodyPart.getContent() NewBody = Body.replaceAll("<.*?>[nr]","") results = results + NewBody } UGH !!
john renfrew Posted September 28, 2012 Posted September 28, 2012 How about some proper Groovy magic with the body? lined = body.tokenize('\n') tabbed = lined.join('\t') result = result + '\t' + tabbed
Cabinetman Posted September 29, 2012 Author Posted September 29, 2012 LOL ... I wish. The only difference ?? Tab's inserted at beginning of each line .... and I forgot to mention/post I still have a new line between Body text first line and Body text 2nd line just like the email. Subject SentDate Body text first line Body text 2nd line Body text 3rd line Body text 4th line This is just crazy !! NOTE: tokenize appears to only work on strings also .... and not objects.
fseipel Posted September 30, 2012 Posted September 30, 2012 First, that was a huge improvement on my original post John, handling attachments & checking MIME types. I don't know Java all that well. One workaround would seem to be to declare a second String for the body only, and then simply use a string array to represent the individual lines by use of split. Since it appears the OP only wants the first 4 lines, anyway, this should do the trick. Tokenize should work fine also, as long as body is cast to string, e.g. .toString -- I wasn't familiar with tokenize, I see it's a more generalized form of split. Either way, it's probably good to check if e-mail body has more than 4 lines so I included that too. Below appeared to work, but I found if I appended only n, no newline was added between messages; why is it necessary to use System.getProperty("line.separator") which presumably is CR LF in Windows? In contrast, if I write a script return "anbncn"; I receive the letters separated by newlines for instance? //amended 12_09_27 JR /* * Java Mail for FMP specialized */ import java.io.* import java.lang.* import java.util.* import javax.mail.* import javax.mail.internet.* import javax.activation.DataHandler import javax.mail.Session import java.text.SimpleDateFormat String Body_only=""; results = '' //ADDED //change to suit - look up sdf for what characters mean df = new SimpleDateFormat('yyyyMMdd') // Get system properties properties = System.getProperties() // Get the default Session object. session = Session.getDefaultInstance(properties) // Get a Store object that implements the specified protocol. store = session.getStore("pop3") //Connect to the current host using the specified username and password. store.connect(host, userName, password) //Create a Folder object corresponding to the given name. folder = store.getFolder('inbox') // Open the Folder. folder.open(Folder.READ_ONLY) try{ // Get the messages from the server messages = folder.getMessages() //return messages.size() // Display message. //return messages.length for (i in 0..< messages.size() ) { //for (i in 0..2 ) { results = results + (!results? '': 'n') //is this getSubject a string?? or is there a method for it? subject = messages.getSubject() //return subject if (subject != null) { results = results + subject + 't' } // Get date sent Date sent = messages.getSentDate() if (sent != null) { //ADDED ss = df.format(sent) results = results + ss + 't' } //return results // Get message body which I need without and new lines if (messages.getContent() != null) { multipart = messages.getContent() //return multipart.class.getName() //return multipart.getCount() if (multipart.class.getName() != 'javax.mail.internet.MimeMultipart'){ Body_only = multipart; String[] lines = Body_only.split(System.getProperty("line.separator")); if (lines.length >=4) { results = results + lines[1] + "t" + lines[2] + "t" + lines[3] + "t" + lines[4] + System.getProperty("line.separator");} } else { z = multipart.getCount() zz = !z? 0:z for (x in 0..< zz) { bodyPart = multipart.getBodyPart(x) //return bodyPart disposition = bodyPart.getDisposition() if (disposition != null && (disposition.equals(BodyPart.ATTACHMENT))) { results = results + 'Mail has an attachment : t' handler = bodyPart.getDataHandler() results = results + 'file name : ' + handler.getName() } else { type = bodyPart.getContentType() //return type if ( type.contains('plain')){ //results = results + type } } //this will only work if this is text/plain if( type.contains('text/plain')){ Body_only = bodyPart.getContent() String[] lines = Body_only.split(System.getProperty("line.separator")); if (lines.length >=4) { results = results + lines[1] + "t" + lines[2] + "t" + lines[3] + "t" + lines[4] + System.getProperty("line.separator");} } } //end if } //end for } //end if } //end for folder.close(true) store.close() } catch (e){ return e } //end try //return results //This simple version uses utf-8 //new File( filePath ).write( textToWrite ) //This more advanced version allows you to specify the character encoding //writer = new OutputStreamWriter( new FileOutputStream( filePath ), "utf-8" ) //writer.write( results ) //writer.close() return results;
john renfrew Posted September 30, 2012 Posted September 30, 2012 @cabinetman - of course it does, you need to turn the object into a string using whatever method is allowed first. Is this an exercise in learning or in others doing all the work for you? We can not ACTUALLY see your output so have no idea what to do with it other than make suggestions. from the documentation >> split (Convenience method to split a string (with whitespace or other as delimiter) Like tokenize, but returns an Array of Strings instead of a List) @fseipel - thanks rn is the Groovy equivalent of CR LF. In your example you could put SEP = System.getProperty("line.separator") at the top and then refer to it later, rather than calling it each time you loop. And as Groovy/Java is zero based does in need to be: if (lines.length >=4) { results = results + lines[0] + 't' + lines[1] + 't' + lines[2] + 't' + lines[3] + SEP } //end if
Cabinetman Posted September 30, 2012 Author Posted September 30, 2012 (edited) First let me say that I realize that threads don't always convey everything going on very well. So my apologies for anything that I may have thought I stated or was understood that wasn't. So a review .... I needed just a simple email checking java (I posted my attempt) and said: "I want to just get the emails subject field/line and either : Import into a field for parsing or possibly append all to 1 field/file with new line separation" Accomplished by @fseipel's first post. I then realized that I was going to need not just the subject but also the body (to get a piece of info out of it.) His code included getting the body so I went with it. However when using his code, which had a .replaceAll() in it, I realized the .replaceAll() had no effect whatsoever on the body. I read several web pages. It was evident quickly that the message body was not a string but an object. I then read probably 50 more pages and tried everything I read and could think of ... nothing was working. So I posted in my 3rd post and I guess where my mistake was .... Dilemma - replaceAll("<.*?>","") doesn't work in/on .... Body.replaceAll("<.*?>","") .... because it's an object. Here I should have specified that I had to have the .replaceAll() work and my dilemma was I couldn't get the object to convert to a string so that the .replaceAll() would work. I thought that since I included within my new posted code this section ..... @cabinetman - of course it does, you need to turn the object into a string using whatever method is allowed first. Yeah .... I posted that some time ago. Is this an exercise in learning or in others doing all the work for you? I had only 1 problem at first. Then a second, after solving the first, of getting the body into string format so the returns could be taken out of it. The other stuff, although nice (and thank you as I'm sure it'll help someone) had nothing to do with what I was asking help for. But I guess that was my fault for not cutting in when you didn't seem to understand the 'Body.replaceAll()' code section. So thanks to all. Both issues solved ...... PS - yes it needs to be set as : // Needs to be converted to String String Body = bodyPart.getContent(); results = results + Body + "n"; and then later .... if( type.contains('text/plain')){ Body = bodyPart.getContent() NewBody = Body.replaceAll("<.*?>[nr]","") results = results + NewBody } ..... it was understood that I was trying to get the object to string format and that I was trying different code. I tried to use the 'assert' and several other thngs I read. MY BAD. SSsssooooo to answer : if (lines.length >=4) { results = results + lines[0] + 't' + lines[1] + 't' + lines[2] + 't' + lines[3] + SEP } //end if Edited September 30, 2012 by Cabinetman
Cabinetman Posted September 30, 2012 Author Posted September 30, 2012 OK ... so my final draft. I probably should've edited out more but I just left it in. I edited the message body section to iterate through and add however many lines 1 at a time .... along with a few other things. /* * Java Mail for FMP specialized * fseipel * amended 12_09_27 JR * amended 12_09_30 BW */ import java.io.* import java.lang.* import java.util.* import javax.mail.* import javax.mail.internet.* import javax.activation.DataHandler import javax.mail.Session import java.text.SimpleDateFormat String Body_only = '' results = '' SEP = System.getProperty('line.separator') //ADDED - change to suit - look up sdf for what characters mean df = new SimpleDateFormat('yyyyMMdd') // Get system properties properties = System.getProperties() // Get the default Session object. session = Session.getDefaultInstance(properties) // Get a Store object that implements the specified protocol. store = session.getStore('pop3') //Connect to the current host using the specified username and password. store.connect(host, userName, password) //Create a Folder object corresponding to the given name. folder = store.getFolder('inbox') // Open the Folder. folder.open(Folder.READ_ONLY) try{ // Get the messages from the server messages = folder.getMessages() //return messages.size() // Display message. //return messages.length for (i in 0..< messages.size() ) { //for (i in 0..2 ) { results = results + (!results? '': '\n') //is this getSubject a string?? or is there a method for it? subject = messages[i].getSubject() //return subject if (subject != null) { results = results + subject + '\t' } // Get date sent Date sent = messages[i].getSentDate() if (sent != null) { //ADDED ss = df.format(sent) results = results + ss + '\t' // } // Get message body which I need without and new lines if (messages[i].getContent() != null) { multipart = messages[i].getContent() //return multipart.class.getName() //return multipart.getCount() if (multipart.class.getName() != 'javax.mail.internet.MimeMultipart'){ Body_only = multipart; String[] lines = Body_only.split(System.getProperty('line.separator')); //if (lines.length >=4) { //results = results + lines[0] + '\t' + lines[1] + '\t' + lines[2] + '\t' + lines[3] + SEP; //} //end if // ADDED to iterate through lines for(int a =0; a < lines.length ; a++) { results = results + lines[a] + '\t' } // END OF ADDEDD } else { z = multipart.getCount() zz = !z? 0:z for (x in 0..< zz) { bodyPart = multipart.getBodyPart(x) //return bodyPart disposition = bodyPart.getDisposition() if (disposition != null && (disposition.equals(BodyPart.ATTACHMENT))) { results = results + 'Mail has an attachment : \t' handler = bodyPart.getDataHandler() results = results + 'file name : ' + handler.getName() } else { type = bodyPart.getContentType() //return type if ( type.contains('plain')){ //results = results + type } //end if //this will only work if this is text/plain if( type.contains('text/plain')){ Body_only = bodyPart.getContent() String[] lines = Body_only.split(System.getProperty('line.separator')) // ADDED to iterate through lines for(int a =0; a < lines.length ; a++) { results = results + lines[a] + '\t' } // END OF ADDED } //end if //if (lines.length >=4) { //results = results + lines[0] + '\t' + lines[1] + '\t' + lines[2] + '\t' + lines[3] + '\t' + SEP; //} //end if } // end else } //end for } //end else } //end if is for the message body } //end for messages results = results + SEP; // ADDED to insert return after body folder.close(true) store.close() } catch (e){ //end try and catch errors return e } //end catch //return results //This simple version uses utf-8 //new File( filePath ).write( textToWrite ) // //This more advanced version allows you to specify the character encoding writer = new OutputStreamWriter( new FileOutputStream( filePath ), 'utf-8' ) writer.write( results ) writer.close() return true
Recommended Posts
This topic is 4780 days old. Please don't post here. Open a new topic instead.
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now