Read E-mail from Outlook Mail Profile

Recently a client of ours had the need to run a DTS Package on a scheduled basis to read an Outlook Mail Profile and perform some transformations on the mail messages before inserting them into a SQL Server table. Below is a VBScript code sample illustrating of how to read unread messages from an Outlook Mail Profile.

Dim oSession
Dim oFolder
Dim oMessages
Dim oMsg
Dim oAttachments

Dim strEmailFrom
Dim strEmailBody
Dim strEmailSubject
Dim EmailRecivedDate

Set oSession = CreateObject(“MAPI.Session”)
oSession.Logon “WARDY IT Solutions”, , True, True

Set oFolder = oSession.Inbox
Set oMessages = oFolder.Messages
Set oMsg = oMessages.GetFirst


While Not oMsg Is Nothing
If oMsg.Unread = True Then
strEmailFrom = oMsg.Sender.Address
strEmailBody = Trim(Replace(oMsg.Text, “‘”, “”””))
strEmailSubject = Trim(Replace(oMsg.Subject, “‘”, “””))
EmailRecivedDate = oMsg.TimeReceived

MsgBox strEmailFrom
MsgBox strEmailBody
End If
Set oMsg = oMessages.GetNext

Set oSession = Nothing