如何通过PowerShell从Oulook文件夹中的电子邮件中获取电子邮件地址?

时间:2017-11-13 12:39:04

标签: powershell email outlook mapi

我有一个Outlook文件夹,我们称之为LoremIpsum,我有超过1000个电子邮件草稿,我想要通过PowerShell进行一些过滤。我可以使用以下脚本访问该文件夹并查看电子邮件:

Function HandleRemaining {
    [CmdletBinding()]
    Param()

    BEGIN {
        Clear-Host
    }

    PROCESS {
        $outlook = New-Object -ComObject outlook.application
        $mapi = $outlook.getnamespace("MAPI");

        $email = $mapi.Folders.Item(1).Folders.Item('LoremIpsum').Items(1)

        foreach ($recip in $email.Recipients) {
           $recip 
        }

        $email.To
        $email.CC
    }

    END {
    }
}

HandleRemaining

问题是,$recip$email.To都没有返回该电子邮件的ToCC的电子邮件地址,而是让我解决了该问题名称,例如:

Application           : Microsoft.Office.Interop.Outlook.ApplicationClass
Class                 : 4
Session               : Microsoft.Office.Interop.Outlook.NameSpaceClass
Parent                : System.__ComObject
Address               : /o=ExchangeLabs/ou=Exchange Administrative Group (ALPHA-NUMERIC)/cn=Recipients/cn=LONG-ALPHANUMERIC-HERE
AddressEntry          : System.__ComObject
AutoResponse          : 
DisplayType           : 0
EntryID               : <snip>
Index                 : 1
MeetingResponseStatus : 0
Name                  : John Walker
Resolved              : True
TrackingStatus        : 0
TrackingStatusTime    : 01-Jan-01 00:00:00
Type                  : 1
PropertyAccessor      : System.__ComObject
Sendable              : True

John Walker

我更改了数字和代码以保护隐私,但这是我得到的回报。 那么,如何才能获得给定电子邮件草稿收件人的正确电子邮件地址?

1 个答案:

答案 0 :(得分:2)

我认为你需要使用PropertyAccessor。

$PR_SMTP_ADDRESS = "http://schemas.microsoft.com/mapi/proptag/0x39FE001E"
$smtpAddress = $recip.PropertyAccessor.GetProperty($PR_SMTP_ADDRESS)

见这里(警告!VBA):https://msdn.microsoft.com/en-us/VBA/Outlook-VBA/articles/obtain-the-e-mail-address-of-a-recipient