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.12
Committed: Sun Aug 1 10:41:08 2004 UTC (19 years, 8 months ago) by tdb
Branch: MAIN
CVS Tags: HEAD
Changes since 1.11: +3 -3 lines
Log Message:
Catch a lot of old URL's and update them. Also remove a couple of old files
that aren't used.

File Contents

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