Read, delete and send email from Outlook in vb.net

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.  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.
 
See the following example.

Private Sub ReadEmailFromOutlook()
        Dim tempApp As Outlook.Application
        Dim tempInbox As Outlook.MAPIFolder
        Dim InboxItems As Outlook.Items
        '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
        InboxItems = Nothing
        tempInbox = Nothing
        tempApp = Nothing
End Sub

* 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. The following code sample will delete all messages in your outlook inbox.

    Private Sub DelateEmailFromOutlook()
        Dim tempApp As Outlook.Application
        Dim tempInbox As Outlook.MAPIFolder
        Dim InboxItems As Outlook.Items
        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
        InboxItems = Nothing
        tempInbox = Nothing
        tempApp = Nothing
    End Sub

 

Send Messages from Outlook:

This example shows how to send e-mail through Outlook  using VB.Net. The following code snippet is very useful when we need to send an email from the local outlook.

 Private Sub SendEmailFromOutlook()
        Dim tempApp As Outlook.Application()
        tempApp = CreateObject("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()
 
        MailFolder = Nothing
        tempNS = Nothing
        tempApp = Nothing
    End Sub

Note : The most important point here to performing all tasks is that we need to add a reference to “Microsoft Outlook object library” in our project . In case of

Microsoft Outlook 2000, Add “Microsoft Outlook 9.0 object library” reference.
Microsoft Outlook 2002, Add “Microsoft Outlook 10.0 object library” reference.
Microsoft Outlook 2003, Add “Microsoft Outlook 11.0 object library” reference.
Microsoft Outlook 2007, Add “Microsoft Outlook 12.0 object library” reference.
Microsoft Outlook 2010, Add “Microsoft Outlook 14.0 object library” reference.

How to use and run the above Code

1. Start a new project in vb.net language in your visual studio.
2. Add a windows form and reference of Outlook library.
3. Write the above code of lines in windows form. ( you can use Button1_Click event).
4. Run the application.

41 thoughts on “Read, delete and send email from Outlook in vb.net”

  1. hi i again! …I found the error.. perfect code and great work, thanks. forgiveness for my improvised english.  🙂

    error:
    Imports Microsoft.Office.Interop
     
     

  2. Hello Hirendra

    When I place the code to read mail within the click event if the button I get thefollowing error.
    I placed this code for reading unread messages within the button click event and I get an error about   ReadEmailFromOutlook() is not declared? Please help as I do need to be able to read mail from VB
     

     

  3. Hi Brian

    ReadEmailFromOutlook() is not declared‘ error means you does not have ReadEmailFromOutlook() function in current context.

    You can write the code under button click event directly, something like this:


    private sub Button1_click()
    Dim tempApp As Outlook.Application
    Dim tempInbox As Outlook.MAPIFolder
    Dim InboxItems As Outlook.Items
    '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
    InboxItems = Nothing
    tempInbox = Nothing
    tempApp = Nothing
    End sub

  4. Hello Hirendra

    There seems to be something I am doing wrong I am still getting lots of errors, telling me things are not defined. I have added a reference to Outlooks object library.

    Could you please send me the entire program. We are in deperate need to read email from VB

    Brian     

  5. Hello, it is another Brian 🙂 I am trying to make a program using a timer that will check outlook every 2 minutes or so… for new emails, and then parse those emails. I am planning on using the above code. I am afraid an unkown error will occur if I have two or more of these programs running at the same time, or someone is using outlook at the same time. Im afraid that the two programs running and the other all trying to interact with outlook will cause the program to crash or somehow mess up. What do I need to look out for and how much of a mess am I getting into?

    1. Hello Brian..I believe there is no need to worry, Your Program can use the outlook at same time when another user will work on that..You can start write the code to check the email with out any worry.

      Errors are the part of the program. so leave your worry and start work..let me know if you have any problem.

  6. I have vb 2015 outlook 2010 and tried this code but didnt work.
    I try on outlook 2016 too – didnt work too.
    I have added a reference outlook 14 object and 16 object.

Comments are closed.