Get items in Outlook Inbox with vb.net

Well life gets stranger and stranger. I am 40 and a girl who was 22 who I have known for a while and asked me if I wanted to have sex. I am just like shocked you know? I am old enough to be her father! Not to mention that people in the generation behind me are so …..up front about this stuff. Anyway, did it make me feel good that someone so young would be interested in me that way? Maybe for a second. But I know from raising my own children that they are surrounded by media telling them to act that way. Perhaps that is the reason. I just don’t know.

Anyway, using vb.net we want to see the items in an Outlook inbox using .NET 2.0 or above.

By the way, does anyone know how to suppress the security alerts in Outlook when accessing the address book? I am floored on this one. I have tried everything!

Make it a great day!


Join me on Facebook

‘ Add a reference to the COM ‘Microsoft Outlook Object Library’ type to the project.
Imports System
Imports System.Collections.Generic
Imports Microsoft.Office.Interop.Outlook
Public Class OutlookInbox
    Public Shared Function GetMail(ByVal mailBoxName As String) As List(Of Email)
        Dim list As New List(Of Email)()
        Dim app As New Application()
        Dim NS As [NameSpace] = app.GetNamespace("MAPI")
        Dim foundMailBox As Boolean = False
        Dim foundInbox As Boolean = False
        For Each mailboxFolder As MAPIFolder In NS.Folders
            If mailboxFolder.Name = "Mailbox – " + mailBoxName Then
                foundMailBox = True
                For Each inboxFolder As MAPIFolder In mailboxFolder.Folders
                    If inboxFolder.Name = "Inbox" Then
                        foundInbox = True
                        For Each item As Object In inboxFolder.Items
                            Dim mailItem As MailItem = TryCast(item, MailItem)
                            If mailItem IsNot Nothing Then
                                Dim email As New Email()
                                email.Subject = mailItem.Subject
                                email.DateReceived = mailItem.ReceivedTime
                                list.Add(email)
                            End If
                        Next
                        Exit For
                    End If
                Next
                Exit For
            End If
        Next
        If Not foundMailBox Then
            Throw New ApplicationException(String.Format("Mailbox for ‘{0}’ not found", mailBoxName))
        End If
        If Not foundInbox Then
            Throw New ApplicationException(String.Format("Inbox for ‘{0}’ not found", mailBoxName))
        End If
        Return list
    End Function
End Class
Public Class Email
    Public Subject As String
    Public DateReceived As DateTime
End Class

 

About these ads
  1. #1 by Khurram Zeshan on March 4, 2011 - 12:08 pm

    Very Nice Thanks it helped a lot :)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: