forked from RenzDot/COM211_A1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSMTPConnect.java
141 lines (107 loc) · 3.45 KB
/
SMTPConnect.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
/*************************************
* Filename: HttpInteract.java
* Name: Lorenzo Jumilla
Student-ID: 201202403
* Name: Daniel Stevens
Student-ID: 201270161
* Date: 16/10/18
*************************************/
import java.net.*;
import java.io.*;
import java.util.*;
/**
* Open SMTP connection to mailserver & send one mail.
*/
public class SMTPConnect {
private Socket connection;//Socket to server
private String response;
//Read & Write streams
private BufferedReader fromServer;
private DataOutputStream toServer;
private boolean isConnected = false; //Checks connection, used in close()
private static final String CRLF = "\r\n"; //Used to separate commands
public SMTPConnect(EmailMessage mailmessage) throws IOException {
//Open TCP client socket
connection = new Socket(mailmessage.DestHost, mailmessage.DestHostPort);
//Allow fromServer to read socket
InputStream is = connection.getInputStream();
InputStreamReader isr = new InputStreamReader(is);//Decodes bytes into char
fromServer = new BufferedReader(isr);
//Allow toServer to write to socket
toServer = new DataOutputStream( connection.getOutputStream() );
//Check server's response is 220
response = fromServer.readLine();
System.out.println(response);
if (!response.startsWith("220")) {
throw new IOException("220 response not received from server.");
}
//Name of client's computer
String localHost = InetAddress.getLocalHost().getHostName();
//Send handshake
sendCommand("HELO " + localHost, 250);
//test(); // Debug only, remove before submitting project
isConnected = true;
}
//Debug only, remove before submitting project
public void test() throws IOException {
sendCommand("MAIL FROM: <a@a>", 250);
sendCommand("RCPT TO: <b@b>", 250);
sendCommand("DATA", 354);
sendCommand("SUBJECT: TEST" + CRLF + CRLF + "Hi Bob, How's the weather? Alice." + CRLF + ".", 250);
}
//Send an SMTP email
public void send(EmailMessage mailmessage) throws IOException {
sendCommand("MAIL FROM: " + mailmessage.Sender, 250);
sendCommand("RCPT TO: " + mailmessage.Recipient, 250);
sendCommand("DATA", 354);
sendCommand( mailmessage.Headers + CRLF
+ CRLF + CRLF + //Extra CRLF to make message nice
mailmessage.Body + CRLF +
".", 250);
//test();
/* Example Format
MAIL FROM: [email protected]
RCPT TO: [email protected]
DATA
Hi, How are you?
.
QUIT
*/
}
//Quit SMTP connection
public void close() {
isConnected = false;
try {
sendCommand("QUIT", 221); //Close SMTP connection
connection.close(); //Close socket
} catch (IOException e) {
System.out.println("Unable to close connection: " + e);
isConnected = true;
}
}
//sendCommand (<command to send>, <expected response number>)
//Sends SMTP commands to server
private void sendCommand(String command, int rc) throws IOException {
toServer.writeBytes(command + CRLF); //Write command to server
//Read server's response
response = fromServer.readLine();
System.out.println(command + " --> " + response);
if (!response.startsWith("" + rc) ) { //Check if response followed RFC 821
throw new IOException(rc + " response not received from server");
}
/* RFC 821
DATA 354
HELO 250
MAIL FROM 250
QUIT 221
RCPT TO 250
*/
}
//Abort connection if something bad happens
protected void finalize() throws Throwable {
if ( isConnected ) {
close();
};
super.finalize();
}
}