[cvs] expresso commit by mtraum: allow a MimeBodyPart's to be attached

JCorporate Ltd jcorp at jcorporate.com
Wed Feb 2 20:31:16 UTC 2005


Log Message:
-----------
allow a MimeBodyPart's to be attached directly to an email (allowing, for example, embedded images in html emails)

Modified Files:
--------------
    expresso/expresso-web/WEB-INF/src/com/jcorporate/expresso/core/security:
        User.java
    expresso/expresso-web/WEB-INF/src/com/jcorporate/expresso/core/misc:
        EMailSender.java

Revision Data
-------------
Index: User.java
===================================================================
RCS file: /home/javacorp/.cvs/expresso/expresso/expresso-web/WEB-INF/src/com/jcorporate/expresso/core/security/User.java,v
retrieving revision 1.51
retrieving revision 1.52
diff -Lexpresso-web/WEB-INF/src/com/jcorporate/expresso/core/security/User.java -Lexpresso-web/WEB-INF/src/com/jcorporate/expresso/core/security/User.java -u -r1.51 -r1.52
--- expresso-web/WEB-INF/src/com/jcorporate/expresso/core/security/User.java
+++ expresso-web/WEB-INF/src/com/jcorporate/expresso/core/security/User.java
@@ -99,6 +99,8 @@
 import java.util.List;
 import java.util.Vector;
 
+import javax.mail.internet.MimeBodyPart;
+
 
 /**
  * This class provides a front-end for maintaining Expresso
@@ -731,7 +733,7 @@
      */
     public void notify(String subject, String message, boolean htmlFormat)
             throws DBException {
-        notify(subject, message, htmlFormat, null);
+        notify(subject, message, htmlFormat, (ByteArrayDataSource[]) null);
     }
 
     /**
@@ -767,6 +769,50 @@
                 // Register virtual raw data attachments
                 for (int k = 0; k < attachments.length; ++k) {
                     ems.addDataSourceAttachment(attachments[k]);
+                }
+            }
+
+            ems.send(sendToUser, subject, message);
+        } catch (Exception e) {
+            throw new DBException(thisClass + "notify()" +
+                    ":Uncaught exception sending e-mail", e);
+        }
+    } /* notify(String, String) */
+    
+    /**
+     * Notify the user with the optional parameter of using html
+     * format with MimeBodyPart attachments. MimeBodyPart attachments are commonly
+     * used to set the Content-ID of a file attachment so that it can be used
+     * as an embedded image in an html email.
+     *
+     * @param subject     the message subject
+     * @param message     the message, possibly in html format
+     * @param htmlFormat  true if you want the message to be html formatted.
+     * @param mimeAttachments a primitive array of MimeBodyPart's to add to the message.
+     * @throws DBException  upon error from database
+     */
+    public void notify(String subject, String message, boolean htmlFormat,
+                       MimeBodyPart mimeAttachments[])
+            throws DBException {
+
+        if (log.isDebugEnabled()) {
+            log.debug("Notifying user " + getUserInfo().getLoginName()
+                    + " of " + subject);
+        }
+
+        String sendToUser = getUserInfo().getEmail();
+
+        try {
+            EMailSender ems = new EMailSender();
+            ems.setDBName(this.getDataContext());
+            if (htmlFormat) {
+                ems.setEmailHtmlFormat();
+            }
+
+            if (mimeAttachments != null) {
+                // Register MimeBodyPart attachments
+                for (int k = 0; k < mimeAttachments.length; ++k) {
+                    ems.addMimeAttachments(mimeAttachments[k]);
                 }
             }
 
Index: EMailSender.java
===================================================================
RCS file: /home/javacorp/.cvs/expresso/expresso/expresso-web/WEB-INF/src/com/jcorporate/expresso/core/misc/EMailSender.java,v
retrieving revision 1.22
retrieving revision 1.23
diff -Lexpresso-web/WEB-INF/src/com/jcorporate/expresso/core/misc/EMailSender.java -Lexpresso-web/WEB-INF/src/com/jcorporate/expresso/core/misc/EMailSender.java -u -r1.22 -r1.23
--- expresso-web/WEB-INF/src/com/jcorporate/expresso/core/misc/EMailSender.java
+++ expresso-web/WEB-INF/src/com/jcorporate/expresso/core/misc/EMailSender.java
@@ -124,6 +124,8 @@
     protected String dbName = null;
     protected List attachments = null;
     protected List dataSourceAttachments = null;
+    protected List mimeAttachments = null;
+
     /** HTML custom Headers list (Francesco Galli - f.galli at inera.it) */
     protected Hashtable headers = null;
     protected boolean htmlFormat = false; // add boolean property for html format emails
@@ -238,6 +240,50 @@
             dataSourceAttachments.add(dataSource);
         }
     }
+    
+    /**
+     * This method adds a MimeBodyPart to the email to be sent.  Multiple calls can
+     * be made to this method to add multiple MimeBodyPart's.
+     * 
+     * Commonly used to set the Content-ID of a file attachment so that it can be used
+     * as an embedded image in an html email.
+     *
+     * @param mime MimeBodyPart object to add to the message
+     *
+     */
+    public void addMimeAttachments( MimeBodyPart mime ) {
+
+        // Lazy instantiation of the list of mimeAttachments, if needed
+        if (mimeAttachments == null) {
+            mimeAttachments = new ArrayList(4);
+        }
+
+        // Add the mime attachment to the list of mime attachments
+        mimeAttachments.add(mime);
+    }
+
+    /**
+     * This method accepts a list of MimeBodyPart's
+     * to be sent in the email.
+     * 
+     * Commonly used to set the Content-ID of a file attachment so that it can be used
+     * as an embedded image in an html email.
+     *
+     * @param mimeList a list of MimeBodyPart's
+     */
+    public void addMimeAttachments(List mimeList) {
+
+        // Lazy instantiation of the list of mimeAttachments, if needed
+        if (mimeAttachments == null) {
+            mimeAttachments = new ArrayList(4);
+        }
+
+        // Loop through each mime body part, adding it to the list of files to send
+        for (Iterator itor = mimeList.iterator(); itor.hasNext();) {
+            MimeBodyPart mime = (MimeBodyPart ) itor.next();
+            mimeAttachments.add( mime );
+        }
+    }
 
     /**
      * This method returns the name of the DB context, either preset by the caller,
@@ -556,6 +602,15 @@
                     mbp3.setDataHandler(new DataHandler(bads));
                     mbp3.setFileName(bads.getName());
                     mp.addBodyPart(mbp3);
+                }
+            }
+            
+            if ( mimeAttachments != null ) {
+                // Take care of the mime attachments.
+                for ( Iterator itor = mimeAttachments.iterator(); itor.hasNext(); ) {
+                    MimeBodyPart mime = (MimeBodyPart)itor.next();
+
+                    mp.addBodyPart(mime);
                 }
             }
 


More information about the cvs mailing list