ARTICLE

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

Posted by Hirendra Sisodiya Articles | Office and VB.NET September 17, 2009
Tags: outlook, VB.Net
This Article includes how to perform various task of outlook with the help of VB.net programming language such as reading,Deleting and sending emails.
 
Reader Level:

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 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 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(, , True, True)
        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"

Login to add your contents and source code to this article
share this article :
post comment
 

Hirendra, Perhaps I should have said Header Report. I would like to read my inbox (Outlook 2010) and select messages based upon the type of information normally seen in a Header Report, To, From, Subject, etc. Thanks, Henry

Posted by Henry Seymour Jan 21, 2012

Hello Henry can you explain me your problem in brief. what do you mean by 'read SMTP header file'? thanks

Posted by Hirendra Sisodiya Jan 21, 2012

Hirenda, In your 2009 article, Read, Delete and send e-mail from Outlook with VB.Net, can you modify your program to read the SMTP Header file? Excellent article! Thanks,

Posted by Henry Seymour Jan 20, 2012

Hello Hirendra Thank you for the reply I have Outlook 2007 But I managed after much searching to find some code that does allow me to read emails Not sure yet if it is the solution to what I want to do http://www.eggheadcafe.com/tutorials/aspnet/7276bb32-88a8-481a-91aa-d4859ec8f58e/read-outlook-mails-and-se.aspx Trying to find the best way to upgrade a VB6 project and re-write in the VS2010 version of VB In VB6 you had the MAPI message control which allowed me to load the subject of unread emails into a data grid and then click on each row and read the email body without loading the email body into the data grid table Regards David

Posted by David Mills Jan 31, 2011

I'm having the same problem as David. I'm using Outlook 2003. The error occurs during execution of the loop for gathering mail, and it seems to occur during every run of the loop. It seems to be some sort of problem rendering the message.

Posted by Dan Thompson Jan 31, 2011
Become a Sponsor
PREMIUM SPONSORS
  • The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Team Foundation Server Hosting
Become a Sponsor