How to send email using default email client?

2019-01-07 19:41发布

问题:

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:test@example.invalid?subject=mysubject&body=mymessage". Do you have any examples on this?

回答1:

Try this:

    System.Diagnostics.Process proc = new System.Diagnostics.Process();
    proc.StartInfo.FileName = "mailto:someone@somewhere.com?subject=hello&body=love my body";
    proc.Start();


回答2:

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.



回答3:

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("person1@somewhere.com");
mapi.AddRecipientTo("person2@somewhere.com");
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");


回答4:

This is what I tried:

Process.Start("mailto:demo@example.invalid?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.