从 C# 启动电子邮件应用程序 (MAPI)(带附件)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/784997/
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
Launching email application (MAPI) from C# (with attachment)
提问by Steven
In the past I have used MAPISendMail to launch Outlook (or whatever the desired MAPI email application was) from a C++ application with a file attachment. (Similar to say Microsoft Word's Send Email functionality).
过去,我使用 MAPISendMail 从带有文件附件的 C++ 应用程序启动 Outlook(或任何所需的 MAPI 电子邮件应用程序)。(类似于 Microsoft Word 的发送电子邮件功能)。
I need to do the equivalent from a C# application and to have it work when running on XP, Vista, Server 2008 (and Windows 7 I suppose).
我需要从 C# 应用程序中执行等效操作,并使其在 XP、Vista、Server 2008(和我想的 Windows 7)上运行时正常工作。
MAPISendMail is a no go under Vista/2008 as it always returns MAPI_ E_FAILURE when Outlook is running and MAPI is not supported in managed code. Even after checking this fix: http://support.microsoft.com/kb/939718I can't get it to reliably work.
MAPISendMail 不适用于 Vista/2008,因为它在 Outlook 运行且托管代码不支持 MAPI 时总是返回 MAPI_E_FAILURE。即使在检查此修复程序之后:http: //support.microsoft.com/kb/939718我也无让它可靠地工作。
I know that Microsoft Word & Adobe Reader 9 can both launch Outlook with an attachment under Vista.
我知道 Microsoft Word 和 Adobe Reader 9 都可以在 Vista 下启动带有附件的 Outlook。
A C# compatible solution would be preferred but I'd be happy with anything that works (doesn't have to use MAPI). I can't seem to find what the current "solution" is. None of the existing answers on Stack Overflow seem to cover this either.
AC# 兼容的解决方案将是首选,但我对任何有效的方都感到满意(不必使用 MAPI)。我似乎无找到当前的“解决方案”是什么。Stack Overflow 上的现有答案似乎也没有涵盖这一点。
Edit:
编辑:
I am aware MAPI and C# do not work together, so I will take a C/C++ solution that works in Vista and Server 2008 when NOTrunning as administrator. See Adobe Reader 9 & Microsoft Word as examples that work.
我知道 MAPI 和 C# 不能一起工作,所以我将采用在 Vista 和 Server 2008 中不以管理员身份运行的 C/C++ 解决方案。请参阅 Adobe Reader 9 和 Microsoft Word 作为工作示例。
采纳答案by AnotherDan
At work we have successfully done this using VSTO.
在工作中,我们使用 VSTO 成功地做到了这一点。
Here is a snippet of some lines we have running on VISTA with Outlook 2007: (the code is in VB.net).
这是我们在带有 Outlook 2007 的 VISTA 上运行的一些行的片段:(代码在 VB.net 中)。
Note that the usage is security locked when doing certain things to the outlook object. (to address, body and other properties marked as security risks). We use a 3rd party component (Redemption) to go around this security. If you dont use a security manager of some sort, outlook will give a little popup that something outside is trying to access it and you can give it access in a period of time.
请注意,在对 Outlook 对象执行某些操作时,该用是安全锁定的。(地址、正文和其他属性标记为安全风险)。我们使用第 3 方组件(Redemption)来解决此安全问题。如果您不使用某种安全管理器,Outlook 会弹出一个小弹出窗口,提示外部正在尝试访问它,您可以在一段时间内授予它访问权限。
The import of the Outlook interface.
Outlook 界面的导入。
Imports Outlook = Microsoft.Office.Interop.Outlook
This example is to give you some direction, not a full working example.
这个例子是给你一些指导,而不是一个完整的工作例子。
dim MailItem As Microsoft.Office.Interop.Outlook.MailItem
' Lets initialize outlook object '
MailItem = OutlookSession.Application.CreateItem(Outlook.OlItemType.olMailItem)
MailItem.To = mailto
MailItem.Subject = communication.Subject
MailItem.BodyFormat = Outlook.OlBodyFormat.olFormatHTML
MailItem.HTMLBody = htmlBody
MailItem.Attachments.Add(filename, Outlook.OlAttachmentType.olByValue)
' If True is supplied to Display it will act as modal and is executed sequential. '
SafeMail.Display(True)
The OutlookSession in the above example is coming from this Property:
上面例子中的 OutlookSession 来自这个属性:
Public ReadOnly Property OutlookSession() As Outlook.NameSpace
Get
If Not OutlookApplication Is Nothing Then
Return OutlookApplication.GetNamespace ("MAPI")
Else
Return Nothing
End If
End Get
End Property
As you can see it is using MAPI inside for this.
正如您所看到的,它在内部使用 MAPI。
Good luck with it.
祝你好运。
回答by Robert MacLean
Bit lowtech method, but using the mailto handler you can do this
有点lowtech方,但使用mailto处理程序可以做到这一点
System.Diagnostics.Process.Start("mailto:[email protected]?subject=hello&attachment=c:\chicken.xls");
Note: As pointed out this may not work on all clients as it is not part of the mailto URL spec. Most importantly (in my world at least) is Outlook 2007 does not support it, while older versions did.
注意:正如所指出的,这可能不适用于所有客户端,因为它不是mailto URL 规范的一部分。最重要的是(至少在我的世界中)是 Outlook 2007 不支持它,而旧版本支持。
回答by PeteT
I'm not sure if you need the email to open in outlook or if you just want to send an email with an attachment from c#. I know you wrote open in outlook but you may be assuming this is the only way to do it. If you just want to send an email with an attachment it can be done something like below.
我不确定您是否需要在 Outlook 中打开电子邮件,或者您是否只想从 c# 发送带有附件的电子邮件。我知道你在 Outlook 中写了 open 但你可能认为这是唯一的方。如果您只想发送带有附件的电子邮件,可以执行如下操作。
#using System.Net.Mail;
SmtpClient smtpClient = new SmtpClient(host, port);
MailMessage message = new MailMessage(from, to, subject, body);
Attachment attachment = new Attachment(@"H:\attachment.jpg");
message.Attachments.Add(attachment);
System.Net.NetworkCredential SMTPUserInfo = new System.Net.NetworkCredential(username, password);
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = SMTPUserInfo;
smtpClient.Send(message);
You can also do it without the authentication bit depending on your email server.
您也可以在没有身份验证位的情况下执行此操作,具体取决于您的电子邮件服务器。
回答by Sanjay10
C# code to send email through Outlook; no security warnings occur.
通过 Outlook 发送电子邮件的 C# 代码;没有安全警告发生。
var outlook = new ApplicationClass();
MailItem mailItem = (MailItem)outlook.Session.Application.CreateItem(Outlook.OlItemType.olMailItem);
mailItem.Display(false);
回答by Sanjay10
You don't really need redemption for VB as suggested above as long as you are simply setting properties in an e-mail and not reading them. Here is a simple VB function to show / send an e-mail via outlook with an attachment. (This code references the Microsoft Outlook 12.0 Object Library e.g. "C:\Program Files\Microsoft Office\Office12\MSOUTL.OLB").
只要您只是在电子邮件中设置属性而不是阅读它们,您就真的不需要像上面建议的那样兑换 VB。这是一个简单的 VB 函数,用于通过 Outlook 显示/发送带有附件的电子邮件。(此代码引用 Microsoft Outlook 12.0 对象库,例如“C:\Program Files\Microsoft Office\Office12\MSOUTL.OLB”)。
Sub DoMail()
Set objOL = CreateObject("Outlook.Application")
Set objNewMail = objOL.CreateItem(olMailItem)
Dim filename As String
filename = "C:\temp\example.txt"
With objNewMail
.To = "cjoy@spam_me_not.com"
.Subject = "test"
.Body = "Test Body"
.Attachments.Add filename, Outlook.OlAttachmentType.olByValue
End With
objNewMail.Display
'objNewMail.Send
End Sub