'Accessing an email attachment as an object

OK so I have a script that goes through my outlook inbox looking for a particular header string. This works great for emails directly in my inbox. Now I'm trying to expand this detection to emails that contain other emails as attachments. I've spent significant time researching this and I can't seem to find the proper way to access the email attachment directly. What I've ended up doing is saving the attachment to disc and then reading it back in using CreateItemFromTemplate. I find this to be a cludge solution and I'm hoping someone here can help me find a more elegant way to do this where I can bypass the saveas as CreateItemFromTemplate and directly create an item object from the attachment. Here is proof of concept script I've put together for this:

Const olFolderInbox = 6
Const olMail = 43
Const olEmbeddeditem = 5
Const PropName = "http://schemas.microsoft.com/mapi/proptag/0x007D001E"

Set app = CreateObject("Outlook.Application")
set objNamespace = app.GetNamespace("MAPI")
set objInboxItems = objNameSpace.GetDefaultFolder(olFolderInbox).items
wscript.echo "Have your inbox open checking for fish tests or emails as attachments"
for each objItem in objInboxItems
    if objItem.Class = olMail then
        with objItem
            strHeader = .PropertyAccessor.GetProperty(PropName)
            iLoc1 = instr(1,strHeader,"X-Testing",1)
            if iLoc1 > 0 then
                wscript.echo "mytest. From: " & .Sender & " at: " & .ReceivedTime & " subjet: " & .Subject
            end if
            iLoc1 = instr(1,strHeader,"X-PHISHTEST",1)
            if iLoc1 > 0 then
                wscript.echo "Go Fish. From: " & .Sender & " at: " & .ReceivedTime & " subjet: " & .Subject
            end if
            if .attachments.count > 0 then
                set objAttachment = .attachments.item(1)
                if objAttachment.type = olEmbeddeditem then
                    wscript.echo "Has Attachment. From: " & .Sender & " at: " & .ReceivedTime & " subjet: " & .Subject
                    wscript.echo " - Filename: " & objAttachment.Filename
                    objAttachment.SaveAsFile ("c:\temp\TempEmail.msg")
                    set objExtMsg = app.CreateItemFromTemplate("c:\temp\TempEmail.msg")
                    strExtHeader = objExtMsg.PropertyAccessor.GetProperty(PropName)
                    iLoc1 = instr(1,strExtHeader,"X-Testing",1)
                    if iLoc1 > 0 then wscript.echo " ++ This is a plain test message"
                end if
            end if
        end with
    end if
next
wscript.echo "That's all folks"    `


Solution 1:[1]

That is the best you can do in OOM alone - save the attachment as an MSG file and then reopen it. OpenSharedItem is a better way to open an MSG file than CreateItemFromTemplate.

On the Extended MAPI level (C++ or Delphi), you can open the PR_ATTACH_DATA_OBJ property as IMessage using IAttach::OpenProperty. If Extended MAPI is not an option, you can use Redemption (I am its author - any language) - both Safe*Item and RDO families of objects expose EmbeddedMsg property on the attachment object that return the attachment message.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1