[Schmitzm-commits] r2413 - trunk/schmitzm-core/src/main/java/de/schmitzm/net/mail

scm-commit at wald.intevation.org scm-commit at wald.intevation.org
Wed Apr 8 23:05:09 CEST 2015


Author: mojays
Date: 2015-04-08 23:05:08 +0200 (Wed, 08 Apr 2015)
New Revision: 2413

Modified:
   trunk/schmitzm-core/src/main/java/de/schmitzm/net/mail/MailUtil.java
Log:
MailUtil: sendDesktopMail(.) extended to pipe exceptions in calling method


Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/net/mail/MailUtil.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/net/mail/MailUtil.java	2015-03-04 09:34:35 UTC (rev 2412)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/net/mail/MailUtil.java	2015-04-08 21:05:08 UTC (rev 2413)
@@ -290,6 +290,7 @@
 
     /**
 	 * Opens the local mail client with a new mail.
+	 * Exceptions are automatically handled by {@link ExceptionDialog}.
 	 * 
 	 * @param mailDestAddr
 	 *            destination address(es); can be {@code null}
@@ -304,54 +305,83 @@
 	 */
 	public static void sendDesktopMail(String mailDestAddr, String mailCcAddr, String mailBccAddr, String mailSubject,
 			String mailBody) {
-	  
-		if (!Desktop.isDesktopSupported()
-				|| !Desktop.getDesktop().isSupported(Desktop.Action.MAIL)) {
+	  try {
+        sendDesktopMail(mailDestAddr, mailCcAddr, mailBccAddr, mailSubject, mailBody, true);
+      } catch (Exception err) {
+        // Should not occur, because exceptions are handled
+        // by sendDesktopMail(.., true) call
+        throw new RuntimeException(err);
+      }
+	}
 
-			if (!GraphicsEnvironment.isHeadless()) {
-				JOptionPane
-						.showMessageDialog(null, RESOURCE
-								.getString("MailUtil.DesktopMail.NotSupported"));
-			} else {
-				log.info("java.awt.Desktop noch available on this system to send a mail.");
-			}
-			return;
-		}
+    /**
+     * Opens the local mail client with a new mail.
+     * 
+     * @param mailDestAddr
+     *            destination address(es); can be {@code null}
+     * @param mailCcAddr
+     *            destination address(es) taken as CC; can be {@code null}
+     * @param mailBccAddr
+     *            destination address(es) taken as BCC; can be {@code null}
+     * @param mailSubject
+     *            mail subject; can be {@code null}
+     * @param mailBody
+     *            mail body content; can be {@code null}
+     * @param handleExceptions
+     *            indicates whether exceptions are handled automatically by {@link ExceptionDialog}
+     *            or thrown to handle by calling mathod     
+     *        
+     */
+    public static boolean sendDesktopMail(String mailDestAddr, String mailCcAddr, String mailBccAddr, String mailSubject,
+            String mailBody, boolean handleExceptions) throws Exception {
+      
+        if (!Desktop.isDesktopSupported()
+                || !Desktop.getDesktop().isSupported(Desktop.Action.MAIL)) {
+            if (!GraphicsEnvironment.isHeadless()) {
+              if ( handleExceptions )
+                JOptionPane.showMessageDialog(null, RESOURCE.getString("MailUtil.DesktopMail.NotSupported"));
+            } else {
+                log.info("java.awt.Desktop noch available on this system to send a mail.");
+            }
+            if ( !handleExceptions )
+              throw new Exception(RESOURCE.getString("MailUtil.DesktopMail.NotSupported"));
+            return false;
+        }
 
-		try {
-		    if ( mailBody == null )
-  		      mailBody = "";
-			// Mail body can not be infinitely big
-			if (mailBody.length() > MAX_DESKTOP_MAIL_BODY_SIZE)
-				mailBody = mailBody.substring(0, MAX_DESKTOP_MAIL_BODY_SIZE)
-						+ "...\n[...]";
+        try {
+            if ( mailBody == null )
+              mailBody = "";
+            // Mail body can not be infinitely big
+            if (mailBody.length() > MAX_DESKTOP_MAIL_BODY_SIZE)
+                mailBody = mailBody.substring(0, MAX_DESKTOP_MAIL_BODY_SIZE)
+                        + "...\n[...]";
 
 
 //Outlook macht Probleme, wenn der SUBJECT-Parameter leer ist; deshalb Parameter
 //nur setzen, wenn Wert nicht leer!
-//			StringBuffer uriMailStr = new StringBuffer();
-//			uriMailStr.append(mailDestAddr != null ? mailDestAddr : "");
-//			uriMailStr.append("?SUBJECT=").append(
-//					mailSubject != null ? mailSubject : "");
-//			if ( mailCcAddr != null && !mailCcAddr.trim().isEmpty() )
+//          StringBuffer uriMailStr = new StringBuffer();
+//          uriMailStr.append(mailDestAddr != null ? mailDestAddr : "");
+//          uriMailStr.append("?SUBJECT=").append(
+//                  mailSubject != null ? mailSubject : "");
+//          if ( mailCcAddr != null && !mailCcAddr.trim().isEmpty() )
 //              uriMailStr.append("&CC=").append(mailCcAddr);
 //            if ( mailBccAddr != null && !mailBccAddr.trim().isEmpty() )
 //              uriMailStr.append("&BCC=").append(mailBccAddr);
-//			uriMailStr.append("&BODY=")
-//					.append(mailBody != null ? mailBody : "");
-			String uriMailStr = "";
-			if ( !StringUtils.isBlank(mailSubject) )
-			  uriMailStr += "&SUBJECT=" + mailSubject;
-			if ( !StringUtils.isBlank(mailCcAddr) )
-			  uriMailStr += "&CC=" + mailCcAddr;
+//          uriMailStr.append("&BODY=")
+//                  .append(mailBody != null ? mailBody : "");
+            String uriMailStr = "";
+            if ( !StringUtils.isBlank(mailSubject) )
+              uriMailStr += "&SUBJECT=" + mailSubject;
+            if ( !StringUtils.isBlank(mailCcAddr) )
+              uriMailStr += "&CC=" + mailCcAddr;
             if ( !StringUtils.isBlank(mailBccAddr) )
               uriMailStr += "&BCC=" + mailBccAddr;
             if ( !StringUtils.isBlank(mailBody) )
               uriMailStr += "&BODY=" + mailBody;
-			// replace leading & with ?
+            // replace leading & with ?
             if ( uriMailStr.startsWith("&") )
               uriMailStr = "?" + uriMailStr.substring(1);
-			// add TO addresses at the beginning
+            // add TO addresses at the beginning
             if ( !StringUtils.isBlank(mailDestAddr) )
               uriMailStr = mailDestAddr + uriMailStr;
 
@@ -367,15 +397,20 @@
             }
             
             
-			URI uriMailTo = new URI("mailto", uriMailStr, null);
-			Desktop.getDesktop().mail(uriMailTo);
-			//System.out.println(uriMailStr);
-		} catch (Exception err) {
-			ExceptionDialog.show(null, err,
-					RESOURCE.getString("MailUtil.DesktopMail.CreationError"),
-					err.getLocalizedMessage() );
-		}
-	}
+            URI uriMailTo = new URI("mailto", uriMailStr, null);
+            Desktop.getDesktop().mail(uriMailTo);
+            //System.out.println(uriMailStr);
+            return true;
+        } catch (Exception err) {
+          if ( handleExceptions )
+            ExceptionDialog.show(null, err,
+                    RESOURCE.getString("MailUtil.DesktopMail.CreationError"),
+                    err.getLocalizedMessage() );
+          else
+            throw err;
+          return false;
+        }
+    }
 
     /**
 	 * Opens the local mail client with a new mail for an exception.



More information about the Schmitzm-commits mailing list