PowerShell EWS API |如何下载附件?



在下面的代码中,我能够检索电子邮件的主题,但无法下载附件。我甚至不能输出附件的名称。附件是WAV声音文件。

Import-Module "C:Program FilesMicrosoftExchangeWeb Services2.2Microsoft.Exchange.WebServices.dll"
$folderid = new-object Microsoft.Exchange.WebServices.Data.FolderId($v_FolderID)
$fiItems = $null
$iv = new-object Microsoft.Exchange.WebServices.Data.ItemView(1000)
$fiItems = $service.FindItems($folderid, $args[1], $iv)
foreach ($Item in $fiItems.Items[0])
{
$v_Subject = $Item.Subject

foreach($attachment in $Item.Attachments)
{
$attachment.Load()
$attachmentname = $attachment.Name.ToString()
$attachmentname
$file = New-Object System.IO.FileStream("C:", [System.IO.FileMode]::Create)
$file.Write($attachment.Content, 0, $attachment.Content.Length)
$file.Close()
}
}
$iv.offset += $fiItems.Items.Count
Out-File -FilePath "C:EWSSubject.txt" -InputObject $v_Subject```

首先需要创建一个PropertySet对象,因为附件信息不会自动加载。

## Target Path Folder
$TargetPath = "c:tempattachments"
## Create a PropertySet with the Attachments metadata
$ItemPropetySet = [Microsoft.Exchange.WebServices.Data.PropertySet]::new(
[Microsoft.Exchange.Webservices.Data.BasePropertySet]::IdOnly,
[Microsoft.Exchange.WebServices.Data.ItemSchema]::Attachments,
[Microsoft.Exchange.WebServices.Data.ItemSchema]::HasAttachments
)

:

## Iterate the items and find messages with attachments
foreach ($item in $fiItems.Items)
{
## Load the Message attachment metadata using the PropertySet Created
$message = [Microsoft.Exchange.WebServices.Data.EmailMessage]::Bind(
$service, $item.Id, $ItemPropetySet)
if ($message.HasAttachments)
{
foreach ($attachment in $message.Attachments)
{
if ($attachment -is [Microsoft.Exchange.WebServices.Data.FileAttachment])
{
$FilePath = Join-Path $TargetPath $attachment.Name
$attachment.Load($FilePath)
}
}
}
}

相关内容

  • 没有找到相关文章

最新更新