-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEmailMessage.java
164 lines (141 loc) · 4.21 KB
/
EmailMessage.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
160
161
162
163
164
/*************************************
* Filename: EmailMessage.java
* Date: 17/10/2019
*************************************/
import java.util.*;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.*;
public class EmailMessage {
/* SMTP-sender of the message (in this case, contents of From-header. */
public String Sender;
/* SMTP-recipient, or contents of To-header. */
public String Recipient;
/* SMTP-cc, or contents of Cc-header. */
public String Cc;
/* Target MX-host */
public String DestHost;
private InetAddress DestAddr;
public int DestHostPort;
/* The headers and the body of the message. */
public String Headers;
public String Body;
/* To make it look nicer */
private static final String CRLF = "\r\n";
boolean emptyCc = false;
/*
* Create the message object by inserting the required headers from RFC 822
* (From, To, Date).
*/
public EmailMessage(String to, String cc, String subject, String text,
String localServer, int localServerPort) throws UnknownHostException {
/* Remove whitespace */
InetAddress id = InetAddress.getLocalHost();
Sender = System.getProperty("user.name") + "@" + id.getHostName();
Recipient = to.trim();
if (cc.isEmpty()) {
emptyCc = true;
}
else {
Cc = cc.trim();
}
Headers = "From: " + Sender + CRLF;
Headers += "To: " + Recipient + CRLF;
if (!emptyCc) {
Headers += "Cc: " + Cc + CRLF;
}
Headers += "Subject: " + subject.trim() + CRLF;
/*
* A close approximation of the required format. Unfortunately only GMT.
*/
SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss 'GMT'");
String dateString = format.format(new Date());
Headers += "Date: " + dateString + CRLF;
/*
* Get message. We must escape the message to make sure that there are
* no single periods on a line. This would mess up sending the mail.
*/
Body = escapeMessage(text);
/*
* Take the name of the local mailserver and map it into an InetAddress
*/
DestHost = localServer;
try {
DestAddr = InetAddress.getByName(DestHost);
} catch (UnknownHostException e) {
System.out.println("Unknown host: " + DestHost);
System.out.println(e);
throw e;
}
DestHostPort = localServerPort;
}
/*
* Check whether the message is valid. In other words, check that both
* sender and recipient contain only one @-sign.
*/
public boolean isValid() {
int fromat = Sender.indexOf('@');
int toat = Recipient.indexOf('@');
if (fromat < 1 || (Sender.length() - fromat) <= 1) {
System.out.println("Sender address is invalid");
return false;
}
if (fromat != Sender.lastIndexOf('@')) {
System.out.println("Sender address is invalid");
return false;
}
if (toat < 1 || (Recipient.length() - toat) <= 1) {
System.out.println("Recipient address is invalid");
return false;
}
if (toat != Recipient.lastIndexOf('@')) {
System.out.println("Recipient address is invalid");
return false;
}
return true;
}
/* For printing the message. */
public String toString() {
String res;
int countCc=0;
res = "Sender: " + Sender + '\n';
res += "Recipient: " + Recipient + '\n';
if (!emptyCc) {
char ccDelimiter = ',';
for(int i=0;i<Cc.length();i++){
if(Cc.charAt(i) == ccDelimiter){
countCc++;
}
}
if(countCc>0){
String[] ccSplitArray = Cc.split(Character.toString(ccDelimiter), countCc+1);
for(int j=0;countCc>j;j++){
res += "Cc: " + ccSplitArray[j] + '\n';
}
} else {
res += "Cc: " + Cc + '\n';
}
}
res += "MX-host: " + DestHost + ", address: " + DestAddr + '\n';
res += "Message:" + '\n';
res += Headers + CRLF;
res += Body;
return res;
}
/*
* Escape the message by doubling all periods at the beginning of a line.
*/
private String escapeMessage(String body) {
String escapedBody = "";
String token;
StringTokenizer parser = new StringTokenizer(body, "\n", true);
while (parser.hasMoreTokens()) {
token = parser.nextToken();
if (token.startsWith(".")) {
token = "." + token;
}
escapedBody += token;
}
return escapedBody;
}
}