001: /*
002: * Copyright (c) 1999-2001 Lutris Technologies, Inc. All Rights
003: * Reserved.
004: *
005: * This source code file is distributed by Lutris Technologies, Inc. for
006: * use only by licensed users of product(s) that include this source
007: * file. Use of this source file or the software that uses it is covered
008: * by the terms and conditions of the Lutris Enhydra Development License
009: * Agreement included with this product.
010: *
011: * This Software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF
012: * ANY KIND, either express or implied. See the License for the specific terms
013: * governing rights and limitations under the License.
014: *
015: * Contributor(s):
016: *
017: * $Id: Email.java,v 1.1 2006-09-11 12:27:25 sinisa Exp $
018: */
019:
020: package com.lutris.airsent.business.email;
021:
022: import java.io.*;
023: import java.net.Socket;
024: import java.util.Vector;
025:
026: /**
027: * Utility class used to send email.
028: *
029: * @author Jason Abbott <jason.abbott@lutris.com>
030: * @author Joseph Shoop
031: */
032: public class Email {
033: private OutputStreamWriter out = null;
034: private BufferedReader in = null;
035: private Socket mailserver = null;
036: private InputStreamReader isr = null;
037: private String mailServerURL = "localhost";
038:
039: /**
040: * Constructor with mail server name.
041: *
042: *
043: * @param mailserverURL
044: *
045: *
046: */
047: public Email(String mailServer) throws IOException {
048: this .mailServerURL = mailServer;
049: }
050:
051: /**
052: * Writes the email to output stream.
053: *
054: *
055: * @param text
056: *
057: * @return
058: *
059: * @throws IOException
060: *
061: *
062: */
063: private String send(String text) throws IOException {
064:
065: out.write(text, 0, text.length());
066: out.flush();
067:
068: // return in.readLine();
069: String retVal = in.readLine();
070: String dump = "x";
071: while ((dump = in.readLine()) == null) {
072: }
073: if (retVal.startsWith("5")) {
074:
075: throw new IOException(mailServerURL + " SMTP Error: "
076: + retVal);
077: }
078:
079: return retVal;
080: }
081:
082: /**
083: * Opens a socket connection to the mail server.
084: *
085: *
086: * @throws IOException
087: *
088: *
089: */
090: private void open() throws IOException {
091: mailserver = new Socket(mailServerURL, 25);
092: isr = new InputStreamReader(mailserver.getInputStream());
093: out = new OutputStreamWriter(mailserver.getOutputStream());
094: in = new BufferedReader(isr);
095: }
096:
097: /**
098: * Closes connection to the mail server.
099: *
100: *
101: * @throws IOException
102: *
103: *
104: */
105: private void close() throws IOException {
106: out.close();
107: in.close();
108: }
109:
110: /**
111: * Sends mail.
112: *
113: *
114: * @param domain
115: * @param fromAddress
116: * @param toAddress
117: * @param subject
118: * @param type
119: * @param text
120: *
121: * @throws Exception
122: *
123: *
124: */
125: public void sendMail(String domain, String fromAddress,
126: String toAddress, String subject, String type, String text)
127: throws Exception {
128: try {
129: open();
130: } catch (Exception e) {
131: throw new Exception("Error opening socket" + e);
132: }
133:
134: // Lets check all of our parameters...
135: // 1.fromAddress
136: if (fromAddress == null) {
137: throw new Exception(
138: "We need a from-address that is not null to send email.");
139: }
140:
141: fromAddress = fromAddress.trim();
142:
143: if (fromAddress.length() == 0) {
144: throw new Exception(
145: "We need a from-address that is not empty to send email.");
146: }
147:
148: if (fromAddress.indexOf("@") == -1) {
149: throw new Exception(
150: "We need a legal from-address to send email, we do not consider \""
151: + fromAddress + "\" to be legal.");
152:
153: // 2.To address
154: }
155:
156: if (toAddress == null) {
157: throw new Exception(
158: "We need to-addresses that are not null to send email.");
159: }
160:
161: toAddress = toAddress.trim();
162:
163: if (toAddress.length() == 0) {
164: throw new Exception(
165: "We need to-addresses that are not empty to send email.");
166: }
167:
168: if (toAddress.indexOf("@") == -1) {
169: throw new Exception(
170: "We need legal to-addresses to send email, we do not consider \""
171: + toAddress + "\" to be legal.");
172:
173: // 3.subject
174: }
175:
176: if (subject == null) {
177: throw new Exception(
178: "We need a non-null subject to send email");
179: }
180:
181: subject = subject.trim();
182:
183: if (subject.length() == 0) {
184: throw new Exception(
185: "We need a non-empty subject to send email");
186: }
187:
188: if (type == null) {
189: type = "test/plain";
190: }
191:
192: // 4. Text body
193: if (text == null) {
194: throw new Exception(
195: "We need a non-null text body to send email");
196: }
197:
198: text = text.trim();
199:
200: if (text.length() == 0) {
201: throw new Exception(
202: "We need a non-empty text body to send email");
203: }
204:
205: try {
206: send("EHLO " + domain + "\r\n");
207: } catch (IOException e) {
208: throw new Exception("SMTP Mail Initialization failure");
209: }
210:
211: try {
212: send("MAIL FROM: " + fromAddress + "\r\n");
213: send("RCPT TO: " + toAddress + "\r\n");
214: } catch (IOException e) {
215: throw new Exception("SMTP Mail addressing failure");
216: }
217:
218: try {
219:
220: //Multipart email
221: String boundary = "AIRSENTBOUNDARY-1";
222: StringBuffer tmp = new StringBuffer();
223: tmp.append("DATA\r\n");
224: tmp.append("MIME-Version: 1.0");
225: tmp.append("\r\n");
226: tmp.append("From: ");
227: tmp.append(fromAddress);
228: tmp.append("\r\n");
229: tmp.append("To: ");
230: tmp.append(toAddress);
231: tmp.append("\r\n");
232: tmp.append("Subject: ");
233: tmp.append(subject);
234: tmp.append("\r\n");
235: tmp
236: .append("Content-Type: multipart/alternative; boundary=\""
237: + boundary + "\"");
238: tmp.append("\r\n");
239: tmp.append("--" + boundary);
240: tmp.append("\r\n");
241: tmp.append("Content-Type: text/plain; charset=us-ascii;");
242: tmp.append("\r\n");
243: tmp.append("\r\n");
244: tmp.append("AirSent delivery notification:");
245: tmp.append("\r\n");
246: tmp.append(text);
247: tmp.append("\r\n");
248: tmp.append("\r\n");
249: tmp.append("--" + boundary);
250: tmp.append("\r\n");
251: tmp.append("Content-Type: text/html; charset=us-ascii;");
252: tmp.append("\r\n");
253: tmp.append("\r\n");
254: tmp.append(text);
255: tmp.append("\r\n");
256: tmp.append("\r\n");
257: tmp.append("--" + boundary + "--");
258: tmp.append("\r\n" + ".\r\n");
259: //End multipart email
260:
261: send(tmp.toString());
262:
263: } catch (Exception e) {
264: throw new Exception("SMTP Mail message body failure");
265: }
266:
267: try {
268: send("QUIT\r\n");
269: close();
270: } catch (IOException e) {
271: throw new Exception("SMTP Mail signoff failure");
272: }
273: }
274:
275: }
|