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: }
|