I have a script that is working 100% without any problems for 2 legacy users in our Office365 environment. We have created a new "Service Account" to use and it is failing while using the exact same script.
Doing some research, I have seen and addressed the following points:
- Credential and Password can login to both outlook.office365.com and our domain via RDP
- I have reset the password on the account and re-syncd with Office365
- I have verified that we are using the UPN and not just the SMTP name
- I have verified the mailbox delegation settings have explicitly named the UPN instead of assuming on the NT AUTHORITY\SELF default (screenshot linked below)
- SSL is enabled in the script
- We have elevated the account to 'Domain Admin' for testing purposes
- I logged into Windows using the account and then ran the script to ensure there wasn't a weird issue like the ISE still using my credentials
As you can see with the last 2 items, we are kind of grasping at straws at this point and could seriously use some help! The nightmare for us is that it is working with 2 other accounts exactly as is (with only changing the Username, Password, and From variables)
Here is the script:
$username = "[email protected]"
$password = "PASSWORD" | convertto-securestring -AsPlainText -Force
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $password
$smtp = "smtp.office365.com"
$port = "587"
$from = "[email protected]"
$to = "[email protected]"
$subject = "This is my Subject Line"
$body = "<b>This is the body of my email.</br>With <font color=`"red`">OPTIONAL</font> HTML tags</b>"
Send-MailMessage -SmtpServer $smtp -Port $port -UseSsl -Credential $cred -From $from -To $to -Subject $subject -Body $body -BodyAsHtml -Verbose
And this is the output using the -Verbose switch:
Send-MailMessage : The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM At line:12 char:1 + Send-MailMessage -SmtpServer $smtp -Port $port -UseSsl -Credential $cred -From $ ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (System.Net.Mail.SmtpClient:SmtpClient) [Send-MailMessage], SmtpException + FullyQualifiedErrorId : SmtpException,Microsoft.PowerShell.Commands.SendMailMessage