java使用IMAP连接Gmail并解析邮件

2018-07-20    来源:open-open

容器云强势上线!快速搭建集群,上万Linux镜像随意使用
import java.util.List;
import java.util.Properties;
import java.util.Date;
import java.util.Locale;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import javax.mail.Session;
import javax.mail.MessagingException;
import javax.mail.Store;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.NoSuchProviderException;
import javax.mail.Address;
import javax.mail.internet.MimeMessage;
import java.text.SimpleDateFormat;

public final class YomunkoEmails {
   public static void main( String[] args ) {

      String host = "imap.gmail.com";
      String username = "myusername";
      String password = "mypassword";

      SimpleDateFormat myFormatter = new SimpleDateFormat( "yyyy-MM-dd", Locale.US );

      Properties props = System.getProperties();
      props.setProperty( "mail.store.protocol", "imaps" );

      BufferedWriter myWriter = null;
      try {
          myWriter = new BufferedWriter( new FileWriter( 
        "/home/diggler/Desktop/groovy_testing/BigBendHotSprings/BBHSHelpers.csv" ) );
          myWriter.write( "\"Sent Date\",\"Subject\",\"From\",
        \"E-Mail\",\"Phone Number\",\"Skills & Interests\",\"Comments\"\n" );
      }
      catch ( IOException myIOE ) {
          myIOE.printStackTrace();
      }

      try {

        Session mySession = Session.getDefaultInstance( props, null );
        Store myStore = mySession.getStore("imaps");
        myStore.connect( host, username, password );

        System.out.println( myStore );

        Folder inbox = myStore.getFolder("BBHS Helpers");
        inbox.open(Folder.READ_ONLY);
        Message messages[] = inbox.getMessages();
        System.out.println( messages.length );

        for ( Message message:messages ) {
            Address addys[] = message.getReplyTo();
            for ( Address addy:addys ) {
                //System.out.println( addy.toString() );
              if (  addy.toString().trim().equals( "alchemiculture <alchemiculture@culligan.dreamhost.com>" ) ) {
                 if ( message.getSubject().startsWith( "BigBendHotSprings.org Message from" ) ) {
                     String messageSubject =  message.getSubject();
                     String sentDate = myFormatter.format( message.getSentDate() );
                     MimeMessage myMimeMessage = (MimeMessage) message;
                   try {
                       // System.out.println( messageSubject );
                       // System.out.println( sentDate );
                      String messageContent = (String) myMimeMessage.getContent();
                      String messageContentLines[] = messageContent.split("\n");
                      String from = "";
                      String eMail = "";
                      String phoneNumber = "";
                      for ( String line:messageContentLines ) {
                         if ( line.startsWith( "From:" ) ) {
                            from = line.split(":")[1].trim();
                         }
                         if ( line.startsWith( "Email:" ) ) {
                            eMail = line.split(":")[1].trim();
                         }
                         if ( line.startsWith( "Phone:" ) ) {
                            phoneNumber = line.split(":")[1].trim();
                         }
                      }
                      String messageContentSplits[] = messageContent.split("Skills and Interests:");
                      String messageContentSplitsII[] = messageContentSplits[1].split("Comments:");
                      String skillsAndInterests = messageContentSplitsII[0].trim();
                      skillsAndInterests = skillsAndInterests.replaceAll( "\n", "" );
                      skillsAndInterests = skillsAndInterests.replaceAll( "\"", "" );
                      skillsAndInterests = skillsAndInterests.replaceAll( ",", "" );
                      String comments = messageContentSplitsII[1].trim();
                      comments = comments.replaceAll( "\n", "" );
                      comments = comments.replaceAll( "\"", "" );
                      comments = comments.replaceAll( ",", "" );

                      // System.out.println( skillsAndInterests );
                      // System.out.println( comments );

                      myWriter.write( "\"" + sentDate + "\",\"" + messageSubject + "\",\"" + 
            from + "\",\"" + eMail + "\",\"" + phoneNumber + "\",\"" + s\
            killsAndInterests + "\",\"" + comments + "\"\n" );

                   }
                   catch ( IOException myIOE ) {
                       myIOE.printStackTrace();
                   }
                 }
              }
            }
        }

      }
      catch ( NoSuchProviderException e ) {
    e.printStackTrace();
      }
      catch ( MessagingException e ) {
          e.printStackTrace();
      }

      try {
          myWriter.close();
      }
      catch ( IOException myIOE ) {
          myIOE.printStackTrace();
      }

   }
}

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。

上一篇:java使用ImageReader判断图片的类型

下一篇:Java提取汉字的拼音