Sending files via the default e-mail client

I recently released a "Send To Email" plugin for Cropper, which makes it easy to grab a portion of your screen and send it as an e-mail attachment. It's easy enough to directly send an e-mail in .NET using System.Net.Mail, but there's no provision for sending a mail using the default client.

Spoiler - if you just want the code without the background, skip to the "Take Three" section below.

Take One - MailTo link

You can launch the default mail client by shelling out to a mailto link, and some e-mail Cropper - Send To Emailclients support an "attach" parameter:

mailto:dvader@deathstar.mil
?Subject=Rebel+Base+Detected
&Body=See+attached+pictures
&Attach="C:\Users\Fett\RebelBase[1].png

public static void Main() { SendMailWithMailTo( "dvader@deathstar.mil", "Rebel Base Detected", "Hoth, baybee!!", "\"C:\\Users\\Fett\\RebelBase[1].png\""); } public static void SendMailWithMailTo( string address, string subject, string body, string attach) { //Don't use this - just an example string mailto = string.Format( "mailto:{0}?Subject={1}&Body={2}&Attach={3}", address,subject,body,attach); System.Diagnostics.Process.Start(mailto); }

That's not really a good plan, though. The main reason is that the "attach" is an unofficial extension to the mailto protocol. It's not supported in a lot of e-mail clients, and those that implement it all work a little differently (for instance, some use "attachment" instead of "attach". I'd expect that fewer mail clients will be support attachments in the mailto protocol due to the obvious security issues (a mailto link on a website with an attachment reference to files in common locations, for instance). There was a security advisory for mailto attachments in Outlook 2003, for instance.

Take Two - Call MapiSendMail via PInvoke

MapiSendMail invokes the default e-mail client and supports attachments. There's a CodeProject article with C++ code, and the author supplied C# code to call MapiSendMail in the comments. It's reasonably straightforward, and it's easy to use. I'm going to skip over it, though, because it has one major problem - the call to MapiSendMail is modal. What that means is that your program hangs until the user edits and sends the e-mail. That's not at all what you'd expect as an end user - I'd want to be able to save the message as a draft and go on using the program.

Take Three - Call MapiSendMail Asynchronously

Andrew Baker's MAPI wrapper class does a great job of sending mail using the default e-mail client, and it does it asynchronously. The function you'll call, ShowDialog, just calls internal utility functions on a new thread and returns. It's very easy to use:

MapiMailMessage message = new MapiMailMessage(subject, body); message.Files.Add(file); message.ShowDialog();
Published Saturday, February 24, 2007 10:54 AM by Jon Galloway

Comments

# re: Sending files via the default e-mail client

Nice... Thanks for posting this...

Saturday, February 24, 2007 6:52 PM by Greg

# re: Sending files via the default e-mail client

Very useful information. Thanks

Sunday, March 04, 2007 11:21 AM by Markus

# re: Sending files via the default e-mail client

THe information was useful, but it does not appear to work in a web application.  I am still doing some testing with it, but so far when the application is deployed to the web server it will not call up the clients email application.

When running it locally it does fine.

Monday, March 05, 2007 7:56 PM by Harry

# re: Sending files via the default e-mail client

Works great!  But I have one additional requirements: place images in the email body.  I assume it would be done by using and <img_src> tag somehow.  But simply adding an <img_src=@c:\photo.jpg> doesn't work.  Any ideas on how adding images to the body might be accomplished?

Thursday, May 17, 2007 5:04 AM by Glenn Rogers

# re: Sending files via the default e-mail client

This is excellent, just what I've been looking for! I was originally sending the request by using outlook's command-line switches, but as soon as the requirement for supporting multiple clients came along, that went out the window!

Monday, May 21, 2007 2:49 PM by James Gregory

# re: Sending files via the default e-mail client

Hi Guys,

This is really good but i dont understand why Body was appended to To Fields.

For more Details.

//Source Code

MapiMailMessage message = new MapiMailMessage("Users Request:{Type your subject here}");

message.Recipients.Add(<recipient>,"Helpdesk",LaunchMailWithAttach.MapiMailMessage.RecipientType.To);

Output:

In To field i can see this.

<recipient><body>

<recipient>

Any idea?

Saturday, December 08, 2007 9:38 PM by Allan Paglinawan

# re: Sending files via the default e-mail client

hi, I tried the above and it does not seem to work in a web application i created in vs2003. can some one help??

Monday, February 04, 2008 2:33 AM by rose

# re: Sending files via the default e-mail client

hi... This is working excellent in local machine only...but when this application hosted in web server or IIS not working...

Tuesday, April 22, 2008 9:01 AM by Antony

# re: Sending files via the default e-mail client

Hi,

I want to open default mail client and want to send mail in Html format, and want to embed image also.

Saturday, August 09, 2008 1:35 AM by Sun Rays

# re: Sending files via the default e-mail client

Hi,

Thanks for solving my problem....

Saturday, October 11, 2008 9:52 PM by Dima

# re: Sending files via the default e-mail client

Only works with local machine but does not work when deployed to webserver.

Thanks

Shafiq

Tuesday, December 02, 2008 2:11 PM by Shafiq

# re: Sending files via the default e-mail client

i want to pass the from and url of the attachment as parameters of the function when i execute the exe. how should i do this

Wednesday, February 04, 2009 6:14 AM by KSR

# re: Sending files via the default e-mail client

I am using Lotus Notes 6. This code is working perfectly on Ms Outlook and Outlook Express but not in Lotus Notes. You need to do a small modifiation to make it work in Lotus Notes. This code is cool!

Saturday, April 18, 2009 10:48 PM by Allan Paglinawan

# re: Sending files via the default e-mail client

Hi,

this code was not working with IIS.

What changes do we need to do to work in IIS

Tuesday, April 21, 2009 5:50 AM by Ranga Rao

# re: Sending files via the default e-mail client

This cannot be done with a web application. Naturally it is not secure to be able to launch the e-mail client of anyone who visits your web page with an attachment from your server. It is not possible, sorry.

Tuesday, May 12, 2009 9:42 AM by Dan

# re: Sending files via the default e-mail client

Hey It brings me a confidence tht..this forum will solve my problem..

My Requirement is -

In mailto body attribute : I need a HTML body with images and a link inside it.

Any help..?

Wednesday, July 01, 2009 11:46 AM by Suman

Leave a Comment

(required) 
(required) 
(optional)
(required)