简体   繁体   English

调用Outlook电子邮件时C#asp.net超时

[英]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). 我的要求是,我需要开发一个Web应用程序(必须是Web应用程序)。
This app lists the outlook templates that has been saved in a specific directly. 该应用程序列出了直接保存在特定视图中的Outlook模板。
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. 每个模板旁边都有一个加载按钮,如果需要,您可以说一个加载链接。当代理(客户端)单击它时,Outlook应使用该模板打开。

I am trying to open the outlook from my asp.net application. 我正在尝试从asp.net应用程序打开Outlook。 when i click on my button, i execute this code 当我单击我的按钮时,我执行此代码

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

It is working on my machine, but when i deploy it on the server, i got this error: 它正在我的机器上运行,但是当我将其部署在服务器上时,出现此错误:

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

It seems a very general error, what could be the problem please? 似乎是一个非常普遍的错误,请问可能是什么问题? how can I diagnosis it? 我该如何诊断呢?

Note 注意

The body of the message in my case must be a html, not text. 在我的情况下,邮件的正文必须是html,而不是文本。 that is why i am not able to mailto like this 这就是为什么我不能像这样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, as I already wrote to you at Retrieving the COM class factory for component with CLSID error: 80070005 Access is denied Marco,正如我在检索COM类工厂中出现CLSID错误的组件时已经写给您的:80070005访问被拒绝

Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment. Microsoft当前不建议也不支持任何无人参与的非交互客户端应用程序或组件(包括ASP,ASP.NET,DCOM和NT Services)中的Microsoft Office应用程序自动化,因为Office可能表现出不稳定的行为和/在此环境中运行Office时出现死锁或死锁。

If you are building a solution that runs in a server-side context, you should try to use components that have been made safe for unattended execution. 如果要构建在服务器端上下文中运行的解决方案,则应尝试使用对无人值守执行安全的组件。 Or, you should try to find alternatives that allow at least part of the code to run client-side. 或者,您应该尝试找到允许至少部分代码在客户端运行的替代方法。 If you use an Office application from a server-side solution, the application will lack many of the necessary capabilities to run successfully. 如果您从服务器端解决方案中使用Office应用程序,则该应用程序将缺少许多成功运行所需的功能。 Additionally, you will be taking risks with the stability of your overall solution. 此外,您将承担整体解决方案稳定性的风险。

As a workaround you can use the low-level API - Extended MAPI. 解决方法是使用低级API-扩展MAPI。 Or any other third-party wrappers around that API (for example, Redemption). 或该API周围的任何其他第三方包装器(例如,赎回)。

I think you're falling into this problem: have you ever tried logging into your Pc as Marco, and then setting up Outlook with your account. 我认为您陷入了这个问题:您是否曾经尝试过以Marco身份登录到PC,然后使用您的帐户设置Outlook。 Then, if you log as a different user, Outlook must be properly configured with his email account. 然后,如果您以其他用户身份登录,则必须使用其电子邮件帐户正确配置Outlook。
Actually, when IIS launches you asp.net application, it is living into a "no user" session, and no set is available for Outlook. 实际上,当IIS启动您的asp.net应用程序时,它处于“无用户”会话状态,并且没有可用的Outlook设置。
Maybe, your debug program worked because it was launched into a normal "Marco" session. 也许,您的调试程序之所以起作用,是因为它已启动到正常的“ Marco”会话中。

Possible solution 可能的解决方案

  1. Create a real User (ie "Server Admin"), login with that user into the server, and configure Outlook 创建一个真实的用户 (即“服务器管理员”),用该用户登录服务器,并配置Outlook

  2. Open IIS, take the Application Pool of your application, chooes "Advanced settings" and then set "Application pool identity" to a "Custom account" . 打开IIS,选择应用程序的应用程序池,选择“高级设置”,然后将“应用程序池标识”设置为“自定义帐户”

I hope this is enough to make your program work. 我希望这足以使您的程序正常工作。

So you have a .msg file on the server, and you want to edit it and transmit it to users. 因此,您在服务器上有一个.msg文件,并且您想要对其进行编辑并将其传输给用户。

1) edit your template 1)编辑您的模板

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

there are many 3rd party tools that can help to edit a .msg files 有很多第三方工具可以帮助您编辑.msg文件
2. Send it to the user 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() 
}

The basic problem here is that you are trying to mix client and server logic in one application. 这里的基本问题是,您正在尝试在一个应用程序中混合使用客户端和服务器逻辑。 Opening an Outlook window from an ASP.NET application server side is a very bad idea - IIS application pools are not allowed to interact with a user desktop (starting from Windows Vista or Windows Server 2008 they run in Session 0 which does not have a visible desktop available). 从ASP.NET应用程序服务器端打开Outlook窗口是一个非常糟糕的主意-不允许IIS应用程序池与用户桌面进行交互(从Windows Vista或Windows Server 2008开始,它们在没有可见的会话0中运行桌面可用)。 So the Outlook window opens, but no one will ever see it. 这样会打开Outlook窗口,但没人能看到它。 I assume that if it's a modal window it blocks the parent thread causing the timeout, but I'm not sure as I haven't had a chance to check it. 我假设它是一个模态窗口,它将阻止导致超时的父线程,但是我不确定,因为我没有机会检查它。 On your development machine you have an IIS Express instance which runs in the same session as the current user and that's why you can see the Outlook window. 在开发计算机上,您有一个IIS Express实例,该实例与当前用户在同一会话中运行,这就是为什么您可以看到Outlook窗口的原因。 The request is probably hanging for the whole time you interact with the Outlook dialog. 与Outlook对话框进行交互的整个过程中,该请求可能会挂起。

I understand that you are under time pressure so the only recommendation I have for you for now is to install on your client machine IIS Express (you may get it from here: http://www.microsoft.com/en-us/download/details.aspx?id=34679 ) and run your application from it. 我了解您面临时间压力,因此,我目前对您的唯一建议是在客户端计算机IIS Express上安装(您可以从此处获取: http : //www.microsoft.com/zh-cn/download /details.aspx?id=34679 ),然后从中运行您的应用程序。 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 that shoudl open Outlook window with valid data in it. 但与此同时,请重写此逻辑-创建一个Web表单以允许用户撰写电子邮件,然后使用SmtpClient类为其发送电子邮件,或使用mailto url来打开其中包含有效数据的Outlook窗口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM