Source Code Cross Referenced for Email.java in  » J2EE » Enhydra-Demos » com » lutris » airsent » business » email » 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 » J2EE » Enhydra Demos » com.lutris.airsent.business.email 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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