question

MarioonMicrosoft-1066 avatar image
0 Votes"
MarioonMicrosoft-1066 asked AndyDavid edited

Send email to O365 using Powershell when MFA is enabled

Hi,

I want to create a Powershell script to send an email to my O365 mailbox when MFA is enabled. Eventually this script will be run from different applications as from a UPS application.

I am using Direct Send to O365. I am sure all the parameters are good. I receive as error: "Mailbox unavailable. The server response was: 5.7.1 Service ....".

Can someone tell me what I am doing wrong?

My Powershell script:

Create credentials

$username = "xxxxxx@yyyyyy"
$password = "pppppppp"
$myPwd = ConvertTo-SecureString -string $password -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential -argumentlist $username, $myPwd

Build email parameters

$mailParams = @{
SmtpServer = 'xxxxxxxx.mail.protection.outlook.com'
Port = '25'
UseSSL = $true
From = 'xxxxxx@yyyyyy'
To = 'xxxxxx@yyyyyy'
Subject = "Subject: Test Direct send "
Body = "Body: Direct send"
Credential = $cred
}

Send the email

Send-MailMessage @mailParams



office-exchange-server-mailflow
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

AndyDavid avatar image
0 Votes"
AndyDavid answered AndyDavid edited

Don't use a MFA enabled account.

Either user a service account that doesnt have MFA applied (Yuck)

or use the App only method with Exchange Powershell and authenticate with a certificate (YAA!)

https://docs.microsoft.com/en-us/powershell/exchange/app-only-auth-powershell-v2?view=exchange-ps

or...
Send the messages anonymously from the application

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.