SOLVED

Client authentification failure while sending email from office365 account via powershell

%3CLINGO-SUB%20id%3D%22lingo-sub-2526718%22%20slang%3D%22en-US%22%3EClient%20authentification%20failure%20while%20sending%20email%20from%20office365%20account%20via%20powershell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2526718%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20I'm%20trying%20to%20send%20email%20from%20office365%20account%20in%20powershell%2C%20but%20got%20client%20not%20authenticated%20failure%3A%3C%2FP%3E%3CDIV%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FDIV%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3ESend-MailMessage%20%3A%20The%20SMTP%20server%20requires%20a%20secure%20connection%20or%20the%20client%20was%20not%20authenticated.%20The%20server%20response%20was%3A%205.7.57%20Client%20not%20authenticated%20to%20send%20mail.%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3C!--%20%20%20%20EndFragment%20%20%20%20%20--%3E%3C%2FP%3E%3CP%3EI%20have%20tried%20powershell%20native%20%22Send-MailMessage%22%20and%20MailKit%20and%20got%20same%20errors.%20Really%20need%20your%20help%20to%20figure%20out%20the%20problem.%20Thanks!%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2526718%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EOffice%20365%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2526786%22%20slang%3D%22en-US%22%3ERe%3A%20Client%20authentification%20failure%20while%20sending%20email%20from%20office365%20account%20via%20powershell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2526786%22%20slang%3D%22en-US%22%3ECan%20you%20provide%20the%20exact%20PowerShell%20command%20you%E2%80%99re%20using%20including%20the%20parameters%20you%E2%80%99re%20setting%3F%20Of%20course%20edit%20any%20potential%20sensitive%20info.%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2526911%22%20slang%3D%22en-US%22%3ERe%3A%20Client%20authentification%20failure%20while%20sending%20email%20from%20office365%20account%20via%20powershell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2526911%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F960791%22%20target%3D%22_blank%22%3E%40pvanberlo%3C%2FA%3E%2C%20I'm%20trying%20commands%20below.%20Now%20I%20can%20use%20it%20or%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Fjstedfast%2FMailKit%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%22%3EMailKit%3C%2FA%3E%20to%20send%20email%20after%20turning%20off%20the%20Authenticator%20approval.%26nbsp%3BHow%20can%20I%20achieve%20it%20when%20authenticator%20is%20turning%20on%3F%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%24cred%20%3D%20Get-Credential%0A%24mail%20%3D%20%40%7B%0ASmtpServer%20%3D%20'smtp.office365.com'%0APort%3D'587'%0AUseSSL%3D%24true%0AFrom%3D'user%40domain'%0ATo%3D'targetuser%40emailserver'%0ASubject%3D'subject'%0ABody%3D'body'%0A%7D%0ASend-MailMessage%20%40mail%20%23%20or%20Use%20MailKit%2C%20see%20https%3A%2F%2Fadamtheautomator.com%2Fpowershell-email%2F%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2527262%22%20slang%3D%22en-US%22%3ERe%3A%20Client%20authentification%20failure%20while%20sending%20email%20from%20office365%20account%20via%20powershell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2527262%22%20slang%3D%22en-US%22%3E%3CP%3EThe%20regular%20way%20to%20do%20this%20when%20using%20modern%20authentication%20with%20MFA%20would%20be%20to%20allow%20%E2%80%98app%20passwords%E2%80%99.%20There%20are%20other%20ways%2C%20which%20I%20would%20not%20recommend%2C%20because%20they%20potentially%20lower%20the%20security%20posture.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Factive-directory%2Fuser-help%2Fmulti-factor-authentication-end-user-app-passwords%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Factive-directory%2Fuser-help%2Fmulti-factor-authentication-end-user-app-passwords%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIf%20that%20doesn't%20work%20or%20is%20not%20allowed%20in%20your%20tenant%2C%20other%20methods%20may%20include%3A%3C%2FP%3E%3CP%3E*%20Disabling%20MFA%20(not%20recommended)%3C%2FP%3E%3CP%3E*%20If%20MFA%20is%20enabled%20using%20Azure%20AD%20Conditional%20Access%2C%20exclude%20this%20user%20for%20specifically%20SMTP%20from%20the%20Azure%20AD%20Conditional%20Access%20policy%3C%2FP%3E%3CP%3E*%20If%20you're%20using%20Security%20Defaults%20within%20your%20Microsoft%20365%20tenant%2C%20legacy%20authentication%20is%20blocked%20by%20default%20and%20can't%20be%20bypassed%20unless%20you%20disable%20Security%20Defaults%3C%2FP%3E%3CP%3E*%20You%20can%20use%20the%20Graph%20API%20for%20sending%20out%20e-mail%20programatically%20(%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fgraph%2Fapi%2Fuser-sendmail%3Fview%3Dgraph-rest-1.0%26amp%3Btabs%3Dhttp%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3ESend%20mail%20-%20Microsoft%20Graph%20v1.0%20%7C%20Microsoft%20Docs%3C%2FA%3E)%3C%2FP%3E%3C%2FLINGO-BODY%3E
New Contributor

Hi I'm trying to send email from office365 account in powershell, but got client not authenticated failure:

 

 

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.

 

I have tried powershell native "Send-MailMessage" and MailKit and got same errors. Really need your help to figure out the problem. Thanks!

 

4 Replies
Can you provide the exact PowerShell command you’re using including the parameters you’re setting? Of course edit any potential sensitive info.

@pvanberlo, I'm trying commands below. Now I can use it or MailKit to send email after turning off the Authenticator approval. How can I achieve it when authenticator is turning on?

$cred = Get-Credential
$mail = @{
SmtpServer = 'smtp.office365.com'
Port='587'
UseSSL=$true
From='user@domain'
To='targetuser@emailserver'
Subject='subject'
Body='body'
}
Send-MailMessage @mail # or Use MailKit, see https://adamtheautomator.com/powershell-email/

 

best response confirmed by remberme (New Contributor)
Solution

The regular way to do this when using modern authentication with MFA would be to allow ‘app passwords’. There are other ways, which I would not recommend, because they potentially lower the security posture.

 

https://docs.microsoft.com/en-us/azure/active-directory/user-help/multi-factor-authentication-end-us...

 

If that doesn't work or is not allowed in your tenant, other methods may include:

* Disabling MFA (not recommended)

* If MFA is enabled using Azure AD Conditional Access, exclude this user for specifically SMTP from the Azure AD Conditional Access policy

* If you're using Security Defaults within your Microsoft 365 tenant, legacy authentication is blocked by default and can't be bypassed unless you disable Security Defaults

* You can use the Graph API for sending out e-mail programatically (Send mail - Microsoft Graph v1.0 | Microsoft Docs)

@remberme 

 

I tested using outlook of my personal email. Try to fill with 365 SMTP:

$cred = Get-Credential
$SMTPServer = "smtp-mail.outlook.com"
$Port = "587"
$From = "user@domain"
$To = "targetuser@emailserver”
$Subject = "subject"
$Body = "Body"
Send-MailMessage -From $From -to $To -Subject $Subject -Body $Body -BodyAsHtml -SmtpServer $SMTPServer -Port $Port -UseSsl -Credential $cred