Link to home
Start Free TrialLog in
Avatar of BobBarker_99
BobBarker_99

asked on

Read outlook emails from vb.net application

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)
 
                Console.WriteLine(i)
                Console.WriteLine(oMsg.SenderName)
                Console.WriteLine(oMsg.Subject)
                Console.WriteLine(oMsg.ReceivedTime)
                Console.WriteLine(oMsg.Body)
                Console.WriteLine("---------------------------")
            End If
        Next
 
        ' Clean up.
        oApp = Nothing
        oNS = Nothing
        oItems = Nothing
        oMsg = Nothing
 
    End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of BobBarker_99
BobBarker_99

ASKER

Anyone have an idea to do it using outlook automation?  I dont really want to buy a license for lesnikowski
It seems like it should be possible using the above code and impersonation?
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)