Sending files via the default e-mail client - Jon Galloway

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

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 5, 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, 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 4, 2008 2:33 AM by rose

# 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 9, 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 2, 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 4, 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 1, 2009 11:46 AM by Suman

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

namespace Adastra.FrodMgmt.CommonUI.BusinessModel

{

   public class MessageSentEventArgs

   {

       public static readonly MessageSentEventArgs Empty;

       public bool CancelPressed { get; set; }

       public MessageSentEventArgs(bool bCancelPressed)

       {

           this.CancelPressed = bCancelPressed;

       }

   }

   /// <summary>

   /// Provides functionality so Outlook can be used to generate Emails.  Use the

   /// write event to log the email.

   /// <example>

   /// Email outlookMsg = new Email();

   /// outlookMsg.Message.Subject = "Foo bar";

   /// outlookMsg.Show();

   /// </example>

   /// </summary>

   public class Email

   {

       #region | Enents |

       public delegate void MessageSentEventHandlerCallBack(object sender, MessageSentEventArgs e);

       public event MessageSentEventHandlerCallBack MessageSent;

       #endregion //| Enents |

       #region | Instance variables |

       /// <summary>

       /// Gets the current instance of Outlook

       /// </summary>

       private OutlookApp.Application      m_outlookInstance = new Microsoft.Office.Interop.Outlook.Application();

       public OutlookApp.MailItem          Message { get; set; }

       #endregion //| Instance variables |

       #region | Constructor stuff |

       /// <summary>

       /// Constructor, gets current outlook instance

       /// and creates a blank email message

       /// </summary>

       public Email()

       {

           Initialize();

       }

       /// <summary>

       /// Initialize object

       /// </summary>

       private void Initialize()

       {

           //### Create a blank email

           this.Message = (OutlookApp.MailItem)m_outlookInstance.CreateItem(OutlookApp.OlItemType.olMailItem);

           //### Wire up the write event for logging

           this.Message.Write += new Microsoft.Office.Interop.Outlook.ItemEvents_10_WriteEventHandler(Message_Write);

       }

       #endregion //| Constructor stuff |

       #region | Email interface |

       /// <summary>

       /// Create email message and open MS Outlook pane

       /// </summary>

       /// <param name="strAddressTo"> address TO send </param>

       /// <param name="strAddressCC"> address carbon copy to send </param>

       /// <param name="bIsHtml"> flag if mail body is in html format</param>

       /// <param name="bIsImportanceHigh"> flag if mail ha importance high</param>

       /// <param name="strMessageBody"> message body</param>

       public void Send(string strAddressTo,

                        string strAddressCC,

                        string strSubject,

                        bool bIsHtml,

                        bool bIsImportanceHigh,

                        string strMessageBody,

                        MessageSentEventHandlerCallBack messageSentEventHandler)

       {

           if (null != messageSentEventHandler)

           {

               this.MessageSent += new MessageSentEventHandlerCallBack(messageSentEventHandler);

           }

           this.Message.To = strAddressTo;

           this.Message.CC = strAddressCC;

           this.Message.Subject = strSubject;

           this.IsHtml = bIsHtml;

           this.IsImportanceHigh = bIsImportanceHigh;

           if(bIsHtml)

           {

               this.Message.HTMLBody = strMessageBody;

           }

           else

           {

               this.Message.Body = strMessageBody;

           }

           this.Show();

       }

       /// <summary>

       /// Get/Set flag if email body is HTML

       /// </summary>

       public bool IsHtml

       {

           get { return (Microsoft.Office.Interop.Outlook.OlBodyFormat.olFormatHTML == this.Message.BodyFormat); }

           set { this.Message.BodyFormat = Microsoft.Office.Interop.Outlook.OlBodyFormat.olFormatHTML; }

       }

       /// <summary>

       /// Get/Set flag if email importance is high

       /// </summary>

       public bool IsImportanceHigh

       {

           get { return (Microsoft.Office.Interop.Outlook.OlImportance.olImportanceHigh == this.Message.Importance); }

           set { this.Message.Importance = Microsoft.Office.Interop.Outlook.OlImportance.olImportanceHigh; }

       }

       /// <summary>

       ///  Used for logging after the end user presses the send

       ///  button in Outlook.  If you need to log the email that was

       ///  sent to a web service or something else, fill this in.  This is

       ///  called after the email is sent via Outlook.

       /// </summary>

       /// <param name="Cancel"></param>

       private void Message_Write(ref bool bCancel)

       {

           if (null != this.MessageSent)

           {

               this.MessageSent(this, new MessageSentEventArgs(bCancel));

           }

       }

       /// <summary>

       /// Displays the outlook screen and shows the email message.

       /// </summary>

       public void Show()

       {

           this.Message.Display(false);

       }

       #endregion //| Email interface |

   }

}

Wednesday, September 30, 2009 11:11 AM by dasi

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

It's not work with outlook 2003 2007

Thursday, February 4, 2010 3:06 PM by Halil

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

What should I do to modify it to work on Lotus Notes?

Monday, May 10, 2010 9:34 PM by Vin

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

Please let me know how this should be applied for Web applications. I have a task of sending a pdf file from server to attach it to a mail and send it with Subject Message/Body and Signature. Please Provide me with whole code. Thanks....

Pradeep

Wednesday, July 28, 2010 1:37 PM by m.pradeep5259

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

its not working :D

Wednesday, February 9, 2011 7:41 AM by mina

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

Good morning, is there an option in HTML mailto command that allows you to insert the default signature set in OLK?

If I publish a mailto link in a web page I can send in the body message only the text I have set in body="...." command.

Thank you.

Friday, August 26, 2011 6:39 AM by Alessandro

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

What I want to do is different from those of others.I have a surprise for you.Mother doesn't make up.They are arguing over who should pay the bill.I have something to tell you.On being introduced to somebody, a British person often shakes hands.On being introduced to somebody, a British person often shakes hands.Neither you nor he is wrong.That's the latest fashion.I borrowed a notebook from Tom and I lent it to Marry.

Monday, December 3, 2012 2:45 AM by http://canadagooseparkachilliwack.webs.com