# encoding: ascii
# api: powershell
# title: Get-Exchange-Mail.ps1
# description: Connect to an exchange mailbox and get your latest emails.
# version: 1.1
# author: George Mauer
# license: CC0
# x-poshcode-id: 4279
# x-archived: 2015-11-24T08:32:46
# x-published: 2015-06-28T17:53:00
#
# This particular example connects to Microsoft Online Services
# Requires the installation of Exchange Web Services API:
# Docs: http://msdn.microsoft.com/en-us/library/dd637749(v=exchg.80).aspx
# Download: http://www.microsoft.com/downloads/en/details.aspx?FamilyID=c3342fb3-fbcc-4127-becf-872c746840e1
# Will return the full mail objects. You can get them into a consumable format as follows:
# $mm = .\Get-Mail.ps1
# $mm | ft -Property Sender,Subject,Body
#
[Reflection.Assembly]::LoadFile("C:\Program Files\Microsoft\Exchange\Web Services\1.1\Microsoft.Exchange.WebServices.dll") | Out-Null
$s = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2007_SP1)
$s.Credentials = New-Object Net.NetworkCredential('email@domain', 'password')
$s.Url = new-object Uri("https://red001.mail.microsoftonline.com/ews/exchange.asmx")

$inbox = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($s,[Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox)
$mails = $inbox.FindItems(5) 
$mails | % {$_.Load()}
$mails
