commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Martin Cooper <mfncoo...@gmail.com>
Subject Re: [email] html email with attachments - update
Date Fri, 20 Aug 2004 22:23:05 GMT
On Fri, 20 Aug 2004 16:04:12 -0400, Scott <scott@minutestopost.com> wrote:
> Hi,
> 
> Attached is an updated (somewhat quick-and-dirty) patch for
> MultiPartEmail.java (the previous one had problems :)). It allows you do
> to something like:
> 
> MultiPartEmail email = new MultiPartEmail();
> email.setFrom("me@mydomain.com");
> email.addTo("you@yourdomain.com");
> email.setSubject("message subject");
> email.setSubType("mixed");
> 
> MimeMultipart multiPart = new MimeMultipart("alternative");
> 
> // text alternative
> MimeBodyPart bodyPart1 = new MimeBodyPart();
> bodyPart1.setContent("Text message", "text/plain");
> multiPart.addBodyPart(bodyPart1);
> 
> // html alternative
> MimeBodyPart bodyPart2 = new MimeBodyPart();
> bodyPart2.setContent("<html><body><p>html message</p></body></html>",
> "text/html");
> multiPart.addBodyPart(bodyPart2);
> 
> email.addPart(multiPart);
> 
> // add attachments if you'd like, then
> email.send()

No doubt I'm missing something obvious here, but why would you want to
construct a message like the one above? Unless I'm mistaken, what the
above code creates is a message structured like:

    multipart/mixed
        multipart/alternate
            text/plain
            text/html

Now I understand the alternate with text and HTML, by why are you
wrapping this in another multipart (which only has one part!) of type
mixed?

--
Martin Cooper


> 
> 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 20 Aug 2004 19:02:57 -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,26 +150,35 @@
>       */
>      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
> 
>

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


Mime
View raw message