Send-MailMessage not able to send an email through Office 365 using App Password

Written by James McDonald

June 24, 2021

Send-MailMessage & App Password Doesn’t Work

So today I tried to test SMTP Auth using Powershell

$appPassword = 'my_app_password_here'

$From = ""
$To = ""

[securestring]$secStringPassword = ConvertTo-SecureString $appPassword -AsPlainText -Force

[pscredential]$msolcred = New-Object System.Management.Automation.PSCredential -ArgumentList $From, $secStringPassword

$myargs = @{
    From = $From
    To = $To
    Subject = "$(Get-Date -Format (Get-Culture).DateTimeFormat.SortableDateTimePattern) test from $($From) to $($To)"
    Body = "Hello Please blinking heck I hope you get this Richard"
    SmtpServer = ""
    Credential = $msolcred
    Port = "587"
    UseSsl = $true

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

Send-MailMessage  @myargs

But it threw an error every time:

Send-MailMessage : The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.57 Client not authenticated to send mail. 
Send-MailMessage works with the user password but not an App Password

Interestingly if I used the Office 365 Users actual password it would work! So my diagnosis is that Send-MailMessage is trying to network style windows credential auth and not doing the SMTP “AUTH LOGIN”

So plan B was try an open source alternative

Use PHPMailer to Test SMTP Auth Against Office 365

So PHPMailer to the rescue. I think this works because it doesn’t try to do a Microsoft login but uses AUTH LOGIN


//Import PHPMailer classes into the global namespace
//These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

//Load Composer's autoloader
require 'vendor/autoload.php';

$relayAccount = "";
$from = '';

$appPassword = 'my_app_password_here';

$to = '';

//Create an instance; passing `true` enables exceptions
$mail = new PHPMailer(true);

try {
  //Server settings
  $mail->SMTPDebug = SMTP::DEBUG_SERVER;                      //Enable verbose debug output
  $mail->isSMTP();                                            //Send using SMTP
  $mail->Host       = '';                     //Set the SMTP server to send through
  $mail->SMTPAuth   = true;                                   //Enable SMTP authentication
  $mail->Username   = $relayAccount;                     //SMTP username
  $mail->Password   = $appPassword;                               //SMTP password
  //$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;            //Enable implicit TLS encryption
  $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;            //Enable implicit TLS encryption
  $mail->Port       = 587;                                    //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`

  $mail->setFrom($from, 'Joe User');
  $mail->addAddress($to);               //Name is optional
  $mail->isHTML(true);                                  //Set email format to HTML
  $mail->Subject = 'Here is the subject regarding N-Central Relay';
  $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
  $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

  echo 'Message has been sent';
} catch (Exception $e) {
  echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";


Submit a Comment

Your email address will not be published. Required fields are marked *

You May Also Like…


This is a graphic I use to figure out which section to add the firewall rules to in the Unifi UDM/USG controller....