You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Scott Dietrich <sd...@emlab.com> on 2004/08/19 07:07:34 UTC

[mail] html email with attachments

Hi,

I've been using commons-email (and turbine) for some time to send plain 
text emails (SimpleEmail) and plain text with an attachment 
(MultiPartEmail), and its really quite nice, easy to use, etc. However, 
recently I've been looking into using commons-email for sending html 
email (and plain text alternative) with an attachment, and things seem 
somewhat broken.

What I'd like is a "multipart/mixed" email with 2 parts: part 1 is a 
nested "multipart/alternative" with plain text followed by text/html, 
and part 2 is the attachment.

The current HtmlEmail class documentation implies that an html email 
with a text alternative will work, but unfortunately it doesn't in many 
email programs (e.g. Apple Mail) because it sets creates a message with 
2 or more parts (html, then text, followed by any attachments) with a 
MIME subtype of "related". I guess the subtype of "related" is used for 
embedded image support, but it really kills the ability to have an html 
email with a text alternative. The MultiPartEmail and HtmlEmail classes 
really need to be modified to deal with nested parts so that things work 
properly across different mail user agents.

Below is a patch to MultiPartEmail that should keep things working the 
same way for current users of MultipartEmail and HtmlEmail, but adds 
some flexibility for people who need more control over how the message 
is constructed (set the subtype of the primary body part, and add nested 
multiparts). If there's anything I can do to improve the chances of 
getting this patch committed, just let me know :).

Thanks,
Scott


Index: MultiPartEmail.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-commons-sandbox/email/src/java/org/apache/commons/mail/MultiPartEmail.java,v
retrieving revision 1.6
diff -u -r1.6 MultiPartEmail.java
--- MultiPartEmail.java 19 Feb 2004 22:38:07 -0000      1.6
+++ MultiPartEmail.java 19 Aug 2004 04:10:36 -0000
@@ -52,6 +52,9 @@
      /** The message container. */
      private MimeBodyPart primaryBodyPart = null;

+    /** The MIME subtype. */
+    private String subType = null;
+
      /** Indicates if the message has been initialized */
      private boolean initialized = false;

@@ -70,14 +73,57 @@
          container = new MimeMultipart();
          super.setContent(container);

