-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmail.java
128 lines (116 loc) · 4.09 KB
/
mail.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
package org.webdriver.seleniumUI.utils;
import lombok.Getter;
import lombok.Setter;
import lombok.SneakyThrows;
import javax.mail.*;
import java.io.IOException;
import java.util.Properties;
public class Mail {
private String host;
private String emailId;
private String emailPwd;
private String folder;
private static Store store;
private String port;
@Getter @Setter
private String title;
public Mail(String host, String emailId, String emailPwd, String folder, String port) {
this.host = host;
this.emailId = emailId;
this.emailPwd = emailPwd;
this.folder = folder;
this.port = port;
}
public Mail(String emailPwd, String folder) {
this.emailPwd = emailPwd;
this.folder = folder;
}
// cisco = mail.cisco.com
private void connectToMail() throws MessagingException {
Properties properties = new Properties();
properties.put("mail.imap.host", host);
properties.put("mail.imap.port", port);
properties.put("mail.imap.ssl.trust", "*");
properties.put("mail.imap.starttls.enable", "true");
Session emailSession = Session.getInstance(properties);
store = emailSession.getStore("imaps");
store.connect(host, emailId, emailPwd);
}
@SneakyThrows ({IOException.class, MessagingException.class})
public String readInbox(Boolean delete) {
connectToMail();
Folder emailFolder = store.getFolder(folder);
emailFolder.open(Folder.READ_WRITE);
Message[] messages = emailFolder.getMessages();
String emailMessage = "";
for (Message message : messages) {
emailMessage = getMessage(message);
title = message.getSubject();
if (delete) {
message.setFlag(Flags.Flag.DELETED, true);
}
}
emailFolder.close(true);
store.close();
return emailMessage;
}
private String getMessage(Part p) throws MessagingException, IOException {
if (p.isMimeType("text/*")) {
String s = (String) p.getContent();
boolean textIsHtml = p.isMimeType("text/html");
return s;
}
if (p.isMimeType("multipart/alternative")) {
// prefer html text over plain text
Multipart mp = (Multipart) p.getContent();
String text = null;
for (int i = 0; i < mp.getCount(); i++) {
Part bp = mp.getBodyPart(i);
if (bp.isMimeType("text/plain")) {
if (text == null)
text = getMessage(bp);
continue;
} else if (bp.isMimeType("text/html")) {
String s = getMessage(bp);
if (s != null)
return s;
} else {
return getMessage(bp);
}
}
return text;
} else if (p.isMimeType("multipart/*")) {
Multipart mp = (Multipart) p.getContent();
for (int i = 0; i < mp.getCount(); i++) {
String s = getMessage(mp.getBodyPart(i));
if (s != null)
return s;
}
}
return null;
}
private String getText(Part part) throws
MessagingException, IOException {
StringBuilder textBuilder = new StringBuilder();
if (part.isMimeType("text/plain")) {
textBuilder.append((String) part.getContent());
return (String) part.getContent();
}
Multipart multipart = (Multipart) part.getContent();
for (int i = 0; i < multipart.getCount(); i++) {
String text = getText(multipart.getBodyPart(i));
if (text != null)
textBuilder.append(text);
return text;
}
String text = textBuilder.toString();
return text;
}
public Mail gmail(){
return new Mail("imap.gmail.com",
this.emailPwd,
this.folder,
"993");
}
}