[Last Call] Learn how to a build a cloud-first strategyRegister Now


Read outlook emails from vb.net application

Posted on 2009-04-30
Medium Priority
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
  • 3
LVL 70

Accepted Solution

Éric Moreau earned 1500 total points
ID: 24270485
instead of using Outlook automation, I have had best results using http://www.lesnikowski.com/mail/Index.aspx

Author Comment

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

Author Comment

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

Author Closing Comment

ID: 31576444
This is fine if you want to buy the library.  I found that it was easiest to do this using the EWS API(http://www.microsoft.com/downloads/details.aspx?FamilyID=a8c9d043-c66c-4971-9459-8d1df1608c8b&displaylang=en)

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
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 …
Loops Section Overview
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month17 days, 22 hours left to enroll

830 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