Hi,
I've been using commons-email (and turbine) for some time to send plain
text emails (SimpleEmail) and plain text with an attachment
(MultiPartEmail), and its really quite nice, easy to use, etc. However,
recently I've been looking into using commons-email for sending html
email (and plain text alternative) with an attachment, and things seem
somewhat broken.
What I'd like is a "multipart/mixed" email with 2 parts: part 1 is a
nested "multipart/alternative" with plain text followed by text/html,
and part 2 is the attachment.
The current HtmlEmail class documentation implies that an html email
with a text alternative will work, but unfortunately it doesn't in many
email programs (e.g. Apple Mail) because it sets creates a message with
2 or more parts (html, then text, followed by any attachments) with a
MIME subtype of "related". I guess the subtype of "related" is used for
embedded image support, but it really kills the ability to have an html
email with a text alternative. The MultiPartEmail and HtmlEmail classes
really need to be modified to deal with nested parts so that things work
properly across different mail user agents.
Below is a patch to MultiPartEmail that should keep things working the
same way for current users of MultipartEmail and HtmlEmail, but adds
some flexibility for people who need more control over how the message
is constructed (set the subtype of the primary body part, and add nested
multiparts). If there's anything I can do to improve the chances of
getting this patch committed, just let me know :).
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 19 Aug 2004 04:10:36 -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 setSubType()
+ {
+ 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.
@@ -119,11 +165,17 @@
// do nothing here. content will be set to an empty string
// as a result.
}
- if(content == null)
+
+ if (content == null)
{
body.setContent("", TEXT_PLAIN);
}
+ if (subType != null)
+ {
+ getContainer().setSubType(subType);
+ }
+
super.send();
}
@@ -259,6 +311,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
|