【问题标题】:Open new email window in Windows desktop e-mail client from C#从 C# 在 Windows 桌面电子邮件客户端中打开新的电子邮件窗口
【发布时间】:2017-04-13 17:14:52
【问题描述】:

我正在尝试使用下面从单独的 STA 线程调用的方法打开一个填充的电子邮件窗口。

    private void SendMailMessage(object ignore)
    {
        MAPIHelperInterop.MapiMessage message = new MAPIHelperInterop.MapiMessage();

        using(RecipientCollection.InteropRecipientCollection interopRecipients
            = fRecipientCollection.GetInteropRepresentation())
        {
            message.Subject = fSubject;
            message.NoteText = fBody;

            message.Recipients = interopRecipients.Handle;
            message.RecipientCount = fRecipientCollection.Count;

            // Check if we need to add attachments
            if(fFiles.Count > 0)
            {
                // Add attachments
                message.Files = AllocateFileAttachments(out message.FileCount);
            }

            // Signal the creating thread (make the remaining code async)
            fManualResetEvent.Set();

            int error = MAPIHelperInterop.MAPISendMailW(IntPtr.Zero, IntPtr.Zero, message, 0x8, 0);

            if(fFiles.Count > 0)
            {
                // Deallocate the files
                DeallocateFileAttachments(message);
            }

            // Check for error
            if(error != SUCCESS_SUCCESS)
            {
                LogMAPIError(error);
            }
        }
  }

我一直在使用 Outlook 对此进行测试,但我不断收到错误代码 2 (MAPI_E_FAILURE),并且 Outlook 中没有任何可见的事情发生(最终它应该适用于任何邮件客户端,但 Outlook 是主要用例,因此是一个可扩展的解决方案。对于 Outlook 将是一个很好的第一步)。只有当我以管理员身份启动 Outlook 或在运行代码时关闭 Outlook 时,它才有效。我尝试使用 Outlook 的句柄和不同的标志组合调用 MAPISendMailW,但这也不起作用。

我发现最接近我的问题的是https://social.msdn.microsoft.com/Forums/office/en-US/63e9f5b2-f5f2-4cf8-bdc2-ca1fad88ebe5/problem-with-outlook-and-mapisendmail-returns-mapiefailure-when-outlook-is-running?forum=outlookdev。为了遵循建议的解决方案,尝试在单独的 AppDomain 中运行 SendMailMessage 方法,如下所示:

     public void ShowDialog()
    {
        Evidence e = new Evidence();
        e.AddHostEvidence(new Zone(SecurityZone.MyComputer));           
        AppDomain appDomain = AppDomain.CreateDomain("Outlook Launcher", e);            
        appDomain.DoCallBack(SendMailMessage);                     
    }

如果我使用“SecurityZone.MyComputer”,那么我会收到与以前相同的错误,如果我使用任何其他 SecurityZone,我会收到以下错误消息:“Request for the permission of type 'System.Security.Permissions.ReflectionPermission, mscorlib,版本=4.0.0.0,文化=中性,PublicKeyToken=b77a5c561934e089' 失败。”也许这不是他们在上面的帖子中建议的,但这是我能想到的。

感谢您的帮助。

【问题讨论】:

    标签: c# .net email outlook mapi


    【解决方案1】:

    我过去使用过 MAPI,但要在不同的 Outlook 版本中正常工作可能会很痛苦。

    一个非常简单的解决方法是使用 mailto 链接。优点是它会自动与不同的邮件客户端一起工作。

     Process.Start("mailto:hello@test.com&subject=This is the subject&body=This is the body");
    

    这将打开您的默认邮件客户端,并填写主题和正文。

    【讨论】:

    • Outlook Redemption 还大大简化了 MAPI,但它是一个付费库 dimastr.com/redemption/RDOMail.htm
    • 我试过了,但它在我的电脑上不起作用,因为它只是打开了一个空的浏览器。我确信我可以通过更改某些设置在我的机器上工作,但如果它在我的计算机上失败,那么我相信它也会在其他用户机器上失败。所以不幸的是,这不是我的解决方案。
    • 是的,它将是控制面板\程序\默认程序\设置关联下的 MAILTO。很公平,我已经使用了很多 MAPI,当它运行良好时,它就很好。但是,我遇到了 MAPI 在重新启动 PC 之前无法工作的问题,以及它在一台机器上而不是另一台机器上工作的其他问题,因此最终不再使用它,因为它是一个支持噩梦。最后,我不需要用户能够编辑电子邮件,因此只需将其更改为使用 SMTP 直接发送电子邮件,这样更可靠。祝你好运!
    猜你喜欢
    • 1970-01-01
    • 2011-07-28
    • 2014-05-20
    • 2022-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多