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 = "[email protected]"
$To = "[email protected]"

[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 = "smtp.office365.com"
    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. 
[ME2PR01CA0139.ausprd01.prod.outlook.com]
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

<?php

//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 = "[email protected]";
$from = '[email protected]';

$appPassword = 'my_app_password_here';

$to = '[email protected]';

//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       = 'smtp.office365.com';                     //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`

  //Recipients
  $mail->setFrom($from, 'Joe User');
  $mail->addAddress($to);               //Name is optional
  //Content
  $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';

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

0 Comments

Submit a Comment

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

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

The reCAPTCHA verification period has expired. Please reload the page.

You May Also Like…

List local disks

GET-CimInstance -query "SELECT * from Win32_DiskDrive" DeviceID Caption Partitions Size Model -------- -------...

Create ISO using Powershell

Usage New-IsoFile -NewIsoFilePath C:\tmp\Hyper-V.iso-sources -ImageName Hyper-V -SourceFilePath 'C:\tmp\Hyper-V'...