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("[EMAIL PROTECTED]");
email.addTo("[EMAIL PROTECTED]");
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()
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: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
