Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Retrieve unread messages from Inbox

0.00/5 (No votes)
30 Jan 2008 1  
How to retrieve unread messages from Inbox by using Outlook Object Model in Visual Basic .NET

Introduction

The article is about reading, mails for th outlook, I faced this requirement when my client told me to automate the reading the UNREAD mails from his inbox without any intervention. So I just created the basic application which could read the inbox mails.

This article describes how to use Microsoft Outlook 10.0 Object Library to retrieve unread messages from the Outlook Inbox in Visual Basic .NET(2005)

Basic Recquirement:

1. Out Look 2003 Should be configured on your machine.

2. Visual Studio 2005 installed on your machine

Using the code:

Start the new session(Console) application of VB.net(2005) and paste the Code below in your module1.vb

Before you run the code don't forget to add the references to the Microsoft Outlook 10.0 Object Library. to add the reference browse through Project->Add References select COM tab

then select "Microsoft Outlook 10.0 Object Library" and press OK.

Run the Application. The Console Window will show the all the UNREAD mails from outlook(2003) the INBOX.

Imports System.Reflection
'// Reference to Microsoft Outlook 11.0 Object Library
Imports Microsoft.Office.Interop.Outlook

Module Module1

    Sub Main()
        ' Create Outlook application.
        Dim oApp As Microsoft.Office.Interop.Outlook.Application = New Microsoft.Office.Interop.Outlook.Application

        ' Get Mapi NameSpace.
        Dim oNS As Microsoft.Office.Interop.Outlook.NameSpace = oApp.GetNamespace("mapi")
        oNS.Logon("YourValidProfile", Missing.Value, False, True) ' TODO:

        ' Get Messages collection of Inbox.
        Dim oInbox As Microsoft.Office.Interop.Outlook.MAPIFolder = oNS.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox)
        Dim oItems As Microsoft.Office.Interop.Outlook.Items = oInbox.Items
        Console.WriteLine("Total : " & oItems.Count)

        ' Get unread e-mail messages.
        oItems = oItems.Restrict("[Unread] = true")
        Console.WriteLine("Total Unread : " & oItems.Count)

        ' Loop each unread message.
        Dim oMsg As Microsoft.Office.Interop.Outlook.MailItem
        Dim i As Integer

        For i = 1 To oItems.Count
            oMsg = oItems.Item(i)

            Console.WriteLine(i)
            Console.WriteLine(oMsg.SenderName)
            Console.WriteLine(oMsg.Subject)
            Console.WriteLine(oMsg.ReceivedTime)
            Console.WriteLine(oMsg.Body)
            Console.WriteLine("---------------------------")
            Console.WriteLine("press any key to continue")
        Next

        ' Log off.
        oNS.Logoff()

        ' Clean up.
        oApp = Nothing
        oNS = Nothing
        oItems = Nothing
        oMsg = Nothing
    End Sub

End Module
        
        

History

  • 29/01/08 - Basic Version
  • Hope this code will help you people. Happy coding.

    License

    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here