【发布时间】: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