2016-11-27 75 views
0

我已经在ASP.NET中使用VB创建了一个应用程序。我写了下面的代码。尝试使用ASP.NET打开Outlook

它给本地主机上的错误,但当我从我的应用程序运行它,它工作正常。

我检查了DCOMCNFG - > Outlook/Office条目不存在。

请指教我如何打开Outlook与我的应用程序在本地主机/服务器附件?

ByVal recipients As List(Of String), 
    ByVal smtpAddress As String, 
    ByVal attachments As List(Of String)) 

    ' Create a new MailItem and set the To, Subject, and Body properties. 
    'Dim application As New Outlook.Application 
    Dim application As New Outlook.Application() 
    Threading.Thread.Sleep(2000) 

    Dim newMail As Outlook.MailItem = DirectCast(application.CreateItem(Outlook.OlItemType.olMailItem), Outlook.MailItem) 

    ' Set up all the recipients. 
    For Each recipient In recipients 
     newMail.Recipients.Add(recipient) 
    Next 

    If newMail.Recipients.ResolveAll() Then 
     newMail.Subject = subject 
     newMail.Body = body 
     For Each attachment As String In attachments 
      newMail.Attachments.Add(attachment, Outlook.OlAttachmentType.olByValue) 
     Next 
    End If 

    newMail.Display(True) 
+1

如果您希望在客户端计算机上调用Outlook,则无法从服务器上的代码执行此操作。试想一下,如果任何网站可以在查看它的计算机上运行任何程序。如果您希望在Web服务器上调用Outlook,那么您的运气再好不过了,因为Office应用程序无法可靠地以这种方式使用:它们被设计为具有交互式UI,而Web服务器没有这种交互式UI。但是,您可以从服务器发送电子邮件; [System.Net.Mail](http://www.systemnetmail.com/default.aspx)网站涵盖了该主题。 –

回答

0

您需要使用任何可以在不涉及Outlook的情况下处理.pst文件的第三方组件。

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

如果您正在构建一个在服务器端上下文中运行的解决方案,那么您应该尝试使用对于无人执行安全的组件。或者,您应该尝试找到允许至少部分代码运行客户端的替代方案。如果您从服务器端解决方案使用Office应用程序,则该应用程序将缺少成功运行所需的许多必要功能。此外,您将面临整体解决方案稳定性的风险。请阅读Considerations for server-side Automation of Office文章中的更多内容。