c# asp.net 调用 outlook 邮件时超时

c# asp.net timeout when calling a outlook email

My requirement is that i need to develop a web app , (it is mandatory to be a web app).
This app lists the outlook templates that has been saved in a specific directly.
Beside each template, there is a load button, you can say a load link if you want When the agent (client) clicks it, the outlook should open with that template.

我正在尝试从我的 asp.net 应用程序打开 Outlook。当我点击我的按钮时,我执行这段代码

Microsoft.Office.Interop.Outlook._MailItem oMailItem2 = oApp.CreateItemFromTemplate(templateFilePath);
oMailItem2.Display(true);

它在我的机器上工作,但是当我将它部署到服务器上时,我收到了这个错误:

Request timed out.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Web.HttpException: Request timed out.

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace: 


[HttpException (0x80004005): Request timed out.]

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.17929

这似乎是一个非常普遍的错误,请问可能是什么问题?我该如何诊断?

备注

我的消息正文必须是 html,而不是文本。这就是为什么我不能像这样mailto

<a href="mailto:someone@example.com?subject=This%20is%20the%20subject&cc=someone_else@example.com&body=This%20is%20the%20body">Send email</a>

Marco,我已经在 Retrieving the COM class factory for component with CLSID error: 80070005 Access is denied

给你写信了

Microsoft 目前不推荐也不支持从任何无人值守的非交互式客户端应用程序或组件(包括 ASP、ASP.NET 自动化 Microsoft Office 应用程序、DCOM 和 NT 服务),因为当 Office 在此环境中为 运行 时,Office 可能表现出不稳定的行为 and/or 死锁。

如果您要在服务器端上下文中构建 运行 的解决方案,您应该尝试使用已针对无人值守执行安全设置的组件。或者,您应该尝试找到至少允许 运行 客户端部分代码的替代方案。如果您从服务器端解决方案使用 Office 应用程序,该应用程序将缺少许多 运行 成功所必需的功能。此外,您将承担整体解决方案稳定性的风险。

作为变通方法,您可以使用低级 API - 扩展 MAPI。或围绕 API 的任何其他第三方包装器(例如,Redemption)。

我认为您遇到了这个问题:您是否尝试过以 Marco 身份登录您的 PC,然后使用您的帐户设置 Outlook。 然后,如果您以其他用户身份登录,则必须使用他的电子邮件帐户正确配置 Outlook。
实际上,当 IIS 启动您的 asp.net 应用程序时,它正在进入一个 "no user" 会话,并且没有可用于 Outlook 的设置。
也许,您的调试程序之所以有效,是因为它是在正常的 "Marco" 会话中启动的。

可能的解决方案

  1. 创建真实用户(即"Server Admin"),用该用户登录 服务器,配置 Outlook

  2. 打开 IIS,获取应用程序的应用程序池,选择 "Advanced settings",然后 将 "Application pool identity" 设置为 "Custom account".

我希望这足以使您的程序运行。

这里的基本问题是您试图在一个应用程序中混合客户端和服务器逻辑。从 ASP.NET 应用程序服务器端打开 Outlook window 是一个非常糟糕的主意 - IIS 应用程序池不允许与用户桌面交互(从 Windows Vista 或 Windows Server 2008 他们 运行 在会话 0 中没有可用的可见桌面)。因此 Outlook window 打开,但没有人会看到它。我假设如果它是模态 window 它会阻塞导致超时的父线程,但我不确定,因为我还没有机会检查它。在您的开发机器上,您有一个 IIS Express 实例,它 运行 与当前用户在同一会话中,这就是为什么您可以看到 Outlook window。在您与 Outlook 对话框交互的整个过程中,请求可能一直处于挂起状态。

我知道您时间紧迫,所以目前我唯一的建议是在您的客户端计算机上安装 IIS Express(您可以从这里获取:http://www.microsoft.com/en-us/download/details.aspx?id=34679) and run your application from it. BUT in the meantime rewrite this logic - either create a web form to allow the user to compose an email and then use SmtpClient class to send it for him or use mailto url 应该打开 Outlook window里面有有效数据。

所以您在服务器上有一个 .msg 文件,您想要编辑它并将其传输给用户。

1) 编辑您的模板

https://www.google.it/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=c%23+edit+.msg+file

有许多第 3 方工具可以帮助编辑 .msg 文件
2。发送给用户

using (FileStream fs = File.OpenRead(path)) 
{ 
int length = (int)fs.Length; 
byte[] buffer; 

using (BinaryReader br = new BinaryReader(fs)) 
{ 
buffer = br.ReadBytes(length); 
} 

Response.Clear(); 
Response.Buffer = true; 
Response.AddHeader("content-disposition", String.Format("attachment;filename={0}", Path.GetFileName(path))); 
Response.ContentType = "application/" + Path.GetExtension(path).Substring(1); 
Response.BinaryWrite(buffer); 
Response.End() 
}