Posted By

jerryvig on 10/06/10


Tagged

java gmail api imap javamail


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

wuliang


Parse Data from Gmail Using JavaMail API and IMAP


 / Published in: Java
 

This is a basic routine for connecting to the Gmail servers using the JavaMail API over the IMAP protocol and parsing data from those e-mails.

  1. import java.util.List;
  2. import java.util.Properties;
  3. import java.util.Date;
  4. import java.util.Locale;
  5. import java.io.BufferedWriter;
  6. import java.io.FileWriter;
  7. import java.io.IOException;
  8. import java.text.SimpleDateFormat;
  9. import javax.mail.Session;
  10. import javax.mail.MessagingException;
  11. import javax.mail.Store;
  12. import javax.mail.Folder;
  13. import javax.mail.Message;
  14. import javax.mail.NoSuchProviderException;
  15. import javax.mail.Address;
  16. import javax.mail.internet.MimeMessage;
  17. import java.text.SimpleDateFormat;
  18.  
  19. public final class YomunkoEmails {
  20. public static void main( String[] args ) {
  21.  
  22. String host = "imap.gmail.com";
  23. String username = "myusername";
  24. String password = "mypassword";
  25.  
  26. SimpleDateFormat myFormatter = new SimpleDateFormat( "yyyy-MM-dd", Locale.US );
  27.  
  28. Properties props = System.getProperties();
  29. props.setProperty( "mail.store.protocol", "imaps" );
  30.  
  31. BufferedWriter myWriter = null;
  32. try {
  33. myWriter = new BufferedWriter( new FileWriter( "/home/diggler/Desktop/groovy_testing/BigBendHotSprings/BBHSHelpers.csv" ) );
  34. myWriter.write( "\"Sent Date\",\"Subject\",\"From\",\"E-Mail\",\"Phone Number\",\"Skills & Interests\",\"Comments\"\n" );
  35. }
  36. catch ( IOException myIOE ) {
  37. myIOE.printStackTrace();
  38. }
  39.  
  40. try {
  41.  
  42. Session mySession = Session.getDefaultInstance( props, null );
  43. Store myStore = mySession.getStore("imaps");
  44. myStore.connect( host, username, password );
  45.  
  46. System.out.println( myStore );
  47.  
  48. Folder inbox = myStore.getFolder("BBHS Helpers");
  49. inbox.open(Folder.READ_ONLY);
  50. Message messages[] = inbox.getMessages();
  51. System.out.println( messages.length );
  52.  
  53. for ( Message message:messages ) {
  54. Address addys[] = message.getReplyTo();
  55. for ( Address addy:addys ) {
  56. //System.out.println( addy.toString() );
  57. if ( addy.toString().trim().equals( "alchemiculture <[email protected]>" ) ) {
  58. if ( message.getSubject().startsWith( "BigBendHotSprings.org Message from" ) ) {
  59. String messageSubject = message.getSubject();
  60. String sentDate = myFormatter.format( message.getSentDate() );
  61. MimeMessage myMimeMessage = (MimeMessage) message;
  62. try {
  63. // System.out.println( messageSubject );
  64. // System.out.println( sentDate );
  65. String messageContent = (String) myMimeMessage.getContent();
  66. String messageContentLines[] = messageContent.split("\n");
  67. String from = "";
  68. String eMail = "";
  69. String phoneNumber = "";
  70. for ( String line:messageContentLines ) {
  71. if ( line.startsWith( "From:" ) ) {
  72. from = line.split(":")[1].trim();
  73. }
  74. if ( line.startsWith( "Email:" ) ) {
  75. eMail = line.split(":")[1].trim();
  76. }
  77. if ( line.startsWith( "Phone:" ) ) {
  78. phoneNumber = line.split(":")[1].trim();
  79. }
  80. }
  81. String messageContentSplits[] = messageContent.split("Skills and Interests:");
  82. String messageContentSplitsII[] = messageContentSplits[1].split("Comments:");
  83. String skillsAndInterests = messageContentSplitsII[0].trim();
  84. skillsAndInterests = skillsAndInterests.replaceAll( "\n", "" );
  85. skillsAndInterests = skillsAndInterests.replaceAll( "\"", "" );
  86. skillsAndInterests = skillsAndInterests.replaceAll( ",", "" );
  87. String comments = messageContentSplitsII[1].trim();
  88. comments = comments.replaceAll( "\n", "" );
  89. comments = comments.replaceAll( "\"", "" );
  90. comments = comments.replaceAll( ",", "" );
  91.  
  92. // System.out.println( skillsAndInterests );
  93. // System.out.println( comments );
  94.  
  95. myWriter.write( "\"" + sentDate + "\",\"" + messageSubject + "\",\"" + from + "\",\"" + eMail + "\",\"" + phoneNumber + "\",\"" + s\
  96. killsAndInterests + "\",\"" + comments + "\"\n" );
  97.  
  98. }
  99. catch ( IOException myIOE ) {
  100. myIOE.printStackTrace();
  101. }
  102. }
  103. }
  104. }
  105. }
  106.  
  107. }
  108. e.printStackTrace();
  109. }
  110. catch ( MessagingException e ) {
  111. e.printStackTrace();
  112. }
  113.  
  114. try {
  115. myWriter.close();
  116. }
  117. catch ( IOException myIOE ) {
  118. myIOE.printStackTrace();
  119. }
  120.  
  121. }
  122. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: Tyagi23 on January 4, 2019

Here you will be able to find out one of the best portal windows 10 control panel location which will let you know how do I find out control panel in windows 10 and will also let you know more about it. Try out for once as it includes all advantageous information.

You need to login to post a comment.