quintonm    2003/03/09 20:19:26

  Modified:    email/src/java/org/apache/commons/mail MultiPartEmail.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.
  
  Revision  Changes    Path
  1.4       +49 -20    
jakarta-commons-sandbox/email/src/java/org/apache/commons/mail/MultiPartEmail.java
  
  Index: MultiPartEmail.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons-sandbox/email/src/java/org/apache/commons/mail/MultiPartEmail.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- MultiPartEmail.java       10 Mar 2003 00:27:33 -0000      1.3
  +++ MultiPartEmail.java       10 Mar 2003 04:19:25 -0000      1.4
  @@ -85,10 +85,10 @@
   public class MultiPartEmail extends Email
   {
       /** Body portion of the email. */
  -    private MimeMultipart emailBody = null;
  +    private MimeMultipart container = null;
   
       /** The message container. */
  -    private MimeBodyPart messageContainer = null;
  +    private MimeBodyPart primaryBodyPart = null;
   
       /** Indicates if the message has been initialized */
       private boolean initialized = false;
  @@ -105,13 +105,12 @@
               throw new IllegalStateException("Already initialized");
           }
   
  -        /* The body of the mail. */
  -        emailBody = new MimeMultipart();
  -        super.setContent(emailBody);
  -
  -        /* The messageContainer message content. */
  -        messageContainer = new MimeBodyPart();
  -        emailBody.addBodyPart(messageContainer);
  +        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;
       }
  @@ -127,16 +126,46 @@
       {
           if (charset != null)
           {
  -            getMessageContainer().setText(msg, charset);
  +            getPrimaryBodyPart().setText(msg, charset);
           }
           else
           {
  -            getMessageContainer().setText(msg);
  +            getPrimaryBodyPart().setText(msg);
           }
           return this;
       }
   
       /**
  +     * Sends the mail message
  +     *
  +     * @throws MessagingException
  +     */
  +    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)
  +        {
  +            // do nothing here.  content will be set to an empty string
  +            // as a result.
  +        }
  +        if(content == null)
  +        {
  +            body.setContent("", TEXT_PLAIN);
  +        }
  +
  +        super.send();
  +    }
  +
  +    /**
        * Attach an EmailAttachement.
        *
        * @param attachment An EmailAttachment.
  @@ -243,7 +272,7 @@
               throws MessagingException
       {
           MimeBodyPart mbp = new MimeBodyPart();
  -        getEmailBody().addBodyPart(mbp);
  +        getContainer().addBodyPart(mbp);
   
           mbp.setDisposition(disposition);
           if (StringUtils.isEmpty(name))
  @@ -258,31 +287,31 @@
       }
   
       /**
  -     * Gets the message container.
  +     * Gets first body part of the message.
        *
  -     * @return The message container.
  +     * @return The primary body part.
        * @throws MessagingException message could not be initialized
        */
  -    protected MimeBodyPart getMessageContainer() throws MessagingException
  +    protected MimeBodyPart getPrimaryBodyPart() throws MessagingException
       {
           if(!initialized) {
               init();
           }
  -        return messageContainer;
  +        return primaryBodyPart;
       }
   
       /**
  -     * Gets the message body.
  +     * Gets the message container.
        *
        * @return The message container.
        * @throws MessagingException message could not be initialized
        */
  -    protected MimeMultipart getEmailBody() throws MessagingException
  +    protected MimeMultipart getContainer() throws MessagingException
       {
           if(!initialized) {
               init();
           }
  -        return emailBody;
  +        return container;
       }
   
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to