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

# User Rev Content
1 tdb 1.1 //---PACKAGE DECLARATION---
2 tdb 1.6 package uk.org.iscream.cms.server.util;
3 tdb 1.1
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 tdb 1.8 * @author $Author: tdb $
13     * @version $Id: Smtp.java,v 1.7 2002/02/07 17:57:06 tdb Exp $
14 tdb 1.1 */
15     public class Smtp {
16    
17     //---FINAL ATTRIBUTES---
18    
19     /**
20     * The current CVS revision of this class
21     */
22 tdb 1.8 public static final String REVISION = "$Revision: 1.7 $";
23 tdb 1.1
24     //---STATIC METHODS---
25    
26     //---CONSTRUCTORS---
27    
28     /**
29 tdb 1.3 * Connects to the specified SMTP server, on the default
30     * (and standard) port 25.
31 tdb 1.1 *
32     * @param server The SMTP server to use
33 tdb 1.3 * @throws IOException if the connection to the server fails
34 tdb 1.1 */
35     public Smtp(String server) throws IOException {
36     this(server, 25);
37     }
38 tdb 1.3
39 tdb 1.1 /**
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 tdb 1.3 * @throws IOException if the connection to the server fails
45 tdb 1.1 */
46     public Smtp(String server, int port) throws IOException {
47     _socket = new Socket(server, port);
48     _socketIn = new BufferedReader(new InputStreamReader(_socket.getInputStream()));
49 tdb 1.3 _socketOut = new PrintWriter(_socket.getOutputStream(), true);
50 tdb 1.8 init();
51 tdb 1.1 }
52    
53     //---PUBLIC METHODS---
54    
55     /**
56 tdb 1.3 * Closes down the connection to the server
57     *
58     * @throws IOException if the command fails
59 tdb 1.1 */
60     public void close() throws IOException {
61     sendCommand("QUIT", 221);
62     _socketIn.close();
63     _socketOut.close();
64     _socket.close();
65     }
66 tdb 1.3
67 tdb 1.1 /**
68 tdb 1.3 * 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 tdb 1.1 */
73 tdb 1.3 public void setSender(String sender) throws IOException {
74     sendCommand("MAIL FROM: <" + sender + ">", 250);
75 tdb 1.1 }
76 tdb 1.3
77 tdb 1.1 /**
78 tdb 1.3 * 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 tdb 1.1 */
83 tdb 1.3 public void setTo(String to) throws IOException {
84 tdb 1.1 sendCommand("RCPT TO: <" + to + ">", 250);
85     }
86 tdb 1.3
87 tdb 1.1 /**
88 tdb 1.3 * 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 tdb 1.1 */
94     public PrintWriter getOutputStream() throws IOException {
95     sendCommand("DATA", 354);
96     return _socketOut;
97     }
98 tdb 1.3
99 tdb 1.1 /**
100 tdb 1.3 * Completes and sends the current message
101     *
102     * @throws IOException if the command fails
103 tdb 1.1 */
104     public void sendMessage() throws IOException {
105     sendCommand(".", 250);
106     }
107 tdb 1.3
108 tdb 1.1 /**
109     * Sends a command to the server
110     *
111     * @param cmd The command to send
112     * @param reply The expected reply-code
113 tdb 1.3 * @throws IOException if the incorrect response code is received
114 tdb 1.1 */
115     public void sendCommand(String cmd, int reply) throws IOException {
116     _socketOut.println(cmd);
117     String temp = _socketIn.readLine();
118 tdb 1.7 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 tdb 1.1 throw new IOException ("Expected " + reply + ", got " + temp);
123     }
124     }
125 tdb 1.3
126 tdb 1.1 /**
127     * Overrides the {@link java.lang.Object#toString() Object.toString()}
128     * method to provide clean logging (every class should have this).
129     *
130 tdb 1.6 * This uses the uk.org.iscream.cms.server.util.FormatName class
131 tdb 1.1 * 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 tdb 1.8
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 tdb 1.1
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 tdb 1.3
179     /**
180     * A reference to the Reader connected to the server
181     */
182 tdb 1.1 private BufferedReader _socketIn;
183 tdb 1.3
184     /**
185     * A reference to the Writer connected to the server
186     */
187 tdb 1.1 private PrintWriter _socketOut;
188 tdb 1.3
189     /**
190     * A reference to the Socket connected to the server
191     */
192 tdb 1.1 private Socket _socket;
193    
194     //---STATIC ATTRIBUTES---
195    
196     }
197