views:

100

answers:

1

Hi,

Can someone explain me how to create "simple" email with embedded images files ? I know that there are tons of RFC that explain it but I don't really succeeded to do it. I have to build the body text of the email programmatically, without using any framework. Don't ask me about the language is used...

Images are base64 encoded. Content-Type: Multipart/related; boundary="boundary-example-1";type=Text/HTML

If there is a "readable" explanation about the way to do this on some web site that i didn't found, i'll be glad to hear about it.

Pierre

+2  A: 

There are tons of RFC's because it's not that simple.

To embed images and/or attachments in the email message MIME format was created.

MIME is actually a tree structure:

+-multipart/mixed
  +-multipart/related
  | +-multipart/alternative
  | |  +-text/plain
  | |  +-text/html
  | +-image/gif
  | +-image/jpeg
  +-application/x-zip-compressed
  +-application/x-zip-compressed

Each multipart mime object consist of one or more parts. Parts are separated using boundary delimiter.

multipart/mixed - is used when parts are not related (regular email with attachments)

multipart/related - is used when pars are related. For example HTML email that contains images used by this HTML. (Cid protocol is used in HTML: )

multipart/alternative - specifies that parts contain same content but in different representations. For example same text in doc format, html and plain text.

  1. So first you need to build correct mime structure,
  2. then encode all mime parts using appropriate encodings (Base64, QuotedPrintable),
  3. add correct headers to all parts,
  4. encode them if they contain national or special characters,
  5. assign boundaries
  6. and finally render entire message

The main point is this problem has been already solved. Most languages have libraries to create and send emails.

.NET framework has one, not perfect, but still it's probably better that what you'll create.

You can also check out commercial products like Mail.dll email component if you need more control on how the email looks like.

Pawel Lesnikowski
Pawel, if I can vote you up 10K times, i'd do it. Thanks a lot for this bright explanation. (When I will have 15 rep. points, I'll be back to thank you the way you deserve it)
Pierre W