001: /*
002: * SMTPHandler.java
003: *
004: * Brazil project web application Framework,
005: * export version: 1.1
006: * Copyright (c) 2000-2001 Sun Microsystems, Inc.
007: *
008: * Sun Public License Notice
009: *
010: * The contents of this file are subject to the Sun Public License Version
011: * 1.0 (the "License"). You may not use this file except in compliance with
012: * the License. A copy of the License is included as the file "license.terms",
013: * and also available at http://www.sun.com/
014: *
015: * The Original Code is from:
016: * Brazil project web application Framework release 1.1.
017: * The Initial Developer of the Original Code is: suhler.
018: * Portions created by suhler are Copyright (C) Sun Microsystems, Inc.
019: * All Rights Reserved.
020: *
021: * Contributor(s): cstevens, suhler.
022: *
023: * Version: 1.6
024: * Created by suhler on 00/01/11
025: * Last modified by suhler on 01/01/14 14:51:10
026: */
027:
028: package sunlabs.brazil.handler;
029:
030: import java.io.BufferedInputStream;
031: import java.io.BufferedOutputStream;
032: import java.io.DataInputStream;
033: import java.io.DataOutputStream;
034: import java.io.IOException;
035: import java.net.Socket;
036: import java.util.Enumeration;
037: import java.util.Hashtable;
038: import java.util.StringTokenizer;
039: import sunlabs.brazil.server.Handler;
040: import sunlabs.brazil.server.Request;
041: import sunlabs.brazil.server.Server;
042:
043: /**
044: * Handler for Sending an email message via SMTP
045: * The following server properties are used:
046: * <dl class=props>
047: * <dt>prefix <dd> url prefix
048: * <dt>host <dd> The mail host (e.g. listening on the SMTP port).
049: * Dedaults to "localhost".
050: * </dl>
051: *
052: * @author Stephen Uhler
053: * @version 1.4, 99/11/30
054: */
055:
056: public class SMTPHandler implements Handler {
057: String mailHost;
058: String me;
059: String urlPrefix;
060:
061: public boolean init(Server server, String prefix) {
062: mailHost = server.props.getProperty(prefix + "host",
063: "localhost");
064: me = server.hostName;
065: urlPrefix = server.props.getProperty(prefix + "prefix", "/");
066:
067: /*
068: * Make sure we can communicate with the host
069: */
070:
071: try {
072: Socket sock = new Socket(mailHost, 25);
073: DataInputStream in = new DataInputStream(
074: new BufferedInputStream(sock.getInputStream()));
075: String line = in.readLine();
076: in.close();
077: sock.close();
078: server.log(Server.LOG_DIAGNOSTIC, prefix, line);
079: if (line.startsWith("220")) {
080: return true;
081: }
082: } catch (IOException e) {
083: server.log(Server.LOG_WARNING, prefix,
084: "Can't contanct SMTP server: " + e);
085: }
086: return false;
087: }
088:
089: /**
090: * Send an email message based on the query data
091: * <p>
092: * Parameters:
093: * <dl>
094: * <dt>to <dd> To address
095: * <dt>from <dd> From address
096: * <dt>message <dd> Message
097: * <dt>subject <dd> Subject
098: * </dl>
099: * Either the message or subject may be null, but not both.
100: */
101:
102: public boolean respond(Request request) throws IOException {
103: if (!request.url.startsWith(urlPrefix)) {
104: return false;
105: }
106: Hashtable data = request.getQueryData();
107: Hashtable headers = new Hashtable(1);
108: String to = (String) data.get("to");
109: String from = (String) data.get("from");
110: String subject = (String) data.get("subject");
111: String message = (String) data.get("message");
112: if (to == null || from == null
113: || (subject == null && message == null)) {
114: request.sendError(400, "Missing query parameters");
115: return true;
116: }
117: if (subject == null) {
118: subject = "(none)";
119: }
120: headers.put("subject", subject);
121: smtp(me, mailHost, from, to, message, headers);
122: request.props.put(mailHost, "sent"); // ??
123: return false;
124: }
125:
126: /**
127: * Send an email message via smtp - simple version.
128: * @param fromHost the hostname of the sender (may be null)
129: * @param smtpHost the SMTP host (whose smtp daemon to contact)
130: * @param from who the email is from
131: * @param to a space delimited list of recepients
132: * @param body The message body
133: * @param headers message headers (may be null)
134: * @thows IOException, if any errors occured (yuk)
135: *
136: * Either the headers Or body may be null, but not both.
137: */
138:
139: public static void smtp(String fromHost, String smtpHost,
140: String from, String to, String body, Hashtable headers)
141: throws IOException {
142: Socket sock = null;
143: DataInputStream in = null;
144: DataOutputStream out = null;
145: try {
146: sock = new Socket(smtpHost, 25);
147: in = new DataInputStream(new BufferedInputStream(sock
148: .getInputStream()));
149: out = new DataOutputStream(new BufferedOutputStream(sock
150: .getOutputStream()));
151:
152: /* Do the protocol - in line */
153:
154: send(in, out, null, "220");
155: if (fromHost != null) {
156: send(in, out, "HELO " + fromHost, "250");
157: }
158: send(in, out, "MAIL FROM:<" + from + ">", "250");
159: StringTokenizer st = new StringTokenizer(to);
160: while (st.hasMoreTokens()) {
161: send(in, out, "RCPT TO:<" + st.nextToken() + ">", "250");
162: }
163: send(in, out, "DATA", "354");
164: if (headers != null) {
165: Enumeration keys = headers.keys();
166: while (keys.hasMoreElements()) {
167: String key = (String) keys.nextElement();
168: out.writeBytes(key + ": " + headers.get(key)
169: + "\r\n");
170: }
171: }
172: out.writeBytes("\r\n");
173: if (body != null) {
174: out.writeBytes(body);
175: }
176: out.writeBytes("\r\n.\r\nQUIT\r\n");
177: out.flush();
178: } finally {
179: try {
180: in.close();
181: out.close();
182: sock.close();
183: } catch (Exception e) {
184: }
185: }
186: }
187:
188: /**
189: * Do a transaction with SMTP server
190: */
191:
192: private static void send(DataInputStream in, DataOutputStream out,
193: String msg, String expect) throws IOException {
194: if (msg != null) {
195: out.writeBytes(msg + "\r\n");
196: // System.out.println("-> " + msg);
197: out.flush();
198: }
199: String line = in.readLine();
200: // System.out.println("<- " + line);
201: if (!line.startsWith(expect)) {
202: // System.out.println("?? " + expect);
203: throw new IOException(line);
204: }
205: }
206: }
|