NVM
NVM

Reputation: 5552

How to create HTML email with embeded image and show it in the default mail client using .net (c#)?

If you look at the Snip tool in windows there is an option to send the snip as an embedded attachment in the default mail client (I guess as an html email). I need exactly that.

At the moment I am using simple MAPI to attach the image but that does not allow embedding.

One key requirement is that it should be created and then shown in the default mail client. I don't think I can use System.Net.Mail for that. Or can I?

Its a WPF application using c#.

EDIT: Please read the question carefully before answering. I am not new to this. I know how to do this in unmanaged code without resorting to hacks. I need a managed equivalent. I am not looking for hacks and am happy to spend significant time on the correct solution if necessary.

Upvotes: 2

Views: 3234

Answers (3)

jgauffin
jgauffin

Reputation: 101176

Use Process.Start with the ShellExecute option on this:

mailto:[email protected]?subject=your+file&body=see attachment&attachment="C:\path\to\file.txt"

I'm unsure of the quotes around the path.

Edit

Just thought of another solution which should work.

  1. Create a HTML email using MailMessage
  2. Save it using the answer here as XXXX.eml
  3. Use ShellExecute option with Process.Start the open the EML file.

It should open it in the default email client.

Upvotes: 3

Harvey Kwok
Harvey Kwok

Reputation: 11883

I think you can use classes in namespace System.Net.Mail to do this job. This requires quite some work. I will leave it as reader's exercise.

According to RFC2111, a HTML email with embedded image is just storing HTML email body and the image attachment in two different MIME content.

 From: [email protected]
 To: [email protected]
 Subject: A simple example
 Mime-Version: 1.0
 Content-Type: multipart/related; boundary="boundary-example-1";
               type=Text/HTML

 --boundary-example 1
 Content-Type: Text/HTML; charset=US-ASCII

 ... text of the HTML document, which might contain a hyperlink
 to the other body part, for example through a statement such as:
 <IMG SRC="cid:foo4*[email protected]" ALT="IETF logo">

 --boundary-example-1
 Content-ID: foo4*[email protected]
 Content-Type: IMAGE/GIF
 Content-Transfer-Encoding: BASE64

 R0lGODlhGAGgAPEAAP/////ZRaCgoAAAACH+PUNvcHlyaWdodCAoQykgMTk5
 NSBJRVRGLiBVbmF1dGhvcml6ZWQgZHVwbGljYXRpb24gcHJvaGliaXRlZC4A
 etc...

 --boundary-example-1--

The key point is that in your HTML email body, instead of using a regular URL path, you put in "cid:xxxxx" where xxxxx is your Content ID. Check RFC2111 for more details about Content ID. From what I see in the System.Net.Mail namespace, it allows you to create a MailMessage with IsBodyHtml property set to true. Then, the next thing you need to do is to add Attachment to this MailMessage. The Attachment is of course your embedded image. Then, you need to remember to set the ContentID of the Attachment. So, just to make sure your HTML email is referencing the correct ContentID, the things should work.

Again, I didn't verify it myself but I did a quick check on some of my emails with embedded image in it. I did see the email with cid:xxxx as the image source and my Outlook client can really open it fine.

Upvotes: 0

dhinesh
dhinesh

Reputation: 4764

I do not think that in mailto you can embed images Please refer to the RFC 2368. It says that mailto purpose is only for short text messages and not for MIME bodies

   The special hname "body" indicates that the associated hvalue is the
   body of the message. The "body" hname should contain the content for
   the first text/plain body part of the message. The mailto URL is
   primarily intended for generation of short text messages that are
   actually the content of automatic processing (such as "subscribe"
   messages for mailing lists), not general MIME bodies.

Upvotes: 4

Related Questions