svn commit: r765476 - /ofbiz/trunk/framework/common/src/org/ofbiz/common/email/EmailServices.java

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

svn commit: r765476 - /ofbiz/trunk/framework/common/src/org/ofbiz/common/email/EmailServices.java

jaz-3
Author: jaz
Date: Thu Apr 16 05:55:51 2009
New Revision: 765476

URL: http://svn.apache.org/viewvc?rev=765476&view=rev
Log:
minor changes to better understand why an email may fail to be sent; specific excpetions are now caught

Modified:
    ofbiz/trunk/framework/common/src/org/ofbiz/common/email/EmailServices.java

Modified: ofbiz/trunk/framework/common/src/org/ofbiz/common/email/EmailServices.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/common/src/org/ofbiz/common/email/EmailServices.java?rev=765476&r1=765475&r2=765476&view=diff
==============================================================================
--- ofbiz/trunk/framework/common/src/org/ofbiz/common/email/EmailServices.java (original)
+++ ofbiz/trunk/framework/common/src/org/ofbiz/common/email/EmailServices.java Thu Apr 16 05:55:51 2009
@@ -42,6 +42,8 @@
 import javax.mail.Message;
 import javax.mail.Session;
 import javax.mail.Transport;
+import javax.mail.MessagingException;
+import javax.mail.SendFailedException;
 import javax.mail.internet.InternetAddress;
 import javax.mail.internet.MimeBodyPart;
 import javax.mail.internet.MimeMessage;
@@ -182,6 +184,8 @@
         }
         results.put("contentType", contentType);
 
+        Session session;
+        MimeMessage mail;
         try {
             Properties props = System.getProperties();
             props.put(sendType, sendVia);
@@ -202,11 +206,11 @@
                 props.put("mail.smtp.auth", "true");
             }
 
-            Session session = Session.getInstance(props);
+            session = Session.getInstance(props);
             boolean debug = UtilProperties.propertyValueEqualsIgnoreCase("general.properties", "mail.debug.on", "Y");
             session.setDebug(debug);
 
-            MimeMessage mail = new MimeMessage(session);
+            mail = new MimeMessage(session);
             if (messageId != null) {
                 mail.setHeader("In-Reply-To", messageId);
                 mail.setHeader("References", messageId);
@@ -263,7 +267,19 @@
                 }
                 mail.saveChanges();
             }
+        } catch (MessagingException e) {
+            String errMsg = "MessagingException when creating message to [" + sendTo + "] from [" + sendFrom + "] cc [" + sendCc + "] bcc [" + sendBcc + "] subject [" + subject + "]";
+            Debug.logError(e, errMsg, module);
+            Debug.logError("Email message that could not be created to [" + sendTo + "] had context: " + context, module);
+            return ServiceUtil.returnError(errMsg);
+        } catch (IOException e) {
+            String errMsg = "IOExcepton when creating message to [" + sendTo + "] from [" + sendFrom + "] cc [" + sendCc + "] bcc [" + sendBcc + "] subject [" + subject + "]";
+            Debug.logError(e, errMsg, module);
+            Debug.logError("Email message that could not be created to [" + sendTo + "] had context: " + context, module);
+            return ServiceUtil.returnError(errMsg);
+        }
 
+        try {
             Transport trans = session.getTransport("smtp");
             if (!useSmtpAuth) {
                 trans.connect();
@@ -273,10 +289,17 @@
             trans.sendMessage(mail, mail.getAllRecipients());
             results.put("messageId", mail.getMessageID());
             trans.close();
-        } catch (Exception e) {
-            String errMsg = "Cannot send email message to [" + sendTo + "] from [" + sendFrom + "] cc [" + sendCc + "] bcc [" + sendBcc + "] subject [" + subject + "]";
+        } catch (SendFailedException e) {
+            // message code prefix may be used by calling services to determine the cause of the failure
+            String errMsg = "[ADDRERR] Address error when sending 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);
+            Debug.logError("Email message that could not be sent to [" + sendTo + "] had context: " + context, module);
+            return ServiceUtil.returnError(errMsg);
+        } catch (MessagingException e) {
+            // message code prefix may be used by calling services to determine the cause of the failure
+            String errMsg = "[CON] Connection error when sending message to [" + sendTo + "] from [" + sendFrom + "] cc [" + sendCc + "] bcc [" + sendBcc + "] subject [" + subject + "]";
+            Debug.logError(e, errMsg, module);
+            Debug.logError("Email message that could not be sent to [" + sendTo + "] had context: " + context, module);
             return ServiceUtil.returnError(errMsg);
         }
         return results;
@@ -285,7 +308,7 @@
     /**
      * 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
+     *@param rcontext Map containing the input parameters
      *@return Map with the result of the service, the output parameters
      */
     public static Map<String, Object> sendMailFromUrl(DispatchContext ctx, Map<String, ? extends Object> rcontext) {
@@ -324,7 +347,7 @@
      * JavaMail Service that gets body content from a Screen Widget
      * defined in the product store record and if available as attachment also.
      *@param dctx The DispatchContext that this service is operating in
-     *@param serviceContext Map containing the input parameters
+     *@param rServiceContext Map containing the input parameters
      *@return Map with the result of the service, the output parameters
      */
     public static Map<String, Object> sendMailFromScreen(DispatchContext dctx, Map<String, ? extends Object> rServiceContext) {