-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFetchingEmail.java
149 lines (116 loc) · 4.1 KB
/
FetchingEmail.java
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package main;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Date;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.NoSuchProviderException;
import javax.mail.Part;
import javax.mail.Session;
import javax.mail.Store;
public class FetchingEmail {
public static void fetch(String pop3Host, String storeType, String user, String password) {
try {
Properties props = new Properties();
props.put("mail.store.protocol", "pop3");
props.put("mail.pop3.host", pop3Host);
props.put("mail.pop3.port", "995");
props.put("mail.pop3.starttls.enable", "true");
Session emailSession = Session.getDefaultInstance(props);
Store store = emailSession.getStore("pop3s");
store.connect(pop3Host, user, password);
Folder emailFolder = store.getFolder("INBOX");
emailFolder.open(Folder.READ_ONLY);
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
Message [] messages = emailFolder.getMessages();
System.out.println("messages.length = " + messages.length);
for (Message message: messages) {
System.out.println("---------------------------------");
writePart(message);
String line = reader.readLine();
if ("YES".equals(line)) {
message.writeTo(System.out);
} else if ("QUIT".equals(line)) {
break;
}
}
emailFolder.close(false);
store.close();
}
catch (NoSuchProviderException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String [] args) {
String host = "pop.gmail.com";// change accordingly
String mailStoreType = "pop3";
String username = "[email protected]";// change accordingly
String password = "Samp7@88";// change accordingly
//Call method fetch
fetch(host, mailStoreType, username, password);
}
public static void writePart(Part p) throws Exception{
if(p instanceof Message)
writeEnvelope((Message) p);
System.out.println("---");
System.out.println("CONTENT-TYPE: " + p.getContentType());
if(p.isMimeType("text/plain")) {
System.out.println("PLAIN TEXT MESSAGE");
System.out.println("---");
System.out.println((String) p.getContent());
}
else if(p.isMimeType("multipart/*")) {
System.out.println("MULTIPART MSG");
System.out.println("---");
Multipart mp = ((Multipart) p.getContent());
for(int i = 0; i < mp.getCount(); i++)
writePart(mp.getBodyPart(i));
}
else if(p.isMimeType("message/rfc822")){
System.out.println("NESTED MSG");
System.out.println("---");
writePart((Part) p.getContent());
}
else if(p.isMimeType("image/jpeg")) {
System.out.println("JPEG IMAGE");
System.out.println("---");
}
else {
System.out.println("UNKOWN TYPE");
}
}
public static void writeEnvelope(Message m) throws Exception {
System.out.println("This is the message envelope");
System.out.println("---------------------------");
Address[] a;
// FROM
if ((a = m.getFrom()) != null) {
for (int j = 0; j < a.length; j++)
System.out.println("FROM: " + a[j].toString());
}
// TO
if ((a = m.getRecipients(Message.RecipientType.TO)) != null) {
for (int j = 0; j < a.length; j++)
System.out.println("TO: " + a[j].toString());
}
// SUBJECT
if (m.getSubject() != null)
System.out.println("SUBJECT: " + m.getSubject());
}
}