以 object 的形式访问电子邮件附件

Accessing an email attachment as an object

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

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

这是您单独在 OOM 中所能做的最好的事情 - 将附件保存为 MSG 文件,然后重新打开它。 OpenSharedItem 是比 CreateItemFromTemplate.

更好的打开 MSG 文件的方法

在扩展 MAPI 级别(C++ 或 Delphi),您可以使用 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 对象将 PR_ATTACH_DATA_OBJ 属性 作为 IMessage 打开20=] 附件消息。