Source Code Cross Referenced for EmailServices.java in  » ERP-CRM-Financial » SourceTap-CRM » org » ofbiz » content » 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 » ERP CRM Financial » SourceTap CRM » org.ofbiz.content.email 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * $Id: EmailServices.java,v 1.2 2004/02/06 01:11:00 jonesde Exp $
003:         *
004:         * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
005:         *
006:         * Permission is hereby granted, free of charge, to any person obtaining a
007:         * copy of this software and associated documentation files (the "Software"),
008:         * to deal in the Software without restriction, including without limitation
009:         * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010:         * and/or sell copies of the Software, and to permit persons to whom the
011:         * Software is furnished to do so, subject to the following conditions:
012:         *
013:         * The above copyright notice and this permission notice shall be included
014:         * in all copies or substantial portions of the Software.
015:         *
016:         * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017:         * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018:         * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019:         * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020:         * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021:         * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022:         * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023:         *
024:         */
025:        package org.ofbiz.content.email;
026:
027:        import java.net.MalformedURLException;
028:        import java.net.URL;
029:        import java.util.Map;
030:        import java.util.Properties;
031:
032:        import javax.mail.Message;
033:        import javax.mail.Session;
034:        import javax.mail.Transport;
035:        import javax.mail.internet.InternetAddress;
036:        import javax.mail.internet.MimeMessage;
037:
038:        import org.ofbiz.base.util.Debug;
039:        import org.ofbiz.base.util.HttpClient;
040:        import org.ofbiz.base.util.HttpClientException;
041:        import org.ofbiz.base.util.UtilProperties;
042:        import org.ofbiz.base.util.UtilValidate;
043:        import org.ofbiz.service.DispatchContext;
044:        import org.ofbiz.service.ServiceUtil;
045:
046:        /**
047:         * Email Services
048:         *
049:         * @author     <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
050:         * @version    $Revision: 1.2 $
051:         * @since      2.0
052:         */
053:        public class EmailServices {
054:
055:            public final static String module = EmailServices.class.getName();
056:
057:            /**
058:             * JavaMail Service that gets body content from a URL
059:             *@param ctx The DispatchContext that this service is operating in
060:             *@param context Map containing the input parameters
061:             *@return Map with the result of the service, the output parameters
062:             */
063:            public static Map sendMailFromUrl(DispatchContext ctx, Map context) {
064:                // pretty simple, get the content and then call the sendMail method below
065:                String bodyUrl = (String) context.remove("bodyUrl");
066:                Map bodyUrlParameters = (Map) context
067:                        .remove("bodyUrlParameters");
068:
069:                URL url = null;
070:
071:                try {
072:                    url = new URL(bodyUrl);
073:                } catch (MalformedURLException e) {
074:                    Debug.logWarning(e, module);
075:                    return ServiceUtil.returnError("Malformed URL: " + bodyUrl
076:                            + "; error was: " + e.toString());
077:                }
078:
079:                HttpClient httpClient = new HttpClient(url, bodyUrlParameters);
080:                String body = null;
081:
082:                try {
083:                    body = httpClient.post();
084:                } catch (HttpClientException e) {
085:                    Debug.logWarning(e, module);
086:                    return ServiceUtil.returnError("Error getting content: "
087:                            + e.toString());
088:                }
089:
090:                context.put("body", body);
091:                Map result = sendMail(ctx, context);
092:
093:                result.put("body", body);
094:                return result;
095:            }
096:
097:            /**
098:             * Basic JavaMail Service
099:             *@param ctx The DispatchContext that this service is operating in
100:             *@param context Map containing the input parameters
101:             *@return Map with the result of the service, the output parameters
102:             */
103:            public static Map sendMail(DispatchContext ctx, Map context) {
104:                // first check to see if sending mail is enabled
105:                String mailEnabled = UtilProperties
106:                        .getPropertyValue("general.properties",
107:                                "mail.notifications.enabled", "N");
108:                if (!"Y".equalsIgnoreCase(mailEnabled)) {
109:                    // no error; just return as if we already processed
110:                    Debug
111:                            .logImportant(
112:                                    "Mail notifications disabled in general.properties",
113:                                    module);
114:                    return ServiceUtil.returnSuccess();
115:                }
116:                String sendTo = (String) context.get("sendTo");
117:                String sendCc = (String) context.get("sendCc");
118:                String sendBcc = (String) context.get("sendBcc");
119:                String subject = (String) context.get("subject");
120:
121:                // check to see if we should redirect all mail for testing
122:                String redirectAddress = UtilProperties.getPropertyValue(
123:                        "general.properties", "mail.notifications.redirectTo");
124:                if (UtilValidate.isNotEmpty(redirectAddress)) {
125:                    String originalRecipients = " [To: " + sendTo + ", Cc: "
126:                            + sendCc + ", Bcc: " + sendBcc + "]";
127:                    subject = subject + originalRecipients;
128:                    sendTo = redirectAddress;
129:                    sendCc = null;
130:                    sendBcc = null;
131:                }
132:
133:                String sendFrom = (String) context.get("sendFrom");
134:                String body = (String) context.get("body");
135:                String sendType = (String) context.get("sendType");
136:                String sendVia = (String) context.get("sendVia");
137:                String authUser = (String) context.get("authUser");
138:                String authPass = (String) context.get("authPass");
139:                String contentType = (String) context.get("contentType");
140:                boolean useSmtpAuth = false;
141:
142:                // define some default       
143:                if (sendType == null || sendType.equals("mail.smtp.host")) {
144:                    sendType = "mail.smtp.host";
145:                    if (sendVia == null || sendVia.length() == 0) {
146:                        sendVia = UtilProperties.getPropertyValue(
147:                                "general.properties", "mail.smtp.relay.host",
148:                                "localhost");
149:                    }
150:                    if (authUser == null || authUser.length() == 0) {
151:                        authUser = UtilProperties.getPropertyValue(
152:                                "general.properties", "mail.smtp.auth.user");
153:                    }
154:                    if (authPass == null || authPass.length() == 0) {
155:                        authPass = UtilProperties
156:                                .getPropertyValue("general.properties",
157:                                        "mail.smtp.auth.password");
158:                    }
159:                    if (authUser != null && authUser.length() > 0) {
160:                        useSmtpAuth = true;
161:                    }
162:                } else if (sendVia == null) {
163:                    return ServiceUtil
164:                            .returnError("Parameter sendVia is required when sendType is not mail.smtp.host");
165:                }
166:
167:                if (contentType == null) {
168:                    contentType = "text/html";
169:                }
170:
171:                try {
172:                    Properties props = System.getProperties();
173:                    props.put(sendType, sendVia);
174:                    if (useSmtpAuth) {
175:                        props.put("mail.smtp.auth", "true");
176:                    }
177:
178:                    Session session = Session.getInstance(props);
179:
180:                    MimeMessage mail = new MimeMessage(session);
181:                    mail.setFrom(new InternetAddress(sendFrom));
182:                    mail.setSubject(subject);
183:                    mail.addRecipients(Message.RecipientType.TO, sendTo);
184:
185:                    if (UtilValidate.isNotEmpty(sendCc)) {
186:                        mail.addRecipients(Message.RecipientType.CC, sendCc);
187:                    }
188:                    if (UtilValidate.isNotEmpty(sendBcc)) {
189:                        mail.addRecipients(Message.RecipientType.BCC, sendBcc);
190:                    }
191:
192:                    mail.setContent(body, contentType);
193:                    mail.saveChanges();
194:
195:                    Transport trans = session.getTransport("smtp");
196:                    if (!useSmtpAuth) {
197:                        trans.connect();
198:                    } else {
199:                        trans.connect(sendVia, authUser, authPass);
200:                    }
201:                    trans.sendMessage(mail, mail.getAllRecipients());
202:                    trans.close();
203:                } catch (Exception e) {
204:                    Debug.logError(e, "Cannot send mail message", module);
205:                    return ServiceUtil
206:                            .returnError("Cannot send mail; see logs");
207:                }
208:                return ServiceUtil.returnSuccess();
209:            }
210:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.