forked from lugnitdgp/TDoC-MailScape
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MailRetriever.java
159 lines (133 loc) · 6.19 KB
/
MailRetriever.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
150
151
152
153
154
155
156
157
158
159
package mailscape;
import org.jetbrains.annotations.NotNull;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMultipart;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import java.util.Scanner;
public class MailRetriever {
public static void main(String[] args) throws FileNotFoundException {
String protocol = "imap";
String host = "imap.gmail.com";
String user_name = Credentials.getCredentials()[0];
String password = Credentials.getCredentials()[1];
String port = "993";
getEmails(protocol, host, port, user_name, password, 3);
}
public static @NotNull
Properties getServerProperties(String protocol, String host, String port) {
Properties properties = new Properties();
// server settings
properties.put(String.format("mail.%s.host", protocol), host);
properties.put(String.format("mail.%s.port", protocol), host);
// SSL settings
properties.setProperty(String.format("mail.%s.socketFactory.class", protocol), "javax.net.ssl" +
".SSLSocketFactory");
properties.setProperty(String.format("mail.%s.socketFactory.fallback", protocol), "false");
properties.setProperty(String.format("mail.%s.socketFactory.port", protocol), String.valueOf(port));
properties.setProperty("mail.imaps.partialfetch", "false");
return properties;
}
public static void getEmails(String protocol, String host, String port, String user_name, String password,
int mail_count) {
String[] fromList = new String[mail_count];
String[] subjectList = new String[mail_count];
String[] bodyList = new String[mail_count];
Properties properties = getServerProperties(protocol, host, port);
Session session = Session.getDefaultInstance(properties);
try {
//connects to the message store
Store store = session.getStore(protocol);
store.connect(user_name, password);
//opens the inbox folder
Folder folderInbox = store.getFolder("[Gmail]/All Mail");
folderInbox.open(Folder.READ_WRITE);
//fetches new messages from server
Message[] messages = folderInbox.getMessages();
int n = messages.length;
for (int i = n - 1; i >= n - mail_count; i--) {
Message msg = messages[i];
InternetAddress sender = (InternetAddress) msg.getFrom()[0];
String from = sender.getAddress();
String subject = msg.getSubject();
String bodypart = "";
if (msg.isMimeType("text/plain")) {
bodypart = msg.getContent().toString();
}
if (msg.isMimeType("multipart/*")) {
try {
MimeMultipart mimeMultipart = (MimeMultipart) msg.getContent();
bodypart = getTextFromMimeMultipart(mimeMultipart).toString();
} catch (IOException e) {
e.printStackTrace();
}
}
bodypart = bodypart.replaceAll("(?m)^[ \t]*\r?\n", "");
fromList[n-1-i] = from;
subjectList[n-1-i] = subject;
bodyList[n-1-i] = bodypart;
System.out.println();
System.out.println(n - i + ")");
System.out.println("FROM : " + from);
System.out.println("SUBJECT : " + subject);
System.out.println("BODY : " + bodypart);
System.out.println();
}
System.out.print("Enter mail number to star: ");
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
starMail(fromList[x-1],subjectList[x-1], bodyList[x-1]);
// disconnect
folderInbox.close(false);
store.close();
} catch (MessagingException | IOException e) {
e.printStackTrace();
}
}
public static @NotNull StringBuilder getTextFromMimeMultipart(@NotNull MimeMultipart mimeMultipart) throws MessagingException {
StringBuilder result = new StringBuilder();
int count = mimeMultipart.getCount();
try {
for (int i = 0; i < count; i++) {
BodyPart bodyPart = mimeMultipart.getBodyPart(i);
if (bodyPart.isMimeType("text/plain")) {
result.append(bodyPart.getContent());
break;
} else if (bodyPart.isMimeType("text/html")) {
String html = (String) bodyPart.getContent();
result.append(org.jsoup.Jsoup.parse(html).text());
} else if (bodyPart.getContent() instanceof MimeMultipart)
result.append(getTextFromMimeMultipart((MimeMultipart) bodyPart.getContent()));
}
} catch (MessagingException | IOException e) {
e.printStackTrace();
}
return result;
}
public static void starMail(String from, String subject, String body) {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
String[] credentials = Credentials.getCredentials();
Connection con = DriverManager.getConnection(credentials[2], credentials[3], credentials[4]);
Statement statement = con.createStatement();
body = body.replace("'", "\\'");
String sqlInsert =
"INSERT INTO star_mail (sender, subject, body) VALUES ( '" + from + "', '" + subject + "', '" + body +
"');";
System.out.println(sqlInsert);
int countInserted = statement.executeUpdate(sqlInsert);
if (countInserted != 0) {
System.out.println("Email is starred successfully");
}
con.close();
} catch (ClassNotFoundException | FileNotFoundException | SQLException e) {
e.printStackTrace();
}
}
}