-        // Add the first body part to the message.  The fist body part 
must be
-        primaryBodyPart = new MimeBodyPart();
-        container.addBodyPart(primaryBodyPart);
-
          initialized = true;
      }

      /**
+     * Set the MIME subtype of the email.
+     */
+    public void setSubType(String subType)
+    {
+        this.subType = subType;
+    }
+
+    /**
+     * Get the MIME subtype of the email.
+     */
+    public String setSubType()
+    {
+        return subType;
+    }
+
+    /**
+     * Add a new part to the email.
+     * @param content The content.
+     * @param contentType The content type.
+     * @return An Email.
+     * @exception MessagingException
+     */
+    public Email addPart(String content, String contentType) throws 
MessagingException
+    {
+        MimeBodyPart bodyPart = new MimeBodyPart();
+        bodyPart.setContent(content, contentType);
+        getContainer().addBodyPart(bodyPart);
+
+        return this;
+    }
+
+    /**
+     * Add a new part to the email.
+     * @param part The MimeMultipart.
+     * @return An Email.
+     * @exception MessagingException
+     */
+    public Email addPart(MimeMultipart multipart) throws MessagingException
+    {
+        MimeBodyPart bodyPart = new MimeBodyPart();
+        bodyPart.setContent(multipart);
+        getContainer().addBodyPart(bodyPart);
+
+        return this;
+    }
+
+    /**
       * Set the message of the email.
       *
       * @param msg A String.
@@ -119,11 +165,17 @@
              // do nothing here.  content will be set to an empty string
              // as a result.
          }
-        if(content == null)
+
+        if (content == null)
          {
              body.setContent("", TEXT_PLAIN);
          }

+        if (subType != null)
+        {
+            getContainer().setSubType(subType);
+        }
+
          super.send();
      }

@@ -259,6 +311,13 @@
          if(!initialized) {
              init();
          }
+
+        if (primaryBodyPart == null)
+        {
+            primaryBodyPart = new MimeBodyPart();
+            container.addBodyPart(primaryBodyPart);
+        }
+
          return primaryBodyPart;
      }

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Re: [mail] html email with attachments

Posted by Scott <sc...@minutestopost.com>.
Martin Cooper wrote:
> 
> 
> On Tue, 31 Aug 2004, Scott wrote:
> 
>> Hi,
>>
>> Leandro Rodrigo Saad Cruz wrote:
>>
>>> You don't have to commit it.. just send the new patch against CVS HEAD
>>> to the list !
>>>
>>
>> Sorry if I'm missing something here, but I thought the patch I sent 
>> was against HEAD (rev. 1.6).
> 
> 
> Sending it embedded in the message itself usually causes problems when 
> someone tries to apply it, which may be what happened in this case. 
> You're better off attaching it to a bug report in Bugzilla.
> 
> -- 
> Martin Cooper
> 

This is in bugzilla as bug #30973:
http://issues.apache.org/bugzilla/show_bug.cgi?id=30973

Thanks,
Scott

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Re: [mail] html email with attachments

Posted by Martin Cooper <ma...@apache.org>.

On Tue, 31 Aug 2004, Scott wrote:

> Hi,
>
> Leandro Rodrigo Saad Cruz wrote:
>> You don't have to commit it.. just send the new patch against CVS HEAD
>> to the list !
>> 
>
> Sorry if I'm missing something here, but I thought the patch I sent was 
> against HEAD (rev. 1.6).

Sending it embedded in the message itself usually causes problems when 
someone tries to apply it, which may be what happened in this case. You're 
better off attaching it to a bug report in Bugzilla.

--
Martin Cooper


>
> Scott
>

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Re: [mail] html email with attachments

Posted by Scott <sc...@minutestopost.com>.
Hi,

Leandro Rodrigo Saad Cruz wrote:
> You don't have to commit it.. just send the new patch against CVS HEAD
> to the list !
> 

Sorry if I'm missing something here, but I thought the patch I sent was against HEAD (rev. 1.6).

Scott

[scott@localhost mail]$ cat CVS/Tag
NHEAD
[scott@localhost mail]$ patch < mp.patch
patching file MultiPartEmail.java
[scott@localhost mail]$ cvs diff -u MultiPartEmail.java
Index: MultiPartEmail.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-commons-sandbox/email/src/java/org/apache/commons/mail/MultiPartEmail.java,v
retrieving revision 1.6
diff -u -r1.6 MultiPartEmail.java
--- MultiPartEmail.java 19 Feb 2004 22:38:07 -0000      1.6
+++ MultiPartEmail.java 31 Aug 2004 14:45:40 -0000
@@ -52,6 +52,9 @@
      /** The message container. */
      private MimeBodyPart primaryBodyPart = null;

+    /** The MIME subtype. */
+    private String subType = null;
+
      /** Indicates if the message has been initialized */
      private boolean initialized = false;

@@ -70,14 +73,57 @@
          container = new MimeMultipart();
          super.setContent(container);

