Reader Level:
ARTICLE

Read Delete and send e-mail from Outlook with VB.Net

Posted by Hirendra Sisodiya Articles | Visual Basic .NET November 08, 2012
This article includes how to perform various tasks of Outlook with the help of the VB.Net programming language such as reading, deleting and sending emails.
  • 0
  • 0
  • 22678

 Introduction

You can create custom Microsoft Office Outlook objects and manipulate those objects from within Microsoft Office Outlook 2003 or from another application. You can manipulate Outlook objects by using  Microsoft Visual Basic .NET code. This article tells the ways to perform some common tasks used in Outlook by using Visual Basic .NET.

Outlook provides a number of objects and methods for taking advantage of its built-in capabilities. This article include programmatically  reading unread messages from the Inbox, deleteing messages and sending messages.

Read Unread Messages from the Outlook Inbox 

This section describes how to use the Microsoft Outlook Object Library to retrieve unread messages from the Outlook Inbox in Visual Basic .NET. One of the methods used in this is the GetDefaultFolder method.  Mailbox folders contain all Outlook built-in and custom folders. One of the ways to add a reference to a folder in the Mailbox is to specify one of the enumerations with the GetDefaultFolder method, such as is the case of this procedure, olFolderInbox representing the Inbox. 

        Dim tempApp As Outlook.Application
        Dim tempInbox As Outlook.MAPIFolder
        Dim InboxItems As Outlook.Items
        Dim tempMail As Object
        'Dim objattachments, objAttach 
        tempApp = CreateObject("Outlook.Application")
        tempInbox = tempApp.GetNamespace("Mapi").GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)
        InboxItems = tempInbox.Items
        Dim newMail As Outlook.MailItem
        For Each newMail In InboxItems
            If newMail.UnRead Then
                Dim des As String = newMail.Body
                'if u want to delete
                'newMail.Delete()
            End If
        Next

 * MAPI is a component provided by Microsoft to help programmers to connect to messaging applications like Outlook or mail server like Exchange
    
Delete Messages from the Outlook Inbox 

This section describes how to use the Microsoft Outlook 11.0 Object Library to Delete messages from the Outlook Inbox in Visual Basic .NET. 
 
        Dim tempApp As Outlook.Application
        Dim tempInbox As Outlook.MAPIFolder
        Dim InboxItems As Outlook.Items
        Dim tempMail As Object
        tempApp = CreateObject("Outlook.Application")
        tempInbox = tempApp.GetNamespace("MAPI").GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)
        InboxItems = tempInbox.Items
        Dim DeleteMail As Outlook.MailItem
        For Each newMail In InboxItems
            DeleteMail.Delete()
        Next

Send Messages :

This example shows how to send e-mail to Microsoft Outlook  using VB.Net 

        Dim tempApp As New Outlook.Application()
        Dim tempNS As Outlook.NameSpace
        Dim MailFolder As Outlook.MAPIFolder
        tempNS = tempApp.GetNamespace("MAPI")
        tempNS.Logon(, , TrueTrue)
        Dim newMail As Outlook.MailItem
        MailFolder = tempNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderOutbox)
        newMail = MailFolder.Items.Add(Outlook.OlItemType.olMailItem)
        ' sent email will be saved in your outbox also
        newMail.Subject = txtSubject.Text
        newMail.Body = txtBody.Text
        newMail.To = txtTo.Text
        newMail.SaveSentMessageFolder = MailFolder
        newMail.Send()

Note : The most improtant point here to performing all tasks is to add a reference to "Microsoft Outlook object library", In case of

 Microsoft Outlook 2000, Add "Microsoft Outlook 9.0 object library"
 
 Microsoft Outlook 2002, Add "Microsoft Outlook 10.0 object library" 
 Microsoft Outlook 2003, Add "Microsoft Outlook 11.0 object library" 
 Microsoft Outlook 2007, Add "Microsoft Outlook 12.0 object library"

COMMENT USING

Trending up