commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From quint...@apache.org
Subject cvs commit: jakarta-commons-sandbox/email/src/java/org/apache/commons/mail HtmlEmail.java
Date Mon, 10 Mar 2003 04:25:29 GMT
quintonm    2003/03/09 20:25:29

  Modified:    email/src/java/org/apache/commons/mail HtmlEmail.java
  Log:
  Changed the names of the instance variables containing MimeMultipart and the first BodyPart.
 The
  old names were exactly opposite of what they actually contained.  The corresponding get
methods
  were also updated.
  
  send() has been overridden to set the content of the first body part to an empty string
if it
  is null.  This prevents an IOException from being thrown when the message is sent without
first
  calling setMsg().  A good example of this would be sending an attachment without a message
in the
  body.
  
  isValid() has been deprecated.  Use StringUtils.isNotEmpty() instead.
  
  Revision  Changes    Path
  1.6       +63 -93    jakarta-commons-sandbox/email/src/java/org/apache/commons/mail/HtmlEmail.java
  
  Index: HtmlEmail.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/email/src/java/org/apache/commons/mail/HtmlEmail.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- HtmlEmail.java	19 Jan 2003 20:03:11 -0000	1.5
  +++ HtmlEmail.java	10 Mar 2003 04:25:29 -0000	1.6
  @@ -54,14 +54,17 @@
   package org.apache.commons.mail;
   
   import java.net.URL;
  +import java.util.List;
  +import java.util.ArrayList;
  +import java.util.Iterator;
   import javax.activation.DataHandler;
  -import javax.activation.DataSource;
   import javax.activation.URLDataSource;
   import javax.mail.BodyPart;
   import javax.mail.MessagingException;
  -import javax.mail.Multipart;
   import javax.mail.internet.MimeBodyPart;
   import javax.mail.internet.MimeMultipart;
  +import org.apache.commons.lang.StringUtils;
  +import org.apache.commons.lang.RandomStringUtils;
   
   /**
    * An HTML multipart email.
  @@ -86,48 +89,27 @@
    * @author <a href="mailto:sean@informage.net">Sean Legassick</a>
    * @version $Id$
    */
  -public class HtmlEmail 
  -    extends MultiPartEmail
  +public class HtmlEmail extends MultiPartEmail
   {
  -    protected MimeMultipart htmlContent;
  -    protected String text;
  -    protected String html;
  -
       /**
  -     * Basic constructor.
  -     *
  -     * @exception MessagingException.
  +     * Text part of the message.  This will be used as alternative text if
  +     * the email client does not support HTML messages.
        */
  -    public HtmlEmail() 
  -        throws MessagingException
  -    {
  -        this.init();
  -    }
  +    private String text;
   
  -    /**
  -     * Instantiates a new MimeMultipart object if it isn't already
  -     * instantiated.
  -     *
  -     * @return A MimeMultipart object
  -     */
  -    public MimeMultipart getHtmlContent()
  -    {
  -        if (htmlContent == null)
  -        {
  -            htmlContent = new MimeMultipart();
  -        }
  -        return htmlContent;
  -    }
  +    /** Html part of the message */
  +    private String html;
  +
  +    /** Embeded images */
  +    private List inlineImages = new ArrayList();
   
       /**
        * Set the text content.
        *
        * @param text A String.
        * @return An HtmlEmail.
  -     * @exception MessagingException.
        */
  -    public HtmlEmail setTextMsg(String text) 
  -        throws MessagingException
  +    public HtmlEmail setTextMsg(String text)
       {
           this.text = text;
           return this;
  @@ -138,10 +120,8 @@
        *
        * @param html A String.
        * @return An HtmlEmail.
  -     * @exception MessagingException.
        */
  -    public HtmlEmail setHtmlMsg(String html) 
  -        throws MessagingException
  +    public HtmlEmail setHtmlMsg(String html)
       {
           this.html = html;
           return this;
  @@ -158,19 +138,17 @@
        *
        * @param msg A String.
        * @return An Email.
  -     * @exception MessagingException.
        */
  -    public Email setMsg(String msg) 
  -        throws MessagingException
  +    public Email setMsg(String msg)
       {
           setTextMsg(msg);
  -        
  +
           setHtmlMsg(new StringBuffer()
  -            .append("<html><body><pre>")
  -            .append(msg)
  -            .append("</pre></body></html>")
  -            .toString());
  -        
  +                .append("<html><body><pre>")
  +                .append(msg)
  +                .append("</pre></body></html>")
  +                .toString());
  +
           return this;
       }
   
  @@ -193,88 +171,79 @@
        * @param name The name that will be set in the filename header
        * field.
        * @return A String with the Content-ID of the file.
  -     * @exception MessagingException.
  +     * @exception MessagingException
        */
  -    public String embed(URL url, String name) 
  -        throws MessagingException
  +    public String embed(URL url, String name) throws MessagingException
       {
           MimeBodyPart mbp = new MimeBodyPart();
   
           mbp.setDataHandler (new DataHandler(new URLDataSource(url)));
           mbp.setFileName(name);
           mbp.setDisposition("inline");
  -        String cid = GenerateUniqueID.getIdentifier();
  +        String cid = RandomStringUtils.randomAscii(10);
           mbp.addHeader("Content-ID", cid);
   
  -        getHtmlContent().addBodyPart(mbp);
  +        inlineImages.add(mbp);
           return mbp.getContentID();
       }
   
       /**
        * Does the work of actually sending the email.
        *
  -     * @exception MessagingException, if there was an error.
  +     * @exception MessagingException if there was an error.
        */
  -    public void send() 
  -        throws MessagingException
  +    public void send() throws MessagingException
       {
  -        MimeBodyPart msgText = null;
  -        MimeBodyPart msgHtml = null;
  +        MimeMultipart container = this.getContainer();
  +        container.setSubType("related");
   
  -        if (isValid(text) && isValid(html))
  -        {
  -            // The message in text and HTML form.
  -            MimeMultipart msg = getHtmlContent();
  -            msg.setSubType("alternative");
  -            main.setContent(msg);
  -
  -            msgText = new MimeBodyPart();
  -            msgHtml = new MimeBodyPart();
  -            msg.addBodyPart(msgText);
  -            msg.addBodyPart(msgHtml);
  +        BodyPart msgText = null;
  +        BodyPart msgHtml = null;
   
  -        }
  -        else if (isValid(text))
  -        {
  -            // just text so the text part is the main part
  -            msgText = main;
  -        }
  -        else if (isValid(html))
  -        {
  -            // just HTML so the html part is the main part
  -            msgHtml = main;
  -        }
  -        else
  +        if (StringUtils.isNotEmpty(html))
           {
  -            msgText = main;
  -            text = "NO BODY";
  -        }
  -
  -        if (msgText != null)
  -        {
  -            // add the text
  +            msgHtml = this.getPrimaryBodyPart();
               if (charset != null)
               {
  -                msgText.setText(text, charset);
  +                msgHtml.setContent(html, TEXT_HTML + ";charset=" + charset);
               }
               else
               {
  -                msgText.setText(text);
  +                msgHtml.setContent(html, TEXT_HTML);
  +            }
  +
  +            for (Iterator iter = inlineImages.iterator(); iter.hasNext();)
  +            {
  +                container.addBodyPart((BodyPart)iter.next());
               }
  +
           }
   
  -        if (msgHtml != null)
  +        if (StringUtils.isNotEmpty(text))
           {
  -            // add the html
  +            // if the html part of the message was null, then the text part
  +            // will become the primary body part
  +            if (msgHtml == null)
  +            {
  +                msgText = getPrimaryBodyPart();
  +            }
  +            else
  +            {
  +                msgText = new MimeBodyPart();
  +                container.addBodyPart(msgText);
  +            }
  +
               if (charset != null)
               {
  -                msgHtml.setContent(html, TEXT_HTML + ";charset=" + charset);
  +                msgText.setContent(text, TEXT_PLAIN + ";charset=" + charset);
               }
               else
               {
  -                msgHtml.setContent(html, TEXT_HTML);
  +                msgText.setContent(text, TEXT_PLAIN);
               }
  +
           }
  +
           super.send();
       }
   
  @@ -284,9 +253,10 @@
        *
        * @param foo The text to check.
        * @return Whether valid.
  +     * @deprecated use StringUtils.isNotEmpty instead
        */
       public static final boolean isValid(String foo)
       {
  -        return (foo != null && foo.length() > 0);
  +        return StringUtils.isNotEmpty(foo);
       }
   }
  
  
  

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


Mime
View raw message