-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSMTPClient.java
More file actions
99 lines (83 loc) · 3.39 KB
/
Copy pathSMTPClient.java
File metadata and controls
99 lines (83 loc) · 3.39 KB
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
import java.io.*;
import java.net.*;
import java.util.Date;
public class SMTPClient {
private static final int DEFAULT_PORT = 25;
private static final int TIMEOUT_MS = 15_000;
private Socket socket;
private BufferedReader reader;
private PrintWriter writer;
public void connect(String host, int port) throws IOException {
socket = new Socket();
socket.connect(new InetSocketAddress(host, port), TIMEOUT_MS);
socket.setSoTimeout(TIMEOUT_MS);
reader = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8"));
writer = new PrintWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF-8"), true);
requireCode(readResponse(), "220", "greeting");
String ehlo = sendCommand("EHLO " + localHostname());
if (!ehlo.startsWith("250")) {
requireCode(sendCommand("HELO " + localHostname()), "250", "HELO");
}
}
public void connect(String host) throws IOException {
connect(host, DEFAULT_PORT);
}
public void disconnect() {
try {
if (socket != null && !socket.isClosed()) {
sendCommand("QUIT");
socket.close();
}
} catch (IOException ignored) {
}
}
public void sendReply(String fromAddress, String toAddress,
String originalSubject, String vacationBody) throws IOException {
requireCode(sendCommand("MAIL FROM:<" + fromAddress + ">"), "250", "MAIL FROM");
requireCode(sendCommand("RCPT TO:<" + toAddress + ">"), "250", "RCPT TO");
requireCode(sendCommand("DATA"), "354", "DATA");
StringBuilder msg = new StringBuilder();
msg.append("From: ").append(fromAddress).append("\r\n");
msg.append("To: ").append(toAddress).append("\r\n");
msg.append("Subject: Re: ").append(originalSubject).append("\r\n");
msg.append("Date: ").append(new Date()).append("\r\n");
msg.append("Auto-Submitted: auto-replied\r\n");
msg.append("Precedence: bulk\r\n");
msg.append("X-Auto-Reply-To: ").append(toAddress).append("\r\n");
msg.append("\r\n");
msg.append(vacationBody).append("\r\n");
for (String line : msg.toString().split("\r\n", -1)) {
writer.print((line.startsWith(".") ? "." + line : line) + "\r\n");
}
writer.print(".\r\n"); // DATA terminator
writer.flush();
requireCode(readResponse(), "250", "end of DATA");
}
private String readResponse() throws IOException {
StringBuilder sb = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
if (line.length() < 4 || line.charAt(3) == ' ')
break;
sb.append("\n");
}
return sb.toString();
}
private String sendCommand(String cmd) throws IOException {
writer.print(cmd + "\r\n");
writer.flush();
return readResponse();
}
private void requireCode(String resp, String code, String cmd) throws IOException {
if (!resp.startsWith(code))
throw new IOException("SMTP '" + cmd + "' expected " + code + ", got: " + resp);
}
private String localHostname() {
try {
return InetAddress.getLocalHost().getHostName();
} catch (UnknownHostException e) {
return "localhost";
}
}
}