从 C# 启动电子邮件应用程序 (MAPI)(带附件) [英] Launching email application (MAPI) from C# (with attachment)

查看:61
本文介绍了从 C# 启动电子邮件应用程序 (MAPI)(带附件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过去,我使用 MAPISendMail 从带有文件附件的 C++ 应用程序启动 Outlook(或任何所需的 MAPI 电子邮件应用程序).(类似于 Microsoft Word 的发送电子邮件功能).

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).

我需要从 C# 应用程序中执行等效操作,并让它在 XP、Vista、Server 2008(我想还有 Windows 7)上运行.

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).

MAPISendMail 不适用于 Vista/2008,因为它在 Outlook 运行且托管代码不支持 MAPI 时总是返回 MAPI_E_FAILURE.即使在检查此修复程序之后:http://support.microsoft.com/kb/939718我无法让它可靠地工作.

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/939718 I can't get it to reliably work.

我知道 Microsoft Word &Adobe Reader 9 都可以在 Vista 下启动带有附件的 Outlook.

首选与 C# 兼容的解决方案,但我对任何可行的解决方案都感到满意(不必使用 MAPI).我似乎无法找到当前的解决方案"是什么.Stack Overflow 上的现有答案似乎也没有涵盖这一点.

我知道 MAPI 和 C# 不能一起工作,所以我将采用一个 C/C++ 解决方案,当以管理员身份运行时,它可以在 Vista 和 Server 2008 中运行.参见 Adob​​e Reader 9 &Microsoft Word 作为有效示例.

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 NOT running as administrator. See Adobe Reader 9 & Microsoft Word as examples that work.

推荐答案

在工作中,我们使用 VSTO 成功地做到了这一点.

At work we have successfully done this using VSTO.

以下是我们在带有 Outlook 2007 的 VISTA 上运行的一些代码段:(代码在 VB.net 中).

Here is a snippet of some lines we have running on VISTA with Outlook 2007: (the code is in VB.net).

请注意,在对 Outlook 对象执行某些操作时,该用法是安全锁定的.(地址、正文和其他属性标记为安全风险).我们使用第 3 方组件(Redemption)来解决此安全问题.如果您不使用某种安全管理器,Outlook 会弹出一个小弹出窗口,提示外部正在尝试访问它,您可以在一段时间内授予它访问权限.

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 界面的导入.

The import of the Outlook interface.

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)

上面例子中的 OutlookSession 来自这个属性:

The OutlookSession in the above example is coming from this Property:

    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

正如你所看到的,它在内部使用了 MAPI.

As you can see it is using MAPI inside for this.

祝你好运.

这篇关于从 C# 启动电子邮件应用程序 (MAPI)(带附件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