You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Scott <sc...@minutestopost.com> on 2004/08/20 22:04:12 UTC
[email] html email with attachments - update
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("me@mydomain.com");
email.addTo("you@yourdomain.com");
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: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org
Re: [email] html email with attachments - update
Posted by Scott <sc...@minutestopost.com>.
Hi,
Martin Cooper wrote:
> On Fri, 20 Aug 2004 16:04:12 -0400, Scott <sc...@minutestopost.com> wrote:
>
>>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("me@mydomain.com");
>>email.addTo("you@yourdomain.com");
>>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()
>
>
> No doubt I'm missing something obvious here, but why would you want to
> construct a message like the one above? Unless I'm mistaken, what the
> above code creates is a message structured like:
>
> multipart/mixed
> multipart/alternate
> text/plain
> text/html
>
> Now I understand the alternate with text and HTML, by why are you
> wrapping this in another multipart (which only has one part!) of type
> mixed?
>
Yes, the above example doesn't make sense unless you're adding
attachments. What I'd like is:
multipart/mixed
multipart/alternative
text/plain
text/html
application/pdf
etc.
The current MultiPartEmail doesn't allow you to do this, and the
HtmlEmail class won't do the job either (plain text alternatives do not
work properly with HtmlEmail). To be honest, I'd prefer to rewrite
larger parts of both classes (wouldn't actually be a lot of work to get
everything sorted out, I don't think) than use the patch, but thought
I'd start somewhere :).
Scott
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org
Re: [email] html email with attachments - update
Posted by Martin Cooper <mf...@gmail.com>.
On Fri, 20 Aug 2004 16:04:12 -0400, Scott <sc...@minutestopost.com> wrote:
> 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("me@mydomain.com");
> email.addTo("you@yourdomain.com");
> 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()
No doubt I'm missing something obvious here, but why would you want to
construct a message like the one above? Unless I'm mistaken, what the
above code creates is a message structured like:
multipart/mixed
multipart/alternate
text/plain
text/html
Now I understand the alternate with text and HTML, by why are you
wrapping this in another multipart (which only has one part!) of type
mixed?
--
Martin Cooper
>
> 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: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org