quintonm 2003/03/09 16:27:33
Modified: email/src/java/org/apache/commons/mail MultiPartEmail.java
Log:
Moved package declaration to the first line of the file.
Default constructor has been removed. It was there previously to call init().
init() is now called within the other methods to avoid declaring
MessagingException on the constructor.
The init() method can only be called once. Calling it more than once will
result in an IllegalStateException.
When attaching an instance of EmailAttachment:
- FileDataSource will be used instead of URLDataSource if there is no URL.
- A MessagingException will be thrown if the file does not exist. It should
have worked this way before but testing showed that a bad filename for the
attachment did not cause the exception to be thrown.
- The path (setPath()) does not have to be an absolute path anymore. Relative
paths are now accepted.
- If you do not set the name for the attachment, it will default to the filename.
emailBody and main (mime parts) are now private. Protected methods have been
added to allow access from subclasses. Calling either one of these methods will
cause init() to be called if it has not been already.
Revision Changes Path
1.3 +102 -64
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.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- MultiPartEmail.java 19 Jan 2003 20:06:17 -0000 1.2
+++ MultiPartEmail.java 10 Mar 2003 00:27:33 -0000 1.3
@@ -1,3 +1,5 @@
+package org.apache.commons.mail;
+
/* ====================================================================
* The Apache Software License, Version 1.1
*
@@ -51,18 +53,17 @@
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
-package org.apache.commons.mail;
-
import java.net.URL;
+import java.io.File;
+import java.io.IOException;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.URLDataSource;
-import javax.mail.BodyPart;
+import javax.activation.FileDataSource;
import javax.mail.MessagingException;
-import javax.mail.Multipart;
import javax.mail.internet.MimeBodyPart;
-import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
+import org.apache.commons.lang.StringUtils;
/**
* A multipart email.
@@ -74,52 +75,45 @@
* then you can call setMsg() to set the message and call the
* different attach() methods.
*
+ * @author <a href="mailto:[EMAIL PROTECTED]">Quinton McCombs</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Jon S. Stevens</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Frank Y. Kim</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Brett McLaughlin</a>
* @author <a href="mailto:unknown">Regis Koenig</a>
* @version $Id$
*/
-public class MultiPartEmail
- extends Email
+public class MultiPartEmail extends Email
{
/** Body portion of the email. */
- protected MimeMultipart emailBody = null;
+ private MimeMultipart emailBody = null;
/** The message container. */
- protected MimeBodyPart main = null;
-
- /** The file server if it exists. */
- private String fileServer = "localhost";
-
- /**
- * Initialize the multipart email.
- *
- * @exception MessagingException.
- */
- public MultiPartEmail() throws MessagingException
- {
- this.init();
- }
+ private MimeBodyPart messageContainer = null;
+ /** Indicates if the message has been initialized */
+ private boolean initialized = false;
/**
* Initialize the multipart email.
*
- * @exception MessagingException.
+ * @exception MessagingException
*/
- protected void init()
- throws MessagingException
+ private void init() throws MessagingException
{
- // super.init(aHostName);
+ if (initialized)
+ {
+ throw new IllegalStateException("Already initialized");
+ }
/* The body of the mail. */
emailBody = new MimeMultipart();
super.setContent(emailBody);
- /* The main message content. */
- main = new MimeBodyPart();
- emailBody.addBodyPart(main);
+ /* The messageContainer message content. */
+ messageContainer = new MimeBodyPart();
+ emailBody.addBodyPart(messageContainer);
+
+ initialized = true;
}
/**
@@ -127,17 +121,17 @@
*
* @param msg A String.
* @return An Email.
- * @exception MessagingException.
+ * @exception MessagingException
*/
public Email setMsg(String msg) throws MessagingException
{
if (charset != null)
{
- main.setText(msg, charset);
+ getMessageContainer().setText(msg, charset);
}
else
{
- main.setText(msg);
+ getMessageContainer().setText(msg);
}
return this;
}
@@ -147,27 +141,42 @@
*
* @param attachment An EmailAttachment.
* @return A MultiPartEmail.
- * @exception MessagingException.
+ * @exception MessagingException
*/
public MultiPartEmail attach(EmailAttachment attachment)
- throws MessagingException
+ throws MessagingException
{
+ MultiPartEmail result = null;
+
URL url = attachment.getURL();
if (url == null)
{
+ String fileName = null;
try
{
- String file = attachment.getPath();
- url = new URL("file", fileServer, file);
+ fileName = attachment.getPath();
+ File file = new File(fileName);
+ if (!file.exists())
+ {
+ throw new IOException("\""+fileName + "\" does not exist");
+ }
+ result = attach(new FileDataSource(file), attachment.getName(),
+ attachment.getDescription(),
+ attachment.getDisposition());
}
catch (Exception e)
{
- throw new MessagingException("Cannot find file", e);
+ throw new MessagingException("Cannot attach file \""
+ +fileName+"\"", e);
}
}
+ else
+ {
+ result = attach(url, attachment.getName(),
+ attachment.getDescription(), attachment.getDisposition());
+ }
- return attach(url, attachment.getName(),
- attachment.getDescription(), attachment.getDisposition());
+ return result;
}
/**
@@ -178,10 +187,10 @@
* @param name The name field for the attachment.
* @param description A description for the attachment.
* @return A MultiPartEmail.
- * @exception MessagingException.
+ * @exception MessagingException
*/
- public MultiPartEmail attach(URL url, String name, String description)
- throws MessagingException
+ public MultiPartEmail attach(URL url, String name, String description)
+ throws MessagingException
{
return attach(url, name, description, EmailAttachment.ATTACHMENT);
}
@@ -194,16 +203,14 @@
* @param description A description for the attachment.
* @param disposition Either mixed or inline.
* @return A MultiPartEmail.
- * @exception MessagingException.
+ * @exception MessagingException
*/
- public MultiPartEmail attach(URL url,
- String name,
- String description,
- String disposition)
- throws MessagingException
+ public MultiPartEmail attach(URL url, String name, String description,
+ String disposition)
+ throws MessagingException
{
return attach(
- new URLDataSource(url), name, description, disposition);
+ new URLDataSource(url), name, description, disposition);
}
/**
@@ -213,12 +220,12 @@
* @param name The name field for the attachment.
* @param description A description for the attachment.
* @return A MultiPartEmail.
- * @exception MessagingException.
+ * @exception MessagingException
*/
- public MultiPartEmail attach(DataSource ds, String name, String description)
- throws MessagingException
+ public MultiPartEmail attach(DataSource ds, String name, String description)
+ throws MessagingException
{
- return attach (ds, name, description, EmailAttachment.ATTACHMENT);
+ return attach(ds, name, description, EmailAttachment.ATTACHMENT);
}
/**
@@ -229,22 +236,53 @@
* @param description A description for the attachement.
* @param disposition Either mixed or inline.
* @return A MultiPartEmail.
- * @exception MessagingException.
+ * @exception MessagingException
*/
- public MultiPartEmail attach(DataSource ds,
- String name,
- String description,
- String disposition)
- throws MessagingException
+ public MultiPartEmail attach(DataSource ds, String name,
+ String description, String disposition)
+ throws MessagingException
{
MimeBodyPart mbp = new MimeBodyPart();
- emailBody.addBodyPart(mbp);
+ getEmailBody().addBodyPart(mbp);
mbp.setDisposition(disposition);
- mbp.setFileName (name);
- mbp.setDescription (description);
- mbp.setDataHandler (new DataHandler(ds));
+ if (StringUtils.isEmpty(name))
+ {
+ name = ds.getName();
+ }
+ mbp.setFileName(name);
+ mbp.setDescription(description);
+ mbp.setDataHandler(new DataHandler(ds));
return this;
}
+
+ /**
+ * Gets the message container.
+ *
+ * @return The message container.
+ * @throws MessagingException message could not be initialized
+ */
+ protected MimeBodyPart getMessageContainer() throws MessagingException
+ {
+ if(!initialized) {
+ init();
+ }
+ return messageContainer;
+ }
+
+ /**
+ * Gets the message body.
+ *
+ * @return The message container.
+ * @throws MessagingException message could not be initialized
+ */
+ protected MimeMultipart getEmailBody() throws MessagingException
+ {
+ if(!initialized) {
+ init();
+ }
+ return emailBody;
+ }
+
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]