C# MailTo with Attachment?

Goober picture Goober · Jul 28, 2009 · Viewed 76.3k times · Source

Currently I am using the below method to open the users outlook email account and populate an email with the relevant content for sending:

public void SendSupportEmail(string emailAddress, string subject, string body)
{
   Process.Start("mailto:" + emailAddress + "?subject=" + subject + "&body=" 
                + body);
}

I want to however, be able to populate the email with an attached file.

something like:

public void SendSupportEmail(string emailAddress, string subject, string body)
{
   Process.Start("mailto:" + emailAddress + "?subject=" + subject + "&body=" 
      + body + "&Attach="
      + @"C:\Documents and Settings\Administrator\Desktop\stuff.txt");
}

However this does not seem to work. Does anyone know of a way which will allow this to work!?

Help greatly appreciate.

Regards.

Answer

Alex picture Alex · Aug 29, 2009

If you want to access the default email client then you can use MAPI32.dll (works on Windows OS only). Take a look at the following wrapper:

http://www.codeproject.com/KB/IP/SendFileToNET.aspx

Code looks like this:

MAPI mapi = new MAPI();
mapi.AddAttachment("c:\\temp\\file1.txt");
mapi.AddAttachment("c:\\temp\\file2.txt");
mapi.AddRecipientTo("[email protected]");
mapi.AddRecipientTo("[email protected]");
mapi.SendMailPopup("testing", "body text");

// Or if you want try and do a direct send without displaying the mail dialog
// mapi.SendMailDirect("testing", "body text");