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.1
Committed: Mon Feb 5 03:14:15 2001 UTC (23 years, 2 months ago) by tdb
Branch: MAIN
Log Message:
An SMTP library. Got it from the GJT, but seriously trimmed for our needs.

File Contents

# User Rev Content
1 tdb 1.1 //---PACKAGE DECLARATION---
2     package uk.ac.ukc.iscream.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$
13     * @version $Id$
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.1 $";
23    
24     //---STATIC METHODS---
25    
26     //---CONSTRUCTORS---
27    
28     /**
29     * Connects to the specified SMTP server
30     *
31     * @param server The SMTP server to use
32     */
33     public Smtp(String server) throws IOException {
34     this(server, 25);
35     }
36    
37     /**
38     * Connects to the specified SMTP server on a given port
39     *
40     * @param server The SMTP server to use
41     * @param port The SMTP server port
42     */
43     public Smtp(String server, int port) throws IOException {
44     _socket = new Socket(server, port);
45     _socketIn = new BufferedReader(new InputStreamReader(_socket.getInputStream()));
46     _socketOut = new PrintWriter(_socket.getOutputStream());
47     sendCommand("HELO " + InetAddress.getLocalHost().getHostName(), 250);
48     }
49    
50     //---PUBLIC METHODS---
51    
52     /**
53     * closes the connection
54     */
55     public void close() throws IOException {
56     sendCommand("QUIT", 221);
57     _socketIn.close();
58     _socketOut.close();
59     _socket.close();
60     }
61    
62     /**
63     * Who is this message from? specify with this command.
64     */
65     public void from(String from) throws IOException {
66     sendCommand("MAIL FROM: <" + from + ">", 250);
67     }
68    
69     /**
70     * Who should this message go to? Specify with this command.
71     */
72     public void to(String to) throws IOException {
73     sendCommand("RCPT TO: <" + to + ">", 250);
74     }
75    
76     /**
77     * gets the outputstream
78     */
79     public PrintWriter getOutputStream() throws IOException {
80     sendCommand("DATA", 354);
81     return _socketOut;
82     }
83    
84     /**
85     * Sends current message.
86     */
87     public void sendMessage() throws IOException {
88     sendCommand(".", 250);
89     }
90    
91     /**
92     * Sends a command to the server
93     *
94     * @param cmd The command to send
95     * @param reply The expected reply-code
96     */
97     public void sendCommand(String cmd, int reply) throws IOException {
98     _socketOut.println(cmd);
99     String temp = _socketIn.readLine();
100     if (!temp.startsWith("" + reply)) {
101     throw new IOException ("Expected " + reply + ", got " + temp);
102     }
103     }
104    
105     /**
106     * Overrides the {@link java.lang.Object#toString() Object.toString()}
107     * method to provide clean logging (every class should have this).
108     *
109     * This uses the uk.ac.ukc.iscream.util.FormatName class
110     * to format the toString()
111     *
112     * @return the name of this class and its CVS revision
113     */
114     public String toString() {
115     return FormatName.getName(
116     _name,
117     getClass().getName(),
118     REVISION);
119     }
120    
121     //---PRIVATE METHODS---
122    
123     //---ACCESSOR/MUTATOR METHODS---
124    
125     //---ATTRIBUTES---
126    
127     /**
128     * This is the friendly identifier of the
129     * component this class is running in.
130     * eg, a Filter may be called "filter1",
131     * If this class does not have an owning
132     * component, a name from the configuration
133     * can be placed here. This name could also
134     * be changed to null for utility classes.
135     */
136     private String _name = null;
137    
138     /** from the server */
139     private BufferedReader _socketIn;
140    
141     /** to the server */
142     private PrintWriter _socketOut;
143    
144     /** The socket to use for this connection */
145     private Socket _socket;
146    
147     //---STATIC ATTRIBUTES---
148    
149     }
150