Importing an EML File into Outlook

I am trying to create a mail item in Outlook with old date and time, which looks some thing like an Inbox Mail Item which arrived long back. After some painful hours of searching and reading forums, 3rd party libraries, somehow, I got a solution to do this and would like to share it with you here.

Here you go!

Sub CreateMailFromEML()

Dim objPost As Outlook.PostItem
Dim objSafePost As Redemption.SafePostItem
Dim objNS As Outlook.NameSpace
Dim objInbox As Outlook.MAPIFolder
Const PR_ICON_INDEX = &H10800003

Try

objNS = Application.GetNamespace(“MAPI”)
objInbox = objNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)
objPost = objInbox.Items.Add(Outlook.OlItemType.olPostItem)
objSafePost = CreateObject(“Redemption.SafePostItem”)
objPost.Save()
objSafePost.Item = objPost
objSafePost.Import(“c:\SampleEml.eml”, RedemptionSaveAsType.olRFC822)
objSafePost.MessageClass = “IPM.Note”
‘ remove IPM.Post icon
objSafePost.Fields(PR_ICON_INDEX) = String.Empty
objSafePost.Save()

Catch ex As Exception

MessageBox.Show(“Error: ” & ex.ToString)

Finally

objSafePost = Nothing
objPost = Nothing
objInbox = Nothing
objNS = Nothing

End Try

End Sub

Notes:

  • We have to create an instance of Outlook.PostItem instead of Outlook.MailItem.
  • We can also do it using RDOMail which is much simpler than this. (I will discuss about this in separate post).

I have created the mail item with old date time stamps, attachments successfully. But, couldn’t reset the icon of the PostItem that is created. Searching for a solution to it . Will update it in this post once I got that.

Advertisements
This entry was posted in Code Snippets, VSTO and tagged , , , , by Pranav. Bookmark the permalink.

About Pranav

A Software Engineer by profession and a Geek at heart. I love exploring new technologies and working on algorithmic problems. As of now, I write code for bread, butter and satisfaction. I like to develop applications, and generally that involves much more than writing code. It’s not just about hurling code, but producing something meaningful that has a measurable and positive impact, helps others and solves interesting problems.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s