Read outlook emails from application

Posted on 2009-04-30
Last Modified: 2012-08-14
Hello, I found some code to help me do this.  I have posted it below in the code section.

It works fine for reading emails in my outlook account.  What I cant figure out is how to change the computer/account it reads emails from.  I want to be able to read emails from a different account that I specify on my companys exchange server.

Does anybody know how I can specify which account to read from?
Sub Main()

        ' Create Outlook application.

        Dim oApp As Outlook.Application = New Outlook.Application

        ' String used for comparison with mail item.

        Dim sClassComp = "IPM.Note"

        ' Get Mapi NameSpace.

        Dim oNS As Outlook.NameSpace = oApp.GetNamespace("MAPI")

        ' Get Messages collection of Inbox.

        Dim oInbox As Outlook.MAPIFolder = oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)

        Dim oItems As 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 Outlook.MailItem

        Dim i As Integer

        For i = 1 To oItems.Count

            'Test to make sure item is a mail item

            'and not a meeting request.

            If oItems.Item(i).MessageClass = sClassComp Then

                oMsg = oItems.Item(i)







            End If


        ' Clean up.

        oApp = Nothing

        oNS = Nothing

        oItems = Nothing

        oMsg = Nothing

    End Sub

Open in new window

Question by:BobBarker_99
    LVL 69

    Accepted Solution

    instead of using Outlook automation, I have had best results using

    Author Comment

    Anyone have an idea to do it using outlook automation?  I dont really want to buy a license for lesnikowski

    Author Comment

    It seems like it should be possible using the above code and impersonation?

    Author Closing Comment

    This is fine if you want to buy the library.  I found that it was easiest to do this using the EWS API(

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
    Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now