How to Read Email in Visual Basic

Written by jaime avelar
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Read Email in Visual Basic
Read e-mail using Visual Basic. (e-mail image by Soja Andrzej from Fotolia.com)

Knowing how to read your e-mail from Microsoft Office Outlook using Microsoft Visual Basic can help you to develop powerful e-mail applications. Outlook is an e-mail management system that is included in the Microsoft Office suite. Visual Basic is an object-oriented computer programming language that is often preferred by programmers for its flexibility and ease of use. In a few steps you can create a Visual Basic Windows application that will read your Outlook inbox and display e-mail content through the console window.

Skill level:
Easy

Other People Are Reading

Things you need

  • Microsoft Visual Basic.NET

Show MoreHide

Instructions

  1. 1

    Open Microsoft Visual Basic Express and click "New Project..." in the left pane of the screen. Double-click "Console Application." Press "Ctrl" and "A" and then press "Delete" to remove the code that is there.

  2. 2

    Copy and paste the following code to your "Module1.vb" module to read the email items in your Outlook inbox:

    Imports Microsoft.Office.Interop

    Module Module 1

    Sub Main()
    
        Dim otkApp As Outlook.Application = New Outlook.Application
    
        Dim otkMailItem = "IPM.Note"
    
        Dim otkNameSpace As Outlook.NameSpace = otkApp.GetNamespace("MAPI")
    
        Dim otkInboxFolder As Outlook.MAPIFolder = otkNameSpace.GetDefaultFolder _
    
                    (Outlook.OlDefaultFolders.olFolderInbox)
    
        Dim otkMailItems As Outlook.Items = otkInboxFolder.Items
    
    
    
        Dim otkMessage As Outlook.MailItem
    
        Dim iCntr As Integer
    
        For iCntr = 1 To otkMailItems.Count
    
            If otkMailItems.Item(iCntr).MessageClass = otkMailItem Then
    
                otkMessage = otkMailItems.Item(iCntr)
    
                Console.WriteLine(iCntr)
    
                Console.WriteLine(otkMessage.SenderName)
    
                Console.WriteLine(otkMessage.Subject)
    
                Console.WriteLine(otkMessage.ReceivedTime)
    
                Console.WriteLine(otkMessage.Body)
    
                Console.WriteLine("______________________________")
    
            End If
    
        Next
    
    
    
        otkApp = Nothing
    
        otkNameSpace = Nothing
    
        otkMailItems = Nothing
    
        otkMessage = Nothing
    
    End Sub
    

    End Module

  3. 3

    Press "F5" to run your program.

Don't Miss

Filter:
  • All types
  • Articles
  • Slideshows
  • Videos
Sort:
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the eHow.co.uk site, you consent to the use of cookies. For more information, please see our Cookie policy.