通过 Visual Basic 错误访问 Outlook

Acces Outlook via Visual Basic error

我正在尝试创建一个程序,从 Outlook(2007 桌面版)收件箱中检索所有电子邮件并将它们放入 DataGridView 中。

代码:

Imports Outlook = Microsoft.Office.Interop.Outlook

Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim dt As DataTable
    Try
        Dim app As Outlook.Application = New Outlook.Application()
        Dim ns As Outlook.[NameSpace] = app.GetNamespace("MAPI")
        Dim inbox As Outlook.MAPIFolder = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)
        ns.SendAndReceive(True)
        dt = New DataTable("Inbox")
        dt.Columns.Add("Subject", GetType(String))
        dt.Columns.Add("Sender", GetType(String))
        dt.Columns.Add("Body", GetType(String))
        dt.Columns.Add("Date", GetType(String))
        DataGridView1.DataSource = dt
        For Each item As Object In inbox.Items
            If TypeOf item Is Outlook.MailItem Then
                Dim item1 As Outlook.MailItem = CType(item, Outlook.MailItem)
                dt.Rows.Add(New Object() {item1.Subject, item1.Sender, item1.HTMLBody, item1.SentOn.ToLongDateString() & "" + item1.SentOn.ToLongTimeString()})
            End If
        Next
        MessageBox.Show("done")
    Catch ex As Exception
        MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.[Error])
    End Try
End Sub
End Class

当我尝试构建项目时出现以下错误:

System.Runtime.InteropServices.COMException (0x80040154): Retrieving the COM class factory for component with CLSID {0006F03A-0000-0000-C000-000000000046} failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))

更新

我已经将编译器 CPU 更改为 x86 和 x64,这并没有解决错误。

Target platform

您的应用程序的目标平台是什么?它是基于 x86 的应用程序吗?

注册 COM class(在 32 位 COM dll 中)的问题(可能),但由于应用程序 运行 在 64 位模式下,它不会找到正确注册(32 位和 64 位 COM 服务器分别注册)。

您还可能会发现 如何解决 COM 异常 Class 未注册(HRESULT 异常:0x80040154 (REGDB_E_CLASSNOTREG))?页面有帮助。

导致错误的问题是两个应用程序(VS2015 和 MS Office 2007)没有 运行 相同的权限。使用相同的权限(管理员或用户)打开两个应用程序,应用程序将运行。感谢您的帮助!

尝试此方法并向其发送参数,例如主题、正文、地址、文件名...我确定这会有效...

Private Sub SendfromOutlook(sSubject As String, sBody As String, sTo As String, sFilename As String)
    Dim oApp As Interop.Outlook._Application
    oApp = New Interop.Outlook.Application
    Dim oMsg As Interop.Outlook._MailItem

    Dim strS As String()
    strS = sTo.Split(",")
    For i As Integer = 0 To strS.Length - 1

        oMsg = oApp.CreateItem(Interop.Outlook.OlItemType.olMailItem)
        oMsg.Subject = sSubject
        oMsg.Body = sBody
        oMsg.To = strS(i)

        Dim str As String = sFilename

        If sFilename <> "" Then


            Dim sBodyLen As Integer = Int(sBody.Length)
            Dim oAttachs As Interop.Outlook.Attachments = oMsg.Attachments
            Dim oAttach As Interop.Outlook.Attachment
            oAttach = oAttachs.Add(str, , sBodyLen)

        End If

        oMsg.Send()

    Next
    MessageBox.Show("EMail Sent Successfully!", "Information", MessageBoxButtons.OK)
    ClearAll()
End Sub

我在这里使用“地址”来一次发送多封邮件..这就是为什么我只是将它拆分并将此函数用作循环函数(Dim strS As String() 和 strS = sTo.Split(","))