PoshCode Archive  Artifact [fe9e48c0cc]

Artifact fe9e48c0cc322b023aaf832e751977bbfd7cb3a3b8992fc20d7c7fc8d94209db:

  • File Get-Exchange-Mail.ps1 — part of check-in [4c8a1ba3fd] at 2018-06-10 14:18:01 on branch trunk — Connect to an exchange mailbox and get your latest emails. (user: George Mauer size: 1405)

# 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: 6573
# x-archived: 2016-12-22T03:51:16
# x-published: 2016-10-13T05:22: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