commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Christopher Schultz <ch...@christopherschultz.net>
Subject Re: Apache Commons Email and Attachments
Date Tue, 25 Aug 2009 17:23:23 GMT
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Tom,

On 8/25/2009 10:45 AM, TomSimmons wrote:
> I'm hoping someone can help me with sending and HTML Email with attachments.

I had a lot of trouble with various mail APIs, including commons-email,
and so I basically researched everything and wrote my own based upon the
Javamail API.

I'm sorry, I can't remember the details of what wasn't working, but I
wanted to do the following and commons-email wasn't working properly
with some combination of:

1. HTML body
2. Text body
3. File attachment

I know I wanted 1+2 and sometimes 3 as well. I think what was happening
was that the file attachment made the text (or HTML) look like an
attachment instead of an alternative body (or the attachment was
attached to only one of the bodies or whatever).

I wrote a single class that does everything (except for embedding
attachments for, say, images within an HTML message... I stopped working
on it a while ago because I had what I wanted already) instead of the
multi-class implementation provided by commons-email.

> I was hoping that I could just use the attachment example from the website,
> along with the HTML example, however this doesn't seem to be the case.  When
> I try it in Outlook I received a text email with the HTML version as an
> attachment and the files I wanted attached.  While just to be different
> Notes displays the HTML email and the required file attachment correctly.
> 
> There seems to be some suggestion from what little I can find online that
> you can't use HtmlEmail and have to instead build a multipart, is this true,
> does someone have an example?

Maybe that's the problem: that HtmlEmail is "untrustworthy" :) I know
that, from my experience, things get very complicated in a hurry when
dealing with MIME messages with multiple parts. I had lots of notes
about what has to be what (like multipart/alternative versus
multipart/attachment). I have this note in my source code:

        //
        // Now, get down to the real work: the content.
        //
        // Messages with both text/plain and text/html
        // need to have a structure like this:
        //
        // multipart/alternate { text/plain, text/html }
        //
        // Messages with attachments need to look like this:
        //
        // multipart/mixed { (actual message), att1, att2, ... }
        //

        //
        // First, figure out what the top-level looks like.
        // If we have attachments, it's multipart/mixed.
        //

I think what HtmlEmail ends up with is a multipart/alternative where the
html and text bodies are alternatives, and so are the file attachments
(or, clients that aren't super-smart see the messages this way: Mozilla
Thunderbird was my test subject and I assume it's as standards-compliant
as they come, and it got confused with messages generated by HtmlEmail).

The key is that you need a top-level multipart/mixed message with a
sub-level multipart/alternate part (with both text & html bodies in
there) and then your attachments are attached to the top-level part as
siblings of the text/html alternative part (got all that?). The code
comment is a bit easier to understand, IMO.

I'm happy to share my code if you're interested.

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAkqUHgsACgkQ9CaO5/Lv0PCGtwCfVBw6ifj6JNnqtBBsWT1q5HMf
UWUAoKQCUrjiAGZJAShBSYGDNp9bmmRX
=XbzX
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Mime
View raw message