ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/util/uk/org/iscream/cms/util/Smtp.java
Revision: 1.8
Committed: Fri Feb 8 16:09:27 2002 UTC (22 years, 2 months ago) by tdb
Branch: MAIN
Changes since 1.7: +24 -5 lines
Log Message:
Made the SMTP connection code more sane.

File Contents

# Content
1 //---PACKAGE DECLARATION---
2 package uk.org.iscream.cms.server.util;
3
4 //---IMPORTS---
5 import java.net.*;
6 import java.io.*;
7
8 /**
9 * The Simple Mail Transfer Protocol class. This class was borrowed
10 * for the GJT, and scaled to do just what we require.
11 *
12 * @author $Author: tdb $
13 * @version $Id: Smtp.java,v 1.7 2002/02/07 17:57:06 tdb Exp $
14 */
15 public class Smtp {
16
17 //---FINAL ATTRIBUTES---
18
19 /**
20 * The current CVS revision of this class
21 */
22 public static final String REVISION = "$Revision: 1.7 $";
23
24 //---STATIC METHODS---
25
26 //---CONSTRUCTORS---
27
28 /**
29 * Connects to the specified SMTP server, on the default
30 * (and standard) port 25.
31 *
32 * @param server The SMTP server to use
33 * @throws IOException if the connection to the server fails
34 */
35 public Smtp(String server) throws IOException {
36 this(server, 25);
37 }
38
39 /**
40 * Connects to the specified SMTP server on a given port
41 *
42 * @param server The SMTP server to use
43 * @param port The SMTP server port
44 * @throws IOException if the connection to the server fails
45 */
46 public Smtp(String server, int port) throws IOException {
47 _socket = new Socket(server, port);
48 _socketIn = new BufferedReader(new InputStreamReader(_socket.getInputStream()));
49 _socketOut = new PrintWriter(_socket.getOutputStream(), true);
50 init();
51 }
52
53 //---PUBLIC METHODS---
54
55 /**
56 * Closes down the connection to the server
57 *
58 * @throws IOException if the command fails
59 */
60 public void close() throws IOException {
61 sendCommand("QUIT", 221);
62 _socketIn.close();
63 _socketOut.close();
64 _socket.close();
65 }
66
67 /**
68 * Specify who the message is from
69 *
70 * @param sender the e-mail address of the sender
71 * @throws IOException if the command fails
72 */
73 public void setSender(String sender) throws IOException {
74 sendCommand("MAIL FROM: <" + sender + ">", 250);
75 }
76
77 /**
78 * Specify who the message is to be sent to
79 *
80 * @param to the e-mail address of the receiver
81 * @throws IOException if the command fails
82 */
83 public void setTo(String to) throws IOException {
84 sendCommand("RCPT TO: <" + to + ">", 250);
85 }
86
87 /**
88 * Gets the PrintWriter allowing data to be sent
89 * directly to the SMTP server
90 *
91 * @return a reference to the PrintWriter
92 * @throws IOException if the command fails
93 */
94 public PrintWriter getOutputStream() throws IOException {
95 sendCommand("DATA", 354);
96 return _socketOut;
97 }
98
99 /**
100 * Completes and sends the current message
101 *
102 * @throws IOException if the command fails
103 */
104 public void sendMessage() throws IOException {
105 sendCommand(".", 250);
106 }
107
108 /**
109 * Sends a command to the server
110 *
111 * @param cmd The command to send
112 * @param reply The expected reply-code
113 * @throws IOException if the incorrect response code is received
114 */
115 public void sendCommand(String cmd, int reply) throws IOException {
116 _socketOut.println(cmd);
117 String temp = _socketIn.readLine();
118 if (temp == null) {
119 throw new IOException ("IO error reading from socket, connection to server died?");
120 }
121 if (!temp.startsWith(String.valueOf(reply))) {
122 throw new IOException ("Expected " + reply + ", got " + temp);
123 }
124 }
125
126 /**
127 * Overrides the {@link java.lang.Object#toString() Object.toString()}
128 * method to provide clean logging (every class should have this).
129 *
130 * This uses the uk.org.iscream.cms.server.util.FormatName class
131 * to format the toString()
132 *
133 * @return the name of this class and its CVS revision
134 */
135 public String toString() {
136 return FormatName.getName(
137 _name,
138 getClass().getName(),
139 REVISION);
140 }
141
142 //---PRIVATE METHODS---
143
144 /**
145 * Check the server sends a 220 message, and
146 * then send our HELO.
147 *
148 * @throws IOException if something goes wrong
149 */
150 private void init() throws IOException {
151 String temp = _socketIn.readLine();
152 // skip over any 220- lines
153 while(temp != null && temp.startsWith("220-")) {
154 temp = _socketIn.readLine();
155 }
156 // check if we got a 220 welcome header
157 if(temp == null || !temp.startsWith("220 ")) {
158 throw new IOException ("Server did not initialise with a 220 message");
159 }
160 // server gave a 220, it's ok to do a HELO
161 sendCommand("HELO " + InetAddress.getLocalHost().getHostName(), 250);
162 }
163
164 //---ACCESSOR/MUTATOR METHODS---
165
166 //---ATTRIBUTES---
167
168 /**
169 * This is the friendly identifier of the
170 * component this class is running in.
171 * eg, a Filter may be called "filter1",
172 * If this class does not have an owning
173 * component, a name from the configuration
174 * can be placed here. This name could also
175 * be changed to null for utility classes.
176 */
177 private String _name = null;
178
179 /**
180 * A reference to the Reader connected to the server
181 */
182 private BufferedReader _socketIn;
183
184 /**
185 * A reference to the Writer connected to the server
186 */
187 private PrintWriter _socketOut;
188
189 /**
190 * A reference to the Socket connected to the server
191 */
192 private Socket _socket;
193
194 //---STATIC ATTRIBUTES---
195
196 }
197