-        // Add the first body part to the message.  The fist body part must be
-        primaryBodyPart = new MimeBodyPart();
-        container.addBodyPart(primaryBodyPart);
-
          initialized = true;
      }

      /**
+     * Set the MIME subtype of the email.
+     */
+    public void setSubType(String subType)
+    {
+        this.subType = subType;
+    }
+
+    /**
+     * Get the MIME subtype of the email.
+     */
+    public String getSubType()
+    {
+        return subType;
+    }
+
+    /**
+     * Add a new part to the email.
+     * @param content The content.
+     * @param contentType The content type.
+     * @return An Email.
+     * @exception MessagingException
+     */
+    public Email addPart(String content, String contentType) throws MessagingException
+    {
+        MimeBodyPart bodyPart = new MimeBodyPart();
+        bodyPart.setContent(content, contentType);
+        getContainer().addBodyPart(bodyPart);
+
+        return this;
+    }
+
+    /**
+     * Add a new part to the email.
+     * @param part The MimeMultipart.
+     * @return An Email.
+     * @exception MessagingException
+     */
+    public Email addPart(MimeMultipart multipart) throws MessagingException
+    {
+        MimeBodyPart bodyPart = new MimeBodyPart();
+        bodyPart.setContent(multipart);
+        getContainer().addBodyPart(bodyPart);
+
+        return this;
+    }
+
+    /**
       * Set the message of the email.
       *
       * @param msg A String.
@@ -104,24 +150,33 @@
       */
      public void send() throws MessagingException
      {
-        // before a multipart message can be sent, we must make sure that
-        // the content for the main body part was actually set.  If not,
-        // an IOException will be thrown during super.send().
-
-        MimeBodyPart body = this.getPrimaryBodyPart();
-        Object content = null;
-        try
-        {
-            content = body.getContent();
-        }
-        catch (IOException e)
+        if (primaryBodyPart != null)
          {
-            // do nothing here.  content will be set to an empty string
-            // as a result.
+            // before a multipart message can be sent, we must make sure that
+            // the content for the main body part was actually set.  If not,
+            // an IOException will be thrown during super.send().
+
+            MimeBodyPart body = this.getPrimaryBodyPart();
+            Object content = null;
+            try
+            {
+                content = body.getContent();
+            }
+            catch (IOException e)
+            {
+                // do nothing here.  content will be set to an empty string
+                // as a result.
+            }
+
+            if (content == null)
+            {
+                body.setContent("", TEXT_PLAIN);
+            }
          }
-        if(content == null)
+
+        if (subType != null)
          {
-            body.setContent("", TEXT_PLAIN);
+            getContainer().setSubType(subType);
          }

          super.send();
@@ -259,6 +314,13 @@
          if(!initialized) {
              init();
          }
+
+        if (primaryBodyPart == null)
+        {
+            primaryBodyPart = new MimeBodyPart();
+            container.addBodyPart(primaryBodyPart);
+        }
+
          return primaryBodyPart;
      }

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Re: [mail] html email with attachments

Posted by Leandro Rodrigo Saad Cruz <le...@ibnetwork.com.br>.
You don't have to commit it.. just send the new patch against CVS HEAD
to the list !

On Tue, 2004-08-31 at 10:25, Scott wrote:
> Hi,
> 
> Unfortunately I don't have the ability to commit to commons-email cvs, however I will send the 
> patch to commons-dev in hopes of getting feedback from someone there.
> 
> Scott
> 
> Leandro Rodrigo Saad Cruz wrote:
> > Hi Scott. Could you send this patch against CVS HEAD ?
> > 
> > On Thu, 2004-08-19 at 02:07, Scott Dietrich wrote:
> > 
> >>Hi,
> >>
> >>I've been using commons-email (and turbine) for some time to send plain 
> >>text emails (SimpleEmail) and plain text with an attachment 
> >>(MultiPartEmail), and its really quite nice, easy to use, etc. However, 
> >>recently I've been looking into using commons-email for sending html 
> >>email (and plain text alternative) with an attachment, and things seem 
> >>somewhat broken.
> >>
> >>What I'd like is a "multipart/mixed" email with 2 parts: part 1 is a 
> >>nested "multipart/alternative" with plain text followed by text/html, 
> >>and part 2 is the attachment.
> >>
> >>The current HtmlEmail class documentation implies that an html email 
> >>with a text alternative will work, but unfortunately it doesn't in many 
> >>email programs (e.g. Apple Mail) because it sets creates a message with 
> >>2 or more parts (html, then text, followed by any attachments) with a 
> >>MIME subtype of "related". I guess the subtype of "related" is used for 
> >>embedded image support, but it really kills the ability to have an html 
> >>email with a text alternative. The MultiPartEmail and HtmlEmail classes 
> >>really need to be modified to deal with nested parts so that things work 
> >>properly across different mail user agents.
> >>
> >>Below is a patch to MultiPartEmail that should keep things working the 
> >>same way for current users of MultipartEmail and HtmlEmail, but adds 
> >>some flexibility for people who need more control over how the message 
> >>is constructed (set the subtype of the primary body part, and add nested 
> >>multiparts). If there's anything I can do to improve the chances of 
> >>getting this patch committed, just let me know :).
> >>
> >>Thanks,
> >>Scott
> >>
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
-- 
Leandro Rodrigo Saad Cruz
InterBusiness Tecnologia e Serviços
IB    - www.ibnetwork.com.br
DB    - www.digitalbrand.com.br
OJB   - db.apache.org/ojb
XINGU - xingu.sf.net

Re: [mail] html email with attachments

Posted by Scott <sc...@minutestopost.com>.
Hi,

Unfortunately I don't have the ability to commit to commons-email cvs, however I will send the 
patch to commons-dev in hopes of getting feedback from someone there.

Scott

Leandro Rodrigo Saad Cruz wrote:
> Hi Scott. Could you send this patch against CVS HEAD ?
> 
> On Thu, 2004-08-19 at 02:07, Scott Dietrich wrote:
> 
>>Hi,
>>
>>I've been using commons-email (and turbine) for some time to send plain 
>>text emails (SimpleEmail) and plain text with an attachment 
>>(MultiPartEmail), and its really quite nice, easy to use, etc. However, 
>>recently I've been looking into using commons-email for sending html 
>>email (and plain text alternative) with an attachment, and things seem 
>>somewhat broken.
>>
>>What I'd like is a "multipart/mixed" email with 2 parts: part 1 is a 
>>nested "multipart/alternative" with plain text followed by text/html, 
>>and part 2 is the attachment.
>>
>>The current HtmlEmail class documentation implies that an html email 
>>with a text alternative will work, but unfortunately it doesn't in many 
>>email programs (e.g. Apple Mail) because it sets creates a message with 
>>2 or more parts (html, then text, followed by any attachments) with a 
>>MIME subtype of "related". I guess the subtype of "related" is used for 
>>embedded image support, but it really kills the ability to have an html 
>>email with a text alternative. The MultiPartEmail and HtmlEmail classes 
>>really need to be modified to deal with nested parts so that things work 
>>properly across different mail user agents.
>>
>>Below is a patch to MultiPartEmail that should keep things working the 
>>same way for current users of MultipartEmail and HtmlEmail, but adds 
>>some flexibility for people who need more control over how the message 
>>is constructed (set the subtype of the primary body part, and add nested 
>>multiparts). If there's anything I can do to improve the chances of 
>>getting this patch committed, just let me know :).
>>
>>Thanks,
>>Scott
>>

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Re: [mail] html email with attachments

Posted by Leandro Rodrigo Saad Cruz <le...@ibnetwork.com.br>.
Hi Scott. Could you send this patch against CVS HEAD ?

On Thu, 2004-08-19 at 02:07, Scott Dietrich wrote:
> Hi,
> 
> I've been using commons-email (and turbine) for some time to send plain 
> text emails (SimpleEmail) and plain text with an attachment 
> (MultiPartEmail), and its really quite nice, easy to use, etc. However, 
> recently I've been looking into using commons-email for sending html 
> email (and plain text alternative) with an attachment, and things seem 
> somewhat broken.
> 
> What I'd like is a "multipart/mixed" email with 2 parts: part 1 is a 
> nested "multipart/alternative" with plain text followed by text/html, 
> and part 2 is the attachment.
> 
> The current HtmlEmail class documentation implies that an html email 
> with a text alternative will work, but unfortunately it doesn't in many 
> email programs (e.g. Apple Mail) because it sets creates a message with 
> 2 or more parts (html, then text, followed by any attachments) with a 
> MIME subtype of "related". I guess the subtype of "related" is used for 
> embedded image support, but it really kills the ability to have an html 
> email with a text alternative. The MultiPartEmail and HtmlEmail classes 
> really need to be modified to deal with nested parts so that things work 
> properly across different mail user agents.
> 
> Below is a patch to MultiPartEmail that should keep things working the 
> same way for current users of MultipartEmail and HtmlEmail, but adds 
> some flexibility for people who need more control over how the message 
> is constructed (set the subtype of the primary body part, and add nested 
> multiparts). If there's anything I can do to improve the chances of 
> getting this patch committed, just let me know :).
> 
> Thanks,
> Scott
> 
> 
> Index: MultiPartEmail.java
> ===================================================================
> RCS file: 
> /home/cvspublic/jakarta-commons-sandbox/email/src/java/org/apache/commons/mail/MultiPartEmail.java,v
> retrieving revision 1.6
> diff -u -r1.6 MultiPartEmail.java
> --- MultiPartEmail.java 19 Feb 2004 22:38:07 -0000      1.6
> +++ MultiPartEmail.java 19 Aug 2004 04:10:36 -0000
> @@ -52,6 +52,9 @@
>       /** The message container. */
>       private MimeBodyPart primaryBodyPart = null;
> 
> +    /** The MIME subtype. */
> +    private String subType = null;
> +
>       /** Indicates if the message has been initialized */
>       private boolean initialized = false;
> 
> @@ -70,14 +73,57 @@
>           container = new MimeMultipart();
>           super.setContent(container);
> 
> -        // Add the first body part to the message.  The fist body part 
> must be
> -        primaryBodyPart = new MimeBodyPart();
> -        container.addBodyPart(primaryBodyPart);
> -
>           initialized = true;
>       }
> 
>       /**
> +     * Set the MIME subtype of the email.
> +     */
> +    public void setSubType(String subType)
> +    {
> +        this.subType = subType;
> +    }
> +
> +    /**
> +     * Get the MIME subtype of the email.
> +     */
> +    public String setSubType()
> +    {
> +        return subType;
> +    }
> +
> +    /**
> +     * Add a new part to the email.
> +     * @param content The content.
> +     * @param contentType The content type.
> +     * @return An Email.
> +     * @exception MessagingException
> +     */
> +    public Email addPart(String content, String contentType) throws 
> MessagingException
> +    {
> +        MimeBodyPart bodyPart = new MimeBodyPart();
> +        bodyPart.setContent(content, contentType);
> +        getContainer().addBodyPart(bodyPart);
> +
> +        return this;
> +    }
> +
> +    /**
> +     * Add a new part to the email.
> +     * @param part The MimeMultipart.
> +     * @return An Email.
> +     * @exception MessagingException
> +     */
> +    public Email addPart(MimeMultipart multipart) throws MessagingException
> +    {
> +        MimeBodyPart bodyPart = new MimeBodyPart();
> +        bodyPart.setContent(multipart);
> +        getContainer().addBodyPart(bodyPart);
> +
> +        return this;
> +    }
> +
> +    /**
>        * Set the message of the email.
>        *
>        * @param msg A String.
> @@ -119,11 +165,17 @@
>               // do nothing here.  content will be set to an empty string
>               // as a result.
>           }
> -        if(content == null)
> +
> +        if (content == null)
>           {
>               body.setContent("", TEXT_PLAIN);
>           }
> 
> +        if (subType != null)
> +        {
> +            getContainer().setSubType(subType);
> +        }
> +
>           super.send();
>       }
> 
> @@ -259,6 +311,13 @@
>           if(!initialized) {
>               init();
>           }
> +
> +        if (primaryBodyPart == null)
> +        {
> +            primaryBodyPart = new MimeBodyPart();
> +            container.addBodyPart(primaryBodyPart);
> +        }
> +
>           return primaryBodyPart;
>       }
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
-- 
Leandro Rodrigo Saad Cruz
InterBusiness Tecnologia e Serviços
IB    - www.ibnetwork.com.br
DB    - www.digitalbrand.com.br
OJB   - db.apache.org/ojb
XINGU - xingu.sf.net