I am trying to write a script for automated mail with PowerShell. Below is my script. I don't why I am getting authentication error. I am suspecting that I might need to add certificate. for testing purpose I have self signed certificate on my local machine but I am not sure how to add it in this script. If it's not certificate then please suggest me why i am getting this error and what is the solution.
PS:- I have looked into other stackoverflow answer but none helped.
# Define clear text string for username and password
[string]$userName = '[email protected]'
[string]$userPassword = 'xxxx@paxxxword'
# Convert to SecureString
[securestring]$secStringPassword = ConvertTo-SecureString $userPassword -AsPlainText -Force
# Get the credential
[pscredential]$credential = New-Object System.Management.Automation.PSCredential ($userName, $secStringPassword)
#$credential = Get-Credential
## Define the Send-MailMessage parameters
$mailParams = @{
SmtpServer = 'smtp.outlook.com'
Port = '587' # or '25' if not using TLS
UseSSL = $true ## or not if using non-TLS
Credential = $credential
From = '[email protected]'
To = '[email protected]'
Subject = "testing mail with powershell - $(Get-Date -Format g)"
Body = 'This is a test email using SMTP Client Submission'
DeliveryNotificationOption = 'OnFailure', 'OnSuccess'
}
## Send the message
Send-MailMessage @mailParams
Below is the error I am getting:-
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
[ZR0P278Cxxxx.xxxP278.PROD.OUTLOOK.COM]
At line:26 char:1
+ Send-MailMessage @mailParams
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.Mail.SmtpClient:Sm
tpClient) [Send-MailMessage], SmtpException
+ FullyQualifiedErrorId : SmtpException,Microsoft.PowerShell.Commands.Send
MailMessage
question from:
https://stackoverflow.com/questions/65932812/send-mailmessage-the-smtp-server-requires-a-secure-connection-or-the-client-was 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…