从 C# 启动电子邮件应用程序 (MAPI)(带附件)

发布于 2024-07-18 02:46:58 字数 861 浏览 6 评论 0 原文

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

我需要从 C# 应用程序执行相同的操作,并使其在 XP、Vista、Server 2008(我想还有 Windows 7)上运行时正常工作。

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

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

AC# 兼容解决方案将是首选,但我会对任何可行的解决方案感到满意(不必使用 MAPI)。 我似乎找不到当前的“解决方案”是什么。 Stack Overflow 上的现有答案似乎都没有涵盖这一点。

编辑:

我知道 MAPI 和 C# 不能一起工作,所以我将采取当以管理员身份运行时,C/C++ 解决方案可在 Vista 和 Server 2008 中运行。 请参阅 Adob​​e Reader 9 和 Adob​​e Reader 9。 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).

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

明媚如初 2024-07-25 02:46:58

在工作中我们已经使用 VSTO 成功地完成了这一任务。

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

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

Outlook界面的导入。

Imports Outlook = Microsoft.Office.Interop.Outlook

此示例旨在为您提供一些指导,而不是完整的工作示例。

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 来自此属性:

    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 来实现此目的。

祝你好运。

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.

对你而言 2024-07-25 02:46:58

只要您只是在电子邮件中设置属性而不是阅读它们,您实际上并不需要像上面建议的那样兑换 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

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

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
浅唱ヾ落雨殇 2024-07-25 02:46:58

技术含量较低的方法,但使用 mailto 处理程序,您可以执行此操作

System.Diagnostics.Process.Start("mailto:[email protected]?subject=hello&attachment=c:\\chicken.xls");

注意:正如所指出的,这可能不适用于所有客户端,因为它不是 mailto URL 规范。 最重要的是(至少在我看来)Outlook 2007 不支持它,而旧版本则支持。

Bit lowtech method, but using the mailto handler you can do this

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.

岁月苍老的讽刺 2024-07-25 02:46:58

我不确定您是否需要在 Outlook 中打开电子邮件,或者您是否只想从 C# 发送带有附件的电子邮件。 我知道您在 Outlook 中打开,但您可能认为这是唯一的方法。 如果您只想发送带有附件的电子邮件,可以执行如下操作。

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

您也可以在没有身份验证位的情况下执行此操作,具体取决于您的电子邮件服务器。

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.

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

℡寂寞咖啡 2024-07-25 02:46:58

通过 Outlook 发送电子邮件的 C# 代码; 不会出现安全警告。

var outlook = new ApplicationClass();
MailItem mailItem = (MailItem)outlook.Session.Application.CreateItem(Outlook.OlItemType.olMailItem);

mailItem.Display(false);

C# code to send email through Outlook; no security warnings occur.

var outlook = new ApplicationClass();
MailItem mailItem = (MailItem)outlook.Session.Application.CreateItem(Outlook.OlItemType.olMailItem);

mailItem.Display(false);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文