I'm trying to create a program which retrieves all emails from Outlook's(2007 desktop version) Inbox and puts them into a DataGridView.
Code:
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
When I try to Build the project I'm getting the follow error:
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))
UPDATE
I've changed the Compiler CPU to x86 and x64, this does not solve the error.
What is the target platform of your application? Is it a x86-based application?
The problem (probably) that the COM class (which is in a 32 bit COM dll) is registered, but since the application is running in 64 bit mode it won't find a correct registration (32 bit and 64 bit COM servers are registered separately).
Also you may find the How to solve COM Exception Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))? page helpful.
The problem causing the error was that both applications(VS2015 and MS Office 2007) were not running under the same rights. Open both applications with same rights(administrator or user) and the application will work. Thanks for the help!
try this method and Send arguments to it such as Subject,body,Toaddress,filename...Im Sure this will works..
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
here I use To address with "," to send Multiple mails at a time..thats why i just split it and used this functions as a looping one ( Dim strS As String() and strS = sTo.Split(","))
User contributions licensed under CC BY-SA 3.0