📄 emailservices.java
字号:
/* * $Id: EmailServices.java 7150 2006-03-31 07:30:43Z byersa $ * * Copyright (c) 2001-2005 The Open For Business Project - www.ofbiz.org * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included * in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * THE USE OR OTHER DEALINGS IN THE SOFTWARE. * */package org.ofbiz.content.email;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.io.StringWriter;import java.io.Writer;import java.net.MalformedURLException;import java.net.URL;import java.util.Iterator;import java.util.List;import java.util.ArrayList;import java.util.Locale;import java.util.Map;import java.util.HashMap;import java.util.Properties;import java.sql.Timestamp;import java.util.Date;import java.util.Calendar;import javax.activation.DataHandler;import javax.activation.DataSource;import javax.mail.Message;import javax.mail.Address;import javax.mail.Session;import javax.mail.Transport;import javax.mail.internet.InternetAddress;import javax.mail.internet.MimeBodyPart;import javax.mail.internet.MimeMessage;import javax.mail.internet.MimeMultipart;import javax.xml.parsers.ParserConfigurationException;import javax.mail.MessagingException;import javax.mail.Multipart;import javax.mail.Part;import javolution.util.FastList;import javolution.util.FastMap;import org.apache.avalon.framework.logger.Log4JLogger;import org.apache.avalon.framework.logger.Logger;import org.apache.fop.apps.Driver;import org.apache.fop.apps.FOPException;import org.apache.fop.image.FopImageFactory;import org.apache.fop.messaging.MessageHandler;import org.apache.fop.tools.DocumentInputSource;import org.ofbiz.base.util.Debug;import org.ofbiz.base.util.GeneralException;import org.ofbiz.base.util.HttpClient;import org.ofbiz.base.util.HttpClientException;import org.ofbiz.base.util.UtilMisc;import org.ofbiz.base.util.UtilProperties;import org.ofbiz.base.util.UtilValidate;import org.ofbiz.base.util.UtilXml;import org.ofbiz.base.util.UtilDateTime;import org.ofbiz.base.util.collections.MapStack;import org.ofbiz.base.util.string.FlexibleStringExpander;import org.ofbiz.entity.GenericDelegator;import org.ofbiz.entity.GenericValue;import org.ofbiz.service.DispatchContext;import org.ofbiz.service.GenericServiceException;import org.ofbiz.service.LocalDispatcher;import org.ofbiz.service.ServiceUtil;import org.ofbiz.service.mail.MimeMessageWrapper;import org.ofbiz.widget.html.HtmlScreenRenderer;import org.ofbiz.widget.screen.ScreenRenderer;import org.w3c.dom.Document;import org.xml.sax.SAXException;/** * Email Services * * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a> * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a> * @author <a href="mailto:h.bakker@antwebsystems.com">Hans Bakker</a> * @author <a href="mailto:byersa@automationgroups.com">Al Byers</a> * @since 2.0 */public class EmailServices { public final static String module = EmailServices.class.getName(); protected static final HtmlScreenRenderer htmlScreenRenderer = new HtmlScreenRenderer(); /** * Basic JavaMail Service *@param ctx The DispatchContext that this service is operating in *@param context Map containing the input parameters *@return Map with the result of the service, the output parameters */ public static Map sendMail(DispatchContext ctx, Map context) { Map results = ServiceUtil.returnSuccess(); String subject = (String) context.get("subject"); String partyId = (String) context.get("partyId"); String body = (String) context.get("body"); List bodyParts = (List) context.get("bodyParts"); GenericValue userLogin = (GenericValue) context.get("userLogin"); results.put("partyId", partyId); results.put("subject", subject); if (UtilValidate.isNotEmpty(body)) results.put("body", body); if (UtilValidate.isNotEmpty(bodyParts)) results.put("bodyParts", bodyParts); results.put("userLogin", userLogin); // first check to see if sending mail is enabled String mailEnabled = UtilProperties.getPropertyValue("general.properties", "mail.notifications.enabled", "N"); if (!"Y".equalsIgnoreCase(mailEnabled)) { // no error; just return as if we already processed Debug.logImportant("Mail notifications disabled in general.properties; here is the context with info that would have been sent: " + context, module); return results; } String sendTo = (String) context.get("sendTo"); String sendCc = (String) context.get("sendCc"); String sendBcc = (String) context.get("sendBcc"); // check to see if we should redirect all mail for testing String redirectAddress = UtilProperties.getPropertyValue("general.properties", "mail.notifications.redirectTo"); if (UtilValidate.isNotEmpty(redirectAddress)) { String originalRecipients = " [To: " + sendTo + ", Cc: " + sendCc + ", Bcc: " + sendBcc + "]"; subject = subject + originalRecipients; sendTo = redirectAddress; sendCc = null; sendBcc = null; } String sendFrom = (String) context.get("sendFrom"); String sendType = (String) context.get("sendType"); String sendVia = (String) context.get("sendVia"); String authUser = (String) context.get("authUser"); String authPass = (String) context.get("authPass"); String contentType = (String) context.get("contentType"); boolean useSmtpAuth = false; // define some default if (sendType == null || sendType.equals("mail.smtp.host")) { sendType = "mail.smtp.host"; if (sendVia == null || sendVia.length() == 0) { sendVia = UtilProperties.getPropertyValue("general.properties", "mail.smtp.relay.host", "localhost"); } if (authUser == null || authUser.length() == 0) { authUser = UtilProperties.getPropertyValue("general.properties", "mail.smtp.auth.user"); } if (authPass == null || authPass.length() == 0) { authPass = UtilProperties.getPropertyValue("general.properties", "mail.smtp.auth.password"); } if (authUser != null && authUser.length() > 0) { useSmtpAuth = true; } } else if (sendVia == null) { return ServiceUtil.returnError("Parameter sendVia is required when sendType is not mail.smtp.host"); } if (contentType == null) { contentType = "text/html"; } if (UtilValidate.isNotEmpty(bodyParts)) { contentType = "multipart/mixed"; } results.put("contentType", contentType); try { Properties props = System.getProperties(); props.put(sendType, sendVia); if (useSmtpAuth) { props.put("mail.smtp.auth", "true"); } Session session = Session.getInstance(props); MimeMessage mail = new MimeMessage(session); mail.setFrom(new InternetAddress(sendFrom)); mail.setSubject(subject); mail.addRecipients(Message.RecipientType.TO, sendTo); if (UtilValidate.isNotEmpty(sendCc)) { mail.addRecipients(Message.RecipientType.CC, sendCc); } if (UtilValidate.isNotEmpty(sendBcc)) { mail.addRecipients(Message.RecipientType.BCC, sendBcc); } if (UtilValidate.isNotEmpty(bodyParts)) { // check for multipart message (with attachments) // BodyParts contain a list of Maps items containing content(String) and type(String) of the attachement MimeMultipart mp = new MimeMultipart(); Debug.logInfo(bodyParts.size() + " multiparts found",module); Iterator bodyPartIter = bodyParts.iterator(); while (bodyPartIter.hasNext()) { Map bodyPart = (Map) bodyPartIter.next(); Object bodyPartContent = bodyPart.get("content"); MimeBodyPart mbp = new MimeBodyPart(); if (bodyPartContent instanceof String) { StringDataSource sdr = new StringDataSource((String) bodyPartContent, (String) bodyPart.get("type")); Debug.logInfo("part of type: " + bodyPart.get("type") + " and size: " + bodyPart.get("content").toString().length() , module); mbp.setDataHandler(new DataHandler(sdr)); } else if (bodyPartContent instanceof byte[]) { ByteArrayDataSource bads = new ByteArrayDataSource((byte[]) bodyPartContent, (String) bodyPart.get("type")); Debug.logInfo("part of type: " + bodyPart.get("type") + " and size: " + ((byte[]) bodyPartContent).length , module); mbp.setDataHandler(new DataHandler(bads)); } else { mbp.setDataHandler(new DataHandler(bodyPartContent, (String) bodyPart.get("type"))); } String fileName = (String) bodyPart.get("filename"); if (fileName != null) { mbp.setFileName(fileName); } mp.addBodyPart(mbp); } mail.setContent(mp); mail.saveChanges(); } else { // create the singelpart message mail.setContent(body, contentType); mail.saveChanges(); } Transport trans = session.getTransport("smtp"); if (!useSmtpAuth) { trans.connect(); } else { trans.connect(sendVia, authUser, authPass); } trans.sendMessage(mail, mail.getAllRecipients()); trans.close(); } catch (Exception e) { String errMsg = "Cannot send email message to [" + sendTo + "] from [" + sendFrom + "] cc [" + sendCc + "] bcc [" + sendBcc + "] subject [" + subject + "]"; Debug.logError(e, errMsg, module); Debug.logError(e, "Email message that could not be sent to [" + sendTo + "] had context: " + context, module); return ServiceUtil.returnError(errMsg); } return results; } /** * JavaMail Service that gets body content from a URL *@param ctx The DispatchContext that this service is operating in *@param context Map containing the input parameters *@return Map with the result of the service, the output parameters */ public static Map sendMailFromUrl(DispatchContext ctx, Map context) { // pretty simple, get the content and then call the sendMail method below String bodyUrl = (String) context.remove("bodyUrl"); Map bodyUrlParameters = (Map) context.remove("bodyUrlParameters"); URL url = null; try { url = new URL(bodyUrl); } catch (MalformedURLException e) { Debug.logWarning(e, module); return ServiceUtil.returnError("Malformed URL: " + bodyUrl + "; error was: " + e.toString()); } HttpClient httpClient = new HttpClient(url, bodyUrlParameters); String body = null; try {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -