PowerShell-Office 365邮箱-从特定文件夹获取电子邮件

时间:2019-04-22 12:02:13

标签: powershell office365

我需要从Office 365帐户中获取所有电子邮件,而不是从“收件箱”中获取,而是从特定文件夹“已​​处理”中获取

    $mail="automate@company.com"
    $password="pass"

    # Set the path to your copy of EWS Managed API 
    $dllpath = "C:\Program Files\Microsoft\Exchange\Web Services\2.0\Microsoft.Exchange.WebServices.dll" 
    # Load the Assemply 
    [void][Reflection.Assembly]::LoadFile($dllpath) 

    # Create a new Exchange service object 
    $service = new-object Microsoft.Exchange.WebServices.Data.ExchangeService 

    #These are your O365 credentials
    $Service.Credentials = New-Object Microsoft.Exchange.WebServices.Data.WebCredentials($mail,$password)

    # this TestUrlCallback is purely a security check
    $TestUrlCallback = {
        param ([string] $url)
        if ($url -eq "https://autodiscover-s.outlook.com/autodiscover/autodiscover.xml") {$true} else {$false}
    }
    # Autodiscover using the mail address set above
    $service.AutodiscoverUrl($mail,$TestUrlCallback)

    # create Property Set to include body and header of email
    $PropertySet = New-Object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties)

    # set email body to text
    $PropertySet.RequestedBodyType = [Microsoft.Exchange.WebServices.Data.BodyType]::Text;

    # Set how many emails we want to read at a time
    $numOfEmailsToRead = 100

    # Index to keep track of where we are up to. Set to 0 initially. 
    $index = 0 

    # Do/while loop for paging through the folder 
    do 
    { 
        # Set what we want to retrieve from the folder. This will grab the first $pagesize emails
        $view = New-Object Microsoft.Exchange.WebServices.Data.ItemView($numOfEmailsToRead,$index) 
        # Retrieve the data from the folder 
       # $findResults = $service.FindItems([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Processed,$view) 

       $findResults = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo([Microsoft.Exchange.WebServices.Data.FolderSchema]::DisplayName,"Processed") 
}

但是$ findResults变量为空,尽管该文件夹中有电子邮件,但是有什么方法可以从Office 365中的特定文件夹中获取电子邮件吗?

1 个答案:

答案 0 :(得分:0)

找到解决方案,this帖子对我有很大帮助

$mail="mail@company.com"
$password="pass"


$USER_DEFINED_FOLDER_IN_MAILBOX = "Processed"

# Set the path to your copy of EWS Managed API 
$dllpath = "C:\Program Files\Microsoft\Exchange\Web Services\2.0\Microsoft.Exchange.WebServices.dll" 
# Load the Assemply 
[void][Reflection.Assembly]::LoadFile($dllpath) 

# Create a new Exchange service object 
$service = new-object Microsoft.Exchange.WebServices.Data.ExchangeService 

#These are your O365 credentials
$Service.Credentials = New-Object Microsoft.Exchange.WebServices.Data.WebCredentials($mail,$password)

# this TestUrlCallback is purely a security check
$TestUrlCallback = {
    param ([string] $url)
    if ($url -eq "https://autodiscover-s.outlook.com/autodiscover/autodiscover.xml") {$true} else {$false}
}
# Autodiscover using the mail address set above
$service.AutodiscoverUrl($mail,$TestUrlCallback)

# get a handle to the inbox
$inbox = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,[Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox)

$MailboxRootid = new-object  Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Root, $email) # selection and creation of new root
$MailboxRoot = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$MailboxRootid)

$fvFolderView = new-object Microsoft.Exchange.WebServices.Data.FolderView(100) #page size for displayed folders
$fvFolderView.Traversal = [Microsoft.Exchange.WebServices.Data.FolderTraversal]::Deep; #Search traversal selection Deep = recursively
$SfSearchFilter = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo([Microsoft.Exchange.WebServices.Data.FolderSchema]::Displayname,$USER_DEFINED_FOLDER_IN_MAILBOX) #for each folder in mailbox define search
$findFolderResults = $MailboxRoot.FindFolders($SfSearchFilter,$fvFolderView) 

$ArchiveFolder = ""


# create Property Set to include body and header of email
$PropertySet = New-Object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties)


# set email body to text
$PropertySet.RequestedBodyType = [Microsoft.Exchange.WebServices.Data.BodyType]::Text;


# This next loop successfully finds my folder, but it is an inefficient way 
# to do it.  It's ok, because there's not that many folders, but there's tens 
# of thousands of emails to search through in the folder itself, and that will
# need a more efficient search.
foreach ($Fdr in $findFolderResults.Folders)
{
    $theDisplayName = $Fdr.DisplayName
    if($theDisplayName -eq $USER_DEFINED_FOLDER_IN_MAILBOX)
    {
        $ArchiveFolder = $Fdr
    }
}

# Now to actually try and search through the emails in my $ArchiveFolder (the hard way)
$textToFindInSubject = "Remove"

$emailsInFolder = $ArchiveFolder.FindItems(9999)   # <-- Successfully finds ALL emails with no filtering, requiring iterative code to find the ones I want.
foreach($individualEmail in $emailsInFolder.Items)
{
    if($individualEmail.Subject -match "$textToFindInSubject")
    {       
        # found the email i want -  but a super inefficient
        # way to do it
        echo "Successfully found the email!"
    }
}

$searchfilter = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+ContainsSubstring([Microsoft.Exchange.WebServices.Data.EmailMessageSchema]::Subject,$textToFindInSubject)     
$itemView = new-object Microsoft.Exchange.WebServices.Data.ItemView(999)
$searchResults = $service.FindItems($ArchiveFolder.ID, $searchfilter, $itemView)

foreach($result in $searchResults)
{
    $result.Load($PropertySet)
    $subj = $result.Subject

    echo "Subject"$subj
    echo "Body: $($result.Body.Text)"
}