-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolutions_Programming_Assignment_2.html
114 lines (100 loc) · 3.82 KB
/
Solutions_Programming_Assignment_2.html
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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0120)file://C:\Documents%20and%20Settings\Student\Local%20Settings\Temporary%20Internet%20Files\OLKF\Solution-MailClient.html -->
<HTML><HEAD><TITLE>Solution for the Mail Client Lab</TITLE>
<META http-equiv=Content-Type content="text/html; charset=windows-1252">
<META content="MSHTML 6.00.2800.1400" name=GENERATOR></HEAD>
<BODY>
<H2>Solution for the Mail Client Lab</H2>
<P>The following code presents one solution for the mail client lab. It includes
only the SMTPConnection.java class, since the other classes do not need to be
modified in the basic lab.
<H2>SMTPConnection.java</H2><PRE>import java.net.*;
import java.io.*;
import java.util.*;
/* $Id: SMTPConnection.java,v 1.1.1.1 2003/09/30 14:36:01 kangasha Exp $ */
/**
* Open an SMTP connection to a remote machine and send one mail.
*
* @author Jussi Kangasharju
*/
public class SMTPConnection {
/* The socket to the server */
public Socket connection;
/* Streams for reading and writing the socket */
public BufferedReader fromServer;
public DataOutputStream toServer;
/* Just to make it look nicer */
private static final int SMTP_PORT = 25;
private static final String CRLF = "\r\n";
/* Are we connected? Used in close() to determine what to do. */
private boolean isConnected = false;
/* Create an SMTPConnection object. Create the socket and the
associated streams. Send HELO-command and check for errors. */
public SMTPConnection(Envelope envelope) throws IOException {
connection = new Socket(envelope.DestAddr, SMTP_PORT);
fromServer = new
BufferedReader(new InputStreamReader(connection.getInputStream()));
toServer = new DataOutputStream(connection.getOutputStream());
String reply = fromServer.readLine();
if(parseReply(reply) != 220) {
System.out.println("Error in connect.");
System.out.println(reply);
return;
}
String localhost = (InetAddress.getLocalHost()).getHostName();
try {
sendCommand("HELO " + localhost, 250);
} catch (IOException e) {
System.out.println("HELO failed. Aborting.");
return;
}
isConnected = true;
}
/* Send the message. Simply writes the correct SMTP-commands in the
correct order. No checking for errors, just throw them to
the caller. */
public void send(Envelope envelope) throws IOException {
sendCommand("MAIL FROM:<" + envelope.Sender + ">", 250);
sendCommand("RCPT TO:<" + envelope.Recipient + ">", 250);
sendCommand("DATA", 354);
sendCommand(envelope.Message.toString() + CRLF + ".", 250);
}
/* Close the connection. Try to send QUIT-commmand and then close
the socket. */
public void close() {
isConnected = false;
try {
sendCommand("QUIT", 221);
connection.close();
} catch (IOException e) {
System.out.println("Unable to close connection: " + e);
isConnected = true;
}
}
/* Send an SMTP command to the server. Check for reply code. Does
not check for multiple reply codes (required for RCPT TO). */
private void sendCommand(String command, int rc) throws IOException {
String reply = null;
toServer.writeBytes(command + CRLF);
reply = fromServer.readLine();
if(parseReply(reply) != rc) {
System.out.println("Error in command: " + command);
System.out.println(reply);
throw new IOException();
}
}
/* Parse the reply line from the server. Returns the reply code. */
private int parseReply(String reply) {
StringTokenizer parser = new StringTokenizer(reply);
String replycode = parser.nextToken();
return (new Integer(replycode)).intValue();
}
/* Destructor. Closes the connection if something bad happens. */
protected void finalize() throws Throwable {
if(isConnected) {
close();
}
super.finalize();
}
}
</PRE></BODY></HTML>