如何使用 EWS 将项目移动到文件夹?
Posted
技术标签:
【中文标题】如何使用 EWS 将项目移动到文件夹?【英文标题】:How can I use EWS to move items to a folder? 【发布时间】:2015-06-19 16:13:50 【问题描述】:以下 PowerShell 脚本已根据我们的情况进行了调整。 它读取收件箱文件夹中的所有电子邮件,然后提取附件
它运行良好,但我想将项目移动到“/Processed”邮箱根文件夹。此文件夹不是收件箱文件夹的子文件夹:
Mailbox
L Inbox
L Processed
L Sent Items
L Deleted Items
如果我使用以下行
[VOID]$miMailItems.Move("DeletedItems")
但是,它没有按预期工作。它删除了电子邮件,但在我的个人邮箱中,而不是“约翰”邮箱!
那么,你能帮我吗
-
更正使用代码
[VOID]$miMailItems.Move("DeletedItems")
时将项目移动到john邮箱的代码
请告诉我如何将项目简单地移动到 john“已处理”邮箱子文件夹?
$MailboxName = 'john@domain.com'
$downloadDirectory = '\\share\'
$dllpath = "C:\Program Files\Microsoft\Exchange Server\V15\Bin\Microsoft.Exchange.WebServices.dll"
[VOID][Reflection.Assembly]::LoadFile($dllpath)
$service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2013)
$sidbind = "LDAP://<SID=" + (Get-ADUser exchadmin).SID.ToString() + ">"
$aceuser = [ADSI]$sidbind
$service.AutodiscoverUrl($aceuser.mail.ToString())
$folderid = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox,$MailboxName)
$InboxFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid)
$Sfha = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo([Microsoft.Exchange.WebServices.Data.EmailMessageSchema]::HasAttachments, $true)
$sfCollection = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+SearchFilterCollection([Microsoft.Exchange.WebServices.Data.LogicalOperator]::And);
$sfCollection.add($Sfha)
$view = new-object Microsoft.Exchange.WebServices.Data.ItemView(2000)
$frFolderResult = $InboxFolder.FindItems($sfCollection,$view)
foreach ($miMailItems in $frFolderResult.Items)
$miMailItems.Load()
foreach($attach in $miMailItems.Attachments)
$attach.Load()
$fiFile = new-object System.IO.FileStream(($downloadDirectory + “\” + (Get-Date).Millisecond + "_" + $attach.Name.ToString()), [System.IO.FileMode]::Create)
$fiFile.Write($attach.Content, 0, $attach.Content.Length)
$fiFile.Close()
$miMailItems.isread = $true
$miMailItems.Update([Microsoft.Exchange.WebServices.Data.ConflictResolutionMode]::AlwaysOverwrite)
# The following send items to my personal "Deleted Items" folder instead of the john mailbox...
[VOID]$miMailItems.Move("DeletedItems")
# How can I send items to the "/Processed" folder of the john mailbox ?
【问题讨论】:
【参考方案1】:Move 方法将获取您要将项目移动到的文件夹的 FolderId,因此您需要首先找到要移动到的文件夹的 FolderId,例如
function FolderIdFromPath
param (
$FolderPath = "$( throw 'Folder Path is a mandatory Parameter' )",
$SmtpAddress = "$( throw 'Folder Path is a mandatory Parameter' )"
)
process
## Find and Bind to Folder based on Path
#Define the path to search should be seperated with \
#Bind to the MSGFolder Root
$folderid = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::MsgFolderRoot,$SmtpAddress)
$tfTargetFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid)
#Split the Search path into an array
$fldArray = $FolderPath.Split("\")
#Loop through the Split Array and do a Search for each level of folder
for ($lint = 1; $lint -lt $fldArray.Length; $lint++)
#Perform search based on the displayname of each folder level
$fvFolderView = new-object Microsoft.Exchange.WebServices.Data.FolderView(1)
$SfSearchFilter = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo([Microsoft.Exchange.WebServices.Data.FolderSchema]::DisplayName,$fldArray[$lint])
$findFolderResults = $service.FindFolders($tfTargetFolder.Id,$SfSearchFilter,$fvFolderView)
if ($findFolderResults.TotalCount -gt 0)
foreach($folder in $findFolderResults.Folders)
$tfTargetFolder = $folder
else
"Error Folder Not Found"
$tfTargetFolder = $null
break
if($tfTargetFolder -ne $null)
return $tfTargetFolder.Id.UniqueId.ToString()
else
throw "Folder not found"
#Example use
$fldId = FolderIdFromPath -FolderPath "\Processed" -SmtpAddress $aceuser.mail.ToString()
$SubFolderId = new-object Microsoft.Exchange.WebServices.Data.FolderId($fldId)
$SubFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$SubFolderId)
那就换个方式
[VOID]$miMailItems.Move($SubFolder.Id)
【讨论】:
以上是关于如何使用 EWS 将项目移动到文件夹?的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 EWS 保存对话历史消息并使其正确显示在对话历史文件夹中