KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lutris > airsent > business > email > Email


1 /*
2  * Copyright (c) 1999-2001 Lutris Technologies, Inc. All Rights
3  * Reserved.
4  *
5  * This source code file is distributed by Lutris Technologies, Inc. for
6  * use only by licensed users of product(s) that include this source
7  * file. Use of this source file or the software that uses it is covered
8  * by the terms and conditions of the Lutris Enhydra Development License
9  * Agreement included with this product.
10  *
11  * This Software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF
12  * ANY KIND, either express or implied. See the License for the specific terms
13  * governing rights and limitations under the License.
14  *
15  * Contributor(s):
16  *
17  * $Id: Email.java,v 1.1 2004/08/16 09:33:11 slobodan Exp $
18  */

19
20 package com.lutris.airsent.business.email;
21
22 import java.io.*;
23 import java.net.Socket JavaDoc;
24 import java.util.Vector JavaDoc;
25
26 /**
27  * Utility class used to send email.
28  *
29  * @author Jason Abbott <jason.abbott@lutris.com>
30  * @author Joseph Shoop
31  */

32 public class Email {
33     private OutputStreamWriter out = null;
34     private BufferedReader in = null;
35     private Socket JavaDoc mailserver = null;
36     private InputStreamReader isr = null;
37     private String JavaDoc mailServerURL = "localhost";
38
39     /**
40      * Constructor with mail server name.
41      *
42      *
43      * @param mailserverURL
44      *
45      *
46      */

47     public Email(String JavaDoc mailServer) throws IOException {
48     this.mailServerURL = mailServer;
49     }
50
51     /**
52      * Writes the email to output stream.
53      *
54      *
55      * @param text
56      *
57      * @return
58      *
59      * @throws IOException
60      *
61      *
62      */

63     private String JavaDoc send(String JavaDoc text) throws IOException {
64     
65     out.write(text, 0, text.length());
66     out.flush();
67     
68     // return in.readLine();
69
String JavaDoc retVal = in.readLine();
70     String JavaDoc dump = "x";
71     while ((dump = in.readLine()) == null) {}
72     if (retVal.startsWith("5")) {
73         
74         throw new IOException(mailServerURL + " SMTP Error: " + retVal);
75     }
76     
77     return retVal;
78     }
79     
80     /**
81      * Opens a socket connection to the mail server.
82      *
83      *
84      * @throws IOException
85      *
86      *
87      */

88     private void open() throws IOException {
89     mailserver = new Socket JavaDoc(mailServerURL, 25);
90         isr = new InputStreamReader(mailserver.getInputStream());
91         out = new OutputStreamWriter(mailserver.getOutputStream());
92         in = new BufferedReader(isr);
93     }
94     
95     /**
96      * Closes connection to the mail server.
97      *
98      *
99      * @throws IOException
100      *
101      *
102      */

103     private void close() throws IOException {
104     out.close();
105     in.close();
106     }
107
108     /**
109      * Sends mail.
110      *
111      *
112      * @param domain
113      * @param fromAddress
114      * @param toAddress
115      * @param subject
116      * @param type
117      * @param text
118      *
119      * @throws Exception
120      *
121      *
122      */

123     public void sendMail(String JavaDoc domain, String JavaDoc fromAddress,
124                 String JavaDoc toAddress, String JavaDoc subject, String JavaDoc type,
125                 String JavaDoc text) throws Exception JavaDoc {
126     try {
127         open();
128     } catch (Exception JavaDoc e) {
129         throw new Exception JavaDoc("Error opening socket" + e);
130     }
131
132     // Lets check all of our parameters...
133
// 1.fromAddress
134
if (fromAddress == null) {
135         throw new Exception JavaDoc("We need a from-address that is not null to send email.");
136     }
137
138     fromAddress = fromAddress.trim();
139
140     if (fromAddress.length() == 0) {
141         throw new Exception JavaDoc("We need a from-address that is not empty to send email.");
142     }
143
144     if (fromAddress.indexOf("@") == -1) {
145         throw new Exception JavaDoc("We need a legal from-address to send email, we do not consider \""
146                 + fromAddress + "\" to be legal.");
147
148         // 2.To address
149
}
150
151     if (toAddress == null) {
152         throw new Exception JavaDoc("We need to-addresses that are not null to send email.");
153     }
154
155     toAddress = toAddress.trim();
156
157     if (toAddress.length() == 0) {
158         throw new Exception JavaDoc("We need to-addresses that are not empty to send email.");
159     }
160
161     if (toAddress.indexOf("@") == -1) {
162         throw new Exception JavaDoc("We need legal to-addresses to send email, we do not consider \""
163                 + toAddress + "\" to be legal.");
164
165         // 3.subject
166
}
167
168     if (subject == null) {
169         throw new Exception JavaDoc("We need a non-null subject to send email");
170     }
171
172     subject = subject.trim();
173
174     if (subject.length() == 0) {
175         throw new Exception JavaDoc("We need a non-empty subject to send email");
176     }
177
178     if (type == null) {
179         type = "test/plain";
180     }
181
182     // 4. Text body
183
if (text == null) {
184         throw new Exception JavaDoc("We need a non-null text body to send email");
185     }
186
187     text = text.trim();
188
189     if (text.length() == 0) {
190         throw new Exception JavaDoc("We need a non-empty text body to send email");
191     }
192
193     try {
194         send("EHLO " + domain + "\r\n");
195     } catch (IOException e) {
196         throw new Exception JavaDoc("SMTP Mail Initialization failure");
197     }
198
199     try {
200         send("MAIL FROM: " + fromAddress + "\r\n");
201         send("RCPT TO: " + toAddress + "\r\n");
202     } catch (IOException e) {
203         throw new Exception JavaDoc("SMTP Mail addressing failure");
204     }
205
206     try {
207         
208         //Multipart email
209
String JavaDoc boundary = "AIRSENTBOUNDARY-1";
210         StringBuffer JavaDoc tmp = new StringBuffer JavaDoc();
211         tmp.append("DATA\r\n");
212         tmp.append("MIME-Version: 1.0");
213         tmp.append("\r\n");
214         tmp.append("From: ");
215         tmp.append(fromAddress);
216         tmp.append("\r\n");
217         tmp.append("To: ");
218         tmp.append(toAddress);
219         tmp.append("\r\n");
220         tmp.append("Subject: ");
221         tmp.append(subject);
222         tmp.append("\r\n");
223         tmp.append("Content-Type: multipart/alternative; boundary=\"" + boundary + "\"");
224         tmp.append("\r\n");
225         tmp.append("--" + boundary);
226         tmp.append("\r\n");
227         tmp.append("Content-Type: text/plain; charset=us-ascii;");
228         tmp.append("\r\n");
229         tmp.append("\r\n");
230         tmp.append("AirSent delivery notification:");
231         tmp.append("\r\n");
232         tmp.append(text);
233         tmp.append("\r\n");
234         tmp.append("\r\n");
235         tmp.append("--" + boundary);
236         tmp.append("\r\n");
237         tmp.append("Content-Type: text/html; charset=us-ascii;");
238         tmp.append("\r\n");
239         tmp.append("\r\n");
240         tmp.append(text);
241         tmp.append("\r\n");
242         tmp.append("\r\n");
243         tmp.append("--" + boundary + "--");
244         tmp.append("\r\n" + ".\r\n");
245         //End multipart email
246

247         send(tmp.toString());
248
249     } catch (Exception JavaDoc e) {
250         throw new Exception JavaDoc("SMTP Mail message body failure");
251     }
252
253     try {
254         send("QUIT\r\n");
255         close();
256     } catch (IOException e) {
257         throw new Exception JavaDoc("SMTP Mail signoff failure");
258     }
259     }
260
261 }
262
263
264
265
266
Popular Tags