Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting Outlook Email with VB.NET 1

Status
Not open for further replies.

BG12424

Programmer
Jun 4, 2002
717
US
I've posted in the Office Forum thread68-731432, but not sure which forum would help most.

I am able to pull back emails in my Outlook Inbox with the following code, however, if the email has no subject line, I get an invalid cast exception. Can anyone take a look at the code below and help me get through this? Thanks

Code:
        Dim olApp As Outlook.Application = New Outlook.Application()
        Dim olNS As Outlook.NameSpace = olApp.GetNamespace("MAPI")
        olNS.Logon("Brian Gaines", "", False, True)
        Dim olInbox As Outlook.MAPIFolder = olNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)
        Dim olItems As Outlook.Items = olInbox.Items
        Dim i As Integer
        Try
            If olItems.Count > 0 Then
                ListBox1.Items.Clear()
                For i = 1 To olItems.Count
                    Dim email As Outlook.MailItem = olItems.Item(i)

                    ListBox1.Items.Add(email.Subject & " >>> FROM: " & email.SenderName)
                Next
                Label1.Text = "Emails: " & (i - 1).ToString
                olNS.Logoff()
            End If
        Catch ex As Exception
            lblMessage.Text = ex.ToString
        Finally
            olApp = Nothing
            olNS = Nothing
            olItems = Nothing
            olInbox = Nothing
        End Try


regards,
Brian
 
Public Sub testing(ByVal strName As String)

Dim olApp As Outlook.Application = New Outlook.Application
Dim olNS As Outlook.NameSpace = olApp.GetNamespace("MAPI")

Try
olNS.Logon(strName, password, False, True)
Dim olInbox As Outlook.MAPIFolder = olNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)
Dim olItems As Outlook.Items = olInbox.Items
Dim i As Integer

If olItems.Count > 0 Then
ListBox1.Items.Clear()
For i = 1 To olItems.Count
Dim email As Outlook.MailItem = olItems.Item(i)

ListBox1.Items.Add(email.Subject & " >>> FROM: " & email.SenderName)
Next
Label1.Text = "Emails: " & (i - 1).ToString
olNS.Logoff()
End If
Catch ex As Exception
Response.Write(ex.ToString)
Finally
olApp = Nothing
olNS = Nothing
'olItems = Nothing
'olInbox = Nothing
End Try
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top