/
MailParser.java
67 lines (58 loc) · 2.18 KB
/
MailParser.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
package io.github.trashemail.utils;
import io.github.trashemail.Telegram.ForwardMailsToTelegram;
import org.apache.commons.mail.util.MimeMessageParser;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.internet.MimeMessage;
import java.util.Date;
public class MailParser {
private String to;
private String from;
private String subject;
private String content;
private Date date;
public MailParser(Message message) throws Exception {
this.content="";
this.from="";
this.to="";
this.subject="";
for(Address a : message.getFrom())
this.from += a.toString() + ", ";
for(Address a : message.getAllRecipients())
this.to += a.toString() + ", ";
this.subject = message.getSubject();
MimeMessageParser messageParser = new MimeMessageParser((MimeMessage) message);
messageParser.parse();
// This block is to check whether mail contains plain text or html entities
// In case of html, with hasHtmlContent(), hasPlainContent() is also giving true, hence two conditions
// Also, getPlainContent().isEmpty() is to confirm that mail has html entities,
if (messageParser.hasPlainContent() && !messageParser.hasHtmlContent()){
this.content = messageParser.getPlainContent();
} else {
if (messageParser.getPlainContent().isEmpty()) {
this.content = org.jsoup.Jsoup.parse(messageParser.getHtmlContent()).text();
} else{
this.content = messageParser.getPlainContent();
}
}
this.date = message.getSentDate();
}
@Override
public String toString() {
String mailData = String.format("" +
"To : %s\n" +
"From : %s\n" +
"Date : %s\n" +
"=========================================\n" +
"Subject : %s\n" +
"=========================================\n\n" +
"%s",
this.to,
this.from,
this.date,
this.subject,
this.content
);
return mailData;
}
}