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

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

问题描述

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



我需要从C#应用程序中执行相当的操作,并在XP,Vista,Server 2008上运行时工作(和Windows 7,我想)。



MAPISendMail是Vista / 2008中没有,因为它总是返回MAPI_ E_FAILURE,当Outlook运行和MAPI不支持托管代码。
即使在检查此修复程序后:
http://support.microsoft.com/kb / 939718
我不能让它可靠地工作。



我知道Microsoft Word& Adobe Reader 9可以在Vista下使用附件启动Outlook。



AC#兼容解决方案将是首选,但我会对任何事情感到满意这是有用的(不必使用MAPI)。我似乎找不到目前的解决方案。



编辑: / p>

我知道MAPI和C#不能一起工作,所以在时,我将采用适用于Vista和Server 2008的C / C ++解决方案作为管理员运行。见Adobe Reader 9& Microsoft Word作为例子工作。

解决方案

在工作中,我们已经使用VSTO成功完成了。



以下是我们使用Outlook 2007在VISTA上运行的一些行代码片段(代码位于VB.net中)。



请注意,当对outlook对象执行某些操作时,使用情况是安全锁定的。 (以地址,身体和其他属性标示为安全隐患)。我们使用第三方组件(赎回)来解决这种安全问题。如果您不使用某种安全管理员,展望将给予一点弹出,外面的东西正在尝试访问它,您可以在一段时间内给予访问。



导入Outlook界面。

 导入Outlook = Microsoft.Office.Interop.Outlook 

这个例子是给你一些方向,而不是一个完整的例子。

  dim MailItem As Microsoft.Office.Interop.Outlook.MailItem 

'让初始化outlook对象'
MailItem = OutlookSession.Application.CreateItem(Outlook .OlItemType.olMailItem)
MailItem.To = mailto

MailItem.Subject = communications.Subject
MailItem.BodyFormat = Outlook.OlBodyFormat.olFormatHTML
MailItem.HTMLBody = htmlBody

MailItem.Attachments.Add(filename,Outlook.OlAttachmentType.olByValue)

'如果True被提供给Display,它将作为模态并执行顺序。 '
SafeMail.Display(True)

上述示例中的OutlookSession来自于属性:

  Public ReadOnly属性OutlookSession()作为Outlook.NameSpace 
获取
如果不是OutlookApplication不是然后
返回OutlookApplication.GetNamespace(MAPI)
Else
返回没有
结束如果
结束获取
结束属性

正如你所见,它正在使用MAPI。



好运气好。


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

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

I know that Microsoft Word & Adobe Reader 9 can both launch Outlook with an attachment under Vista.

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.

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

解决方案

At work we have successfully done this using VSTO.

Here is a snippet of some lines we have running on VISTA with Outlook 2007: (the code is in 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.

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)

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

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

Good luck with it.

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

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