KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > net > SMTP


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17
18 /* $Id: SMTP.java 190390 2005-06-13 12:31:46Z edith $ */
19
20 package org.apache.lenya.net;
21
22 import java.io.BufferedReader JavaDoc;
23 import java.io.BufferedWriter JavaDoc;
24 import java.io.DataInputStream JavaDoc;
25 import java.io.FileReader JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.OutputStreamWriter JavaDoc;
28 import java.io.PrintWriter JavaDoc;
29 import java.net.ConnectException JavaDoc;
30 import java.net.Socket JavaDoc;
31 import java.util.StringTokenizer JavaDoc;
32
33 import org.apache.log4j.Category;
34
35
36 /**
37  * DOCUMENT ME!
38  * @deprecated use cocoon mail block
39  */

40 public class SMTP {
41     static Category log = Category.getInstance(SMTP.class);
42     String JavaDoc host = null;
43     int port;
44     String JavaDoc domain = null;
45     Socket JavaDoc socket = null;
46     PrintWriter JavaDoc out = null;
47     DataInputStream JavaDoc in = null;
48     String JavaDoc errlog = null;
49     String JavaDoc from = null;
50     String JavaDoc to = null;
51     String JavaDoc reply_to = null;
52     String JavaDoc cc = null;
53     String JavaDoc[] ccs = null;
54     String JavaDoc bcc = null;
55     String JavaDoc[] bccs = null;
56     String JavaDoc subject = null;
57     String JavaDoc data = null;
58
59     /**
60      *
61      */

62     public SMTP() {
63         Configuration conf = new Configuration();
64         host = conf.smtpHost;
65         port = new Integer JavaDoc(conf.smtpPort).intValue();
66         domain = conf.smtpDomain;
67         log.debug(host + ":" + port + " (" + domain + ")");
68     }
69
70
71     /**
72      * DOCUMENT ME!
73      *
74      * @param from DOCUMENT ME!
75      * @param to DOCUMENT ME!
76      * @param cc DOCUMENT ME!
77      * @param bcc DOCUMENT ME!
78      * @param subject DOCUMENT ME!
79      * @param body DOCUMENT ME!
80      */

81     public void send(String JavaDoc from, String JavaDoc to, String JavaDoc cc, String JavaDoc bcc, String JavaDoc subject, String JavaDoc body) {
82         From(from);
83         Reply_To(from);
84         To(to);
85         Cc(cc);
86         Bcc(bcc);
87         Subject(subject);
88         DATA(body);
89         send();
90     }
91
92     /**
93      * DOCUMENT ME!
94      */

95     public void send() {
96         errlog = "";
97
98         try {
99             socket = new Socket JavaDoc(host, port);
100             out = new PrintWriter JavaDoc(new BufferedWriter JavaDoc(new OutputStreamWriter JavaDoc(socket.getOutputStream(), "UTF-8")), true);
101             in = new DataInputStream JavaDoc(socket.getInputStream());
102             
103             errlog = errlog + getResponse(220);
104
105             errlog = errlog + "HELO " + domain + "\n";
106             out.println("HELO " + domain);
107             errlog = errlog + getResponse(250);
108
109             errlog = errlog + "MAIL FROM:<" + from + ">\n";
110             out.println("MAIL FROM:<" + from + ">");
111             errlog = errlog + getResponse(250);
112
113             errlog = errlog + "RCPT TO:<" + to + ">\n";
114             out.println("RCPT TO:<" + to + ">");
115             errlog = errlog + getResponse(250);
116
117             for (int i = 0; i < ccs.length; i++) {
118                 errlog = errlog + "RCPT TO:<" + ccs[i] + ">\n";
119                 out.println("RCPT TO:<" + ccs[i] + ">");
120                 errlog = errlog + getResponse(250);
121             }
122
123             for (int i = 0; i < bccs.length; i++) {
124                 errlog = errlog + "RCPT TO:<" + bccs[i] + ">\n";
125                 out.println("RCPT TO:<" + bccs[i] + ">");
126                 errlog = errlog + getResponse(250);
127             }
128
129             errlog = errlog + "DATA\n";
130             out.println("DATA");
131             errlog = errlog + getResponse(354);
132
133             errlog = errlog + "From: " + from + "\n";
134             out.println("From: " + from);
135             errlog = errlog + "To: " + to + "\n";
136             out.println("To: " + to);
137             errlog = errlog + "Reply-To: " + reply_to + "\n";
138             out.println("Reply-To: " + reply_to);
139
140             if (cc != null) {
141                 errlog = errlog + "Cc: " + cc + "\n";
142                 out.println("Cc: " + cc);
143             }
144
145             if (bcc != null) {
146                 errlog = errlog + "Bcc: " + bcc + "\n";
147                 out.println("Bcc: " + bcc);
148             }
149
150             errlog = errlog + "Subject: " + subject + "\n";
151             out.println("Subject: " + subject);
152
153             errlog = errlog + "MIME-Version: 1.0\n";
154             out.println("MIME-Version: 1.0");
155
156             errlog = errlog + "Content-Type: text/plain; charset=UTF-8; format=flowed\n";
157             out.println("Content-Type: text/plain; charset=UTF-8; format=flowed");
158
159             errlog = errlog + "Content-Transfer-Encoding: 8bit\n";
160             out.println("Content-Transfer-Encoding: 8bit");
161             
162             errlog = errlog + data + "\n.\n";
163             out.println(data + "\n.");
164             errlog = errlog + getResponse(250);
165
166             errlog = errlog + "QUIT\n";
167             out.println("QUIT");
168             errlog = errlog + getResponse(221);
169             log.debug(errlog);
170         } catch (ConnectException JavaDoc e) {
171             log.error(".send(): " + e + " (sendmail is probably not running)");
172
173             return;
174         } catch (Exception JavaDoc e) {
175             log.error(".send(): " + e);
176         }
177
178         try {
179             in.close();
180             out.close();
181             socket.close();
182         } catch (IOException JavaDoc e) {
183             log.error(this.getClass().getName() + ".send(): " + e);
184         }
185     }
186
187     private String JavaDoc getResponse(int value) throws IOException JavaDoc {
188         try {
189             Thread.sleep(200);
190         } catch (InterruptedException JavaDoc e) {
191         }
192
193         return readLine(in);
194     }
195
196     private String JavaDoc readLine(DataInputStream JavaDoc in) throws IOException JavaDoc {
197         StringBuffer JavaDoc line = new StringBuffer JavaDoc("");
198
199         while (in.available() > 0) {
200             char character = (char) in.read();
201             line.append(character);
202         }
203
204         String JavaDoc response = new String JavaDoc(line);
205
206         return response;
207     }
208
209     /**
210      * DOCUMENT ME!
211      *
212      * @param data DOCUMENT ME!
213      */

214     public void DATA(String JavaDoc data) {
215         this.data = data;
216     }
217
218     /**
219      * DOCUMENT ME!
220      *
221      * @param from DOCUMENT ME!
222      */

223     public void From(String JavaDoc from) {
224         this.from = from;
225     }
226
227     /**
228      * DOCUMENT ME!
229      *
230      * @param to DOCUMENT ME!
231      */

232     public void To(String JavaDoc to) {
233         this.to = to;
234     }
235
236     /**
237      * DOCUMENT ME!
238      *
239      * @param reply_to DOCUMENT ME!
240      */

241     public void Reply_To(String JavaDoc reply_to) {
242         this.reply_to = reply_to;
243     }
244
245     /**
246      * DOCUMENT ME!
247      *
248      * @param cc DOCUMENT ME!
249      */

250     public void Cc(String JavaDoc cc) {
251         if (cc == null) {
252             ccs = new String JavaDoc[0];
253
254             return;
255         }
256
257         this.cc = cc;
258
259         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(cc, ",");
260         ccs = new String JavaDoc[st.countTokens()];
261
262         for (int i = 0; i < ccs.length; i++) {
263             ccs[i] = st.nextToken();
264         }
265     }
266
267     /**
268      * DOCUMENT ME!
269      *
270      * @param bcc DOCUMENT ME!
271      */

272     public void Bcc(String JavaDoc bcc) {
273         if (bcc == null) {
274             bccs = new String JavaDoc[0];
275
276             return;
277         }
278
279         this.bcc = bcc;
280
281         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(bcc, ",");
282         bccs = new String JavaDoc[st.countTokens()];
283
284         for (int i = 0; i < bccs.length; i++) {
285             bccs[i] = st.nextToken();
286         }
287     }
288
289     /**
290      * DOCUMENT ME!
291      *
292      * @param subject DOCUMENT ME!
293      */

294     public void Subject(String JavaDoc subject) {
295         this.subject = subject;
296     }
297
298     /**
299      * DOCUMENT ME!
300      *
301      * @param filename DOCUMENT ME!
302      *
303      * @return DOCUMENT ME!
304      */

305     public int numberOfLines(String JavaDoc filename) {
306         String JavaDoc string = "";
307         int nlines = 0;
308
309         try {
310             BufferedReader JavaDoc reader = new BufferedReader JavaDoc(new FileReader JavaDoc(filename));
311
312             while (in.available() != 0) {
313                 string = reader.readLine();
314                 nlines++;
315             }
316
317             in.close();
318         } catch (Exception JavaDoc e) {
319             System.out.println(e);
320         }
321
322         return nlines;
323     }
324
325     /**
326      * DOCUMENT ME!
327      *
328      * @param filename DOCUMENT ME!
329      *
330      * @return DOCUMENT ME!
331      */

332     public String JavaDoc[] loadLines(String JavaDoc filename) {
333         String JavaDoc[] string = new String JavaDoc[numberOfLines(filename)];
334         int nlines = 0;
335
336         try {
337             BufferedReader JavaDoc reader = new BufferedReader JavaDoc(new FileReader JavaDoc(filename));
338
339             while (in.available() != 0) {
340                 string[nlines] = reader.readLine();
341                 nlines++;
342             }
343
344             in.close();
345         } catch (Exception JavaDoc e) {
346             System.out.println(e);
347         }
348
349         return string;
350     }
351 }
352
Popular Tags