java使用IMAP连接Gmail并解析邮件详解编程语言

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 <[email protected]>" ) ) { 
                 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(); 
      } 
 
   } 
}

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/11048.html

(0)
上一篇 2021年7月19日
下一篇 2021年7月19日

相关推荐

发表回复

登录后才能评论