Source Code Cross Referenced for SMTPHandler.java in  » Web-Server » Brazil » sunlabs » brazil » handler » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Web Server » Brazil » sunlabs.brazil.handler 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.