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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/809538/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 12:36:49  来源:igfitidea点击:

How to send email using default email client?

.netwinformsemail

提问by P a u l

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?

我想使用系统的默认电子邮件客户端(thunderbird、outlook 等)从 .net windows 窗体应用程序发送电子邮件。我想预设主题和正文文本——我认为有一种方法可以通过向 Windows 资源管理器发送这样的内容来做到这一点:“mailto:[email protected]?subject=mysubject&body=mymessage”。你有这方面的例子吗?

回答by Jay Riggs

Try this:

尝试这个:

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

回答by Alex

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

如果您只在 MS Windows 环境中工作,那么您可以使用 MAPI32.DLL。可以在此处找到托管包装器:

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

回答by Scott Dorman

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 shouldbe OK.

执行此操作的正确方法是使用 MAPI,但对 MAPI dll 使用互操作代码实际上不是支持也不推荐的方法。我已经这样做了,只要你非常小心你的互操作代码并且不做比打开邮件客户端发送电子邮件更多的交互,你应该没问题

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

使用“mailto”方法有几个问题,其中最不重要的是您不能附加文件。

回答by P a u l

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"

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.

所以问题仍然存在,因为我需要处理长正文。我不知道此错误的大小限制。