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.9
Committed: Sat May 18 18:16:04 2002 UTC (21 years, 11 months ago) by tdb
Branch: MAIN
Changes since 1.8: +23 -4 lines
Log Message:
i-scream is now licensed under the GPL. I've added the GPL headers to every
source file, and put a full copy of the license in the appropriate places.
I think I've covered everything. This is going to be a mad commit ;)

File Contents

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