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

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
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();
      }
 
   }
}