简体   繁体   English

如何使用默认电子邮件客户端发送邮件?

[英]How to send email using default email client?

I want to send an email from a .net windows forms application using the system's default email client (thunderbird, outlook, etc.). 我想使用系统的默认电子邮件客户端(thunderbird,outlook等)从.net Windows窗体应用程序发送电子邮件。 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". 我想预设主题和正文 - 我认为有一种方法可以通过向Windows资源管理器发送这样的内容来实现:“mailto:test@example.invalid?subject = mysubject&body = mymessage”。 Do you have any examples on this? 你有这方面的例子吗?

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();

If you are working in a MS Windows environment only then you can use MAPI32.DLL. 如果您只在MS Windows环境中工作,则可以使用MAPI32.DLL。 A managed wrapper can be found here: 可以在此处找到托管包装器:

http://www.codeproject.com/KB/IP/SendFileToNET.aspx 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");

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 . 执行此操作的正确方法是使用MAPI,但使用interop代码到MAPI dll 实际上不是支持或建议的方法来执行此操作 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. 使用“mailto”方法有几个问题,其中最不重要的是你无法附加文件。

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" Win32Exception“传递给系统调用的数据区域太小”

So the question is still open since I need to handle long body text. 所以问题仍然存在,因为我需要处理长正文。 I don't know the size limit for this error. 我不知道这个错误的大小限制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM