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.

Leave a Comment

Your email address will not be published. Required fields are marked *