访问电子邮件附件作为对象

时间:2017-12-07 20:14:50

标签: vbscript outlook outlook-vba email-attachments

好的,所以我有一个脚本通过我的Outlook收件箱查找特定的标题字符串。这适用于直接在我的收件箱中的电子邮件。现在,我尝试将此检测扩展到包含其他电子邮件作为附件的电子邮件。我花了很多时间研究这个,但我似乎无法找到直接访问电子邮件附件的正确方法。我最终做的是将附件保存到光盘,然后使用CreateItemFromTemplate将其读回。我发现这是一个cludge解决方案,我希望这里有人可以帮助我找到一个更优雅的方法,我可以绕过Saveas作为CreateItemFromTemplate,并直接从附件创建一个项目对象。以下是我为此做出的概念证明:

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"    `

1 个答案:

答案 0 :(得分:0)

这是您在OOM中可以做的最好的事情 - 将附件保存为MSG文件,然后重新打开它。 OpenSharedItem是打开MSG文件的更好方式,而不是CreateItemFromTemplate

在扩展MAPI级别(C ++或Delphi)上,您可以使用IAttach::OpenPropertyPR_ATTACH_DATA_OBJ属性打开为IMessage。如果无法选择扩展MAPI,则可以使用Redemption(任何语言) - Safe*ItemRDO个对象系列都会在attachment对象上显示EmbeddedMsg属性。