VB.Net Outlook电子邮件创建在本地工作,在服务器上失败

时间:2014-10-08 16:12:00

标签: asp.net vb.net outlook

当我在本地计算机上运行项目时,它会按预期生成Outlook电子邮件,但如果我将所有代码上传到我们的开发服务器,则运行时会出现此错误消息:[例外:无法创建ActiveX组件。] Microsoft.VisualBasic.Interaction.CreateObject(String ProgId,String ServerName)+509721 等...

我知道这是Outlook的失败,因为如果我评论sendmail()函数,页面的其余部分工作正常。 (它只是不创建电子邮件)。 本地计算机是安装了Outlook的Windows 7,服务器是2008R2,没有安装Office。我有其他页面可以写入Excel文件,但他们使用CrystalReports来处理它,所以我不确定服务器是否需要注册Outlook dll,或者是否还需要其他东西。

带有VB.Net代码隐藏的Aspx页面。我的创建电子邮件如下:

    Dim OApp2 As Object, OMail2 As Object, signature2 As String
    OApp2 = CreateObject("Outlook.Application")
    OMail2 = OApp2.CreateItem(0)

    Dim alAttachList As New ArrayList(2)
    Dim iCounter As Integer

    alAttachList.Insert(0, "E:\Test\DEBUG CODE.txt")
    alAttachList.Insert(1, "\\RemoteServer\z\Test\Hello.bmp")

    sBody += "<br />" + "Attached are some files." + "<br />" + "They can also be found in: X:\Test\Test\Name  "
    With OMail2
        .Display()
    End With
    signature2 = OMail2.HTMLBody
    With OMail2
        .Subject = sSubject
        .To = sTo
        .CC = sCC
        .HTMLbody = sBody & "<br /><br />" & signature2

    End With

    Dim sBodyLen As Integer = Int(sBody.Length)
    Dim oAttachs2 As Interop.Outlook.Attachments = OMail2.Attachments
    Dim oAttach2 As Interop.Outlook.Attachment
    For iCounter = 0 To alAttachList.Count - 1
        oAttach2 = oAttachs2.Add(Source:=alAttachList(iCounter), DisplayName:=alAttachList(iCounter))
    Next

    OMail2.Display(True)

    OApp2 = Nothing
    OMail2 = Nothing 

2 个答案:

答案 0 :(得分:1)

当您执行以下操作时:

OApp2 = CreateObject("Outlook.Application")

您正在使用随MS Office一起安装的Office.Interop库(如果您走这条路线,则使用各个应用程序)

您必须在服务器上安装MS Office或至少Outlook才能使用Office.Interop。您可能会考虑使用MAPI而不是互操作来发送电子邮件。 MAPI是.Net的一部分,不需要在您的服务器上安装其他程序。

答案 1 :(得分:0)

首先,必须安装Outlook。其次,您的代码是在服务(例如IIS)中运行的吗?不能从服务中使用Office应用程序(包括Outlook)。

相关问题