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

Send-MailMessage & App Password Doesn't…

Login

Blog History

Send-MailMessage & App Password Doesn't Work

So today I tried to test SMTP Auth using Powershell

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
$appPassword = 'my_app_password_here'
 
$From = "james@toggen.com.au"
$To = "user@example.com"
 
[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:

1
2
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?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 = "james@toggen.com.au";
$from = 'user@toggen.com.au';
 
$appPassword = 'my_app_password_here';
 
$to = 'anotheruser@example.com';
 
//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.