使用EWS API读取邮件

 

#安装EwsManagedApi.msi,然后导入EWS模块
Import-Module -Name "C:\Program Files\Microsoft\Exchange\Web Services\2.0\Microsoft.Exchange.WebServices.dll"
#$exchService.UseDefaultCredentials = $true 使用本地默认凭据
#使用其他凭据
$Credentials = New-Object Microsoft.Exchange.WebServices.Data.WebCredentials("username","password","uuuu")
$exchService = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService
$exchService.Credentials = $Credentials
$exchService.AutodiscoverUrl("[email protected]") #登录Exchange
$exchService
#绑定收件箱(Inbox)
$inbox = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($exchService,[Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox)
$inbox|gm

$ms = $inbox.FindItems(10) #查询邮件数量
foreach ($m in $ms)
{
$m.Load()
$m.subject
$m.IsRead
}

 

读取未读邮件:

#使用其他凭据
$Credentials = New-Object Microsoft.Exchange.WebServices.Data.WebCredentials("ly","l","u")
$exchService = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService
$exchService.Credentials = $Credentials
$exchService.AutodiscoverUrl("[email protected]") #登录Exchange
$exchService
#绑定收件箱(Inbox)
$inbox = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($exchService,[Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox)
$view = New-Object Microsoft.Exchange.WebServices.Data.ItemView(100) #The view that controls the number of items that are returned.
#读取未读邮件
$searchFilter =  new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo([Microsoft.Exchange.WebServices.Data.EmailMessageSchema]::IsRead, $false)
$findResults = $exchService.FindItems([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox,$searchFilter,$view)
$findResults.TotalCount

 

遍历Inbox下所有文件夹:

Import-Module -Name "C:\Program Files\Microsoft\Exchange\Web Services\2.0\Microsoft.Exchange.WebServices.dll"
#$exchService.UseDefaultCredentials = $true 使用本地默认凭据
#使用其他凭据
$Credentials = New-Object Microsoft.Exchange.WebServices.Data.WebCredentials("ly","l","ux")
$exchService = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService
$exchService.Credentials = $Credentials
$exchService.AutodiscoverUrl("[email protected]") #登录Exchange
$exchService
#绑定收件箱(Inbox)
$inbox = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($exchService,[Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox)  #MsgFolderRoot根目录
#查询Inbox下的所有文件夹
$FolderList = new-object Microsoft.Exchange.WebServices.Data.FolderView(100)
$FolderList.Traversal = [Microsoft.Exchange.WebServices.Data.FolderTraversal]::Deep
$findFolderResults = $inbox.FindFolders($FolderList)

 

遍历某个子文件夹(手机邮箱配置)下面的邮件:

#安装EwsManagedApi.msi,然后导入EWS模块
Import-Module -Name "C:\Program Files\Microsoft\Exchange\Web Services\2.0\Microsoft.Exchange.WebServices.dll"
#$exchService.UseDefaultCredentials = $true 使用本地默认凭据
#使用其他凭据
$Credentials = New-Object Microsoft.Exchange.WebServices.Data.WebCredentials("l","l","ux")
$exchService = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService
$exchService.Credentials = $Credentials
$exchService.AutodiscoverUrl("[email protected]") #登录Exchange
$exchService
#绑定收件箱(Inbox)
$inbox = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($exchService,[Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox)

#查询Inbox下的所有文件夹
$FolderList = new-object Microsoft.Exchange.WebServices.Data.FolderView(100)
$FolderList.Traversal = [Microsoft.Exchange.WebServices.Data.FolderTraversal]::Deep
$findFolderResults = $inbox.FindFolders($FolderList)
#查询名为“手机邮箱配置”的文件夹,然后遍历其中的邮件
$folder_mobile = $findFolderResults|?{$_.DisplayName -eq "手机邮箱配置"}
$folder_ms= [Microsoft.Exchange.WebServices.Data.Folder]::Bind($exchService,$folder_mobile.Id)
$mails = $folder_ms.FindItems(100) #查询邮件数量
foreach ($m in $mails)
{
$m.Load()
$m.Subject
}

 

读取“手机配置邮箱”文件夹下的未读邮件:

#安装EwsManagedApi.msi,然后导入EWS模块
Import-Module -Name "C:\Program Files\Microsoft\Exchange\Web Services\2.0\Microsoft.Exchange.WebServices.dll"
#$exchService.UseDefaultCredentials = $true 使用本地默认凭据
#使用其他凭据
$Credentials = New-Object Microsoft.Exchange.WebServices.Data.WebCredentials("l","l","ux")
$exchService = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService
$exchService.Credentials = $Credentials
$exchService.AutodiscoverUrl("[email protected]") #登录Exchange

#绑定收件箱(Inbox)
$inbox = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($exchService,[Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox)

#查询Inbox下的所有文件夹
$FolderList = new-object Microsoft.Exchange.WebServices.Data.FolderView(100)
$FolderList.Traversal = [Microsoft.Exchange.WebServices.Data.FolderTraversal]::Deep
$findFolderResults = $inbox.FindFolders($FolderList)
#找到“手机邮箱配置”文件夹
$folder_mobile = $findFolderResults|?{$_.DisplayName -eq "手机邮箱配置"}
$folder_ms= [Microsoft.Exchange.WebServices.Data.Folder]::Bind($exchService,$folder_mobile.Id)
#读取未读邮件
$view = New-Object Microsoft.Exchange.WebServices.Data.ItemView(200)
$searchFilter =  new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo([Microsoft.Exchange.WebServices.Data.EmailMessageSchema]::IsRead, $false)
$findResults = $exchService.FindItems($folder_mobile.Id,$searchFilter,$view)
$findResults.TotalCount

#遍历邮件内容
foreach ($m in $findResults.Items)
{
$m.Load()
$m.From
$m.Subject
$m.Body.BodyType
#$m.Body.Text
}

 

 

将邮件置为已读:

if ($m.IsRead -eq $false)
    {
     $m.IsRead = $true
     $m.Update([Microsoft.Exchange.WebServices.Data.ConflictResolutionMode]::AlwaysOverwrite) #提交到Server端
    }

 

发送邮件:

$message = New-Object Microsoft.Exchange.WebServices.Data.EmailMessage -ArgumentList $exchService
$message.Subject = '11Test is a test222'
$message.Body = 'This message is being sent through EWS with PowerShell'
$message.ToRecipients.Add(‘ly@yy.com’)
$message.SendAndSaveCopy()

 

你可能感兴趣的:(使用EWS API读取邮件)