Skip to content

Send mail from an Outlook.com account

Jeremy edited this page Dec 19, 2020 · 6 revisions

⚠ DRAFT

Send mail from an Outlook.com account

In this example, we will use New-MSGraphAccessToken to obtain an access token including the mail.send scope (i.e. delegated permission), and we'll do so using the Device Code authorization flow. There is no pre-setup required, other than having an Outlook.com account and Windows PowerShell 5.1 (or PowerShell Core 6+). Once we have the access token in hand, we'll use New-MSGraphRequest to send our sendMail requests to Microsoft Graph. The message will be sent from your Outlook.com account just as though you had sent it using OWA or the Outlook desktop client.

ℹ To save you needing to create your own application in your own Azure AD, I'll supply you with the ApplicationId from my MSGraphPSEssentials application, which is registered as a multi-tenant app in my Azure AD tenant. Don't worry, you aren't granting me or my Azure AD tenant access to your Outlook.com account by using my app. Rather, you'll be the only one with the access token (and refresh token, optionally) and your PowerShell session is where all activity will be carried out. Once finished, simply close your PowerShell console and wipe the permissions off your Outlook.com account via https://microsoft.com/consent.

Step 1 - Install the MSGraphPSEssentials module

Install-Module MSGraphPSEssentials -Scope CurrentUser

If you've never installed a module this way, you will be prompted to approve a couple of things, as the PSGallery repository won't be configured as Trusted. We've chosen to install the module into the CurrentUser scope, so no administrative access will be required.

Step 2 - Obtain an access token using New-MSGraphAccessToken

$TokenObject = New-MSGraphAccessToken -ApplicationId '0c26a905-7c94-4296-aeb4-b8925cb7e036' -Scopes mail.send

Follow the instructions, which should look something like this:

Authorization started (expires at 2020-12-19 03:01:44 PM)
To sign in, use a web browser to open the page https://microsoft.com/devicelogin and enter the code FB228GRH7 to authenticate.
[D] Done  [?] Help (default is "D"):

Step 3 - Prepare the email message

Step 4 - Send the email message (using New-MSGraphRequest)