65.9K
CodeProject is changing. Read more.
Home

Extracting mails and attachments from outlook 2003 in vb.net

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.67/5 (9 votes)

Mar 16, 2006

viewsIcon

92711

Extracting mails

Introduction

hi,

Here, I am putting some sample code for extractng mails and attachments from Outlook 2003 using VB.NET.

The program will extarct the mail from selected folder in Outlook 2003.

For this, first add a reference to Outlook.

****write this code for getting the mails ***************

Dim objOL As Outlook.Application
Dim objNS As Outlook.NameSpace
Dim objFolder As Outlook.Folders
Dim Item As Object
Dim myItems As Outlook.Items
Dim x As Int16

objOL = New Outlook.Application()
objNS = objOL.GetNamespace("MAPI")

Dim olfolder As Outlook.MAPIFolder
olfolder = objOL.GetNamespace("MAPI").PickFolder
myItems = olfolder.Items

Dim i As Integer
For x = 1 To myItems.Count
    messagebox.show(myItems.Item(x).SenderName) 
    messagebox.show myItems.Item(x).SenderEmailAddress)
    messagebox.showmyItems.Item(x).Subject)
    messagebox.showmyItems.Item(x).Body)
    messagebox.show myItems.Item(x).to)
    messagebox.showmyItems.Item(x).ReceivedByName)
    messagebox.show myItems.Item(x).ReceivedOnBehalfOfName)
    messagebox.showmyItems.Item(x).ReplyRecipientNames)
    messagebox.showmyItems.Item(x).SentOnBehalfOfName)
    messagebox.showmyItems.Item(x).CC)
    messagebox.show myItems.Item(x).ReceivedTime)
Next x

***********for getting attachments use this code******************

Dim Atmt As Outlook.Attachment

For Each Atmt In Item.Attachments
    filename = "C:\Email Attachments\" + Atmt.FileName
    Atmt.SaveAsFile(filename)
Next Atmt

Try it out .. and let me know if there is any problem.

Thanks.