views:

864

answers:

4

I want to send an email from a .net windows forms application using the system's default email client (thunderbird, outlook, etc.). I'd like to preset the subject and body text -- I think there's a way to do this by sending something like this to windows explorer: "mailto:[email protected]?subject=mysubject&body=mymessage". Do you have any examples on this?

A: 

This is what I tried:

Process.Start("mailto:[email protected]?subject=" +
    HttpUtility.HtmlAttributeEncode("Application error report") + 
    "&body=" + HttpUtility.HtmlAttributeEncode(memoEdit1.Text));

But if the body text is too large I get the exception:

Win32Exception "The data area passed to a system call is too small"

So the question is still open since I need to handle long body text. I don't know the size limit for this error.

P a u l
+3  A: 

Try this:

    System.Diagnostics.Process proc = new System.Diagnostics.Process();
    proc.StartInfo.FileName = "mailto:[email protected]?subject=hello&body=love my body";
    proc.Start();
Jay Riggs
A: 

If you are working in a MS Windows environment only then you can use MAPI32.DLL. A managed wrapper can be found here:

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");
Alex
+1  A: 

The correct way to do this is by using MAPI, but using interop code to the MAPI dll is not actually a supported nor recommended way to do this. I have done this and, as long as you are very careful about your interop code and don't do much more interaction than opening the mail client to send an email you should be OK.

There are several problems with using the "mailto" approach, least of which is that you can't attach files.

Scott Dorman