Quantcast
Channel: Active questions tagged email - Stack Overflow
Viewing all articles
Browse latest Browse all 29935

Send-MailMessage PowerShell Not Authenticating

$
0
0

I am trying to send emails using the Send-MailMessage cmdlet. The eventual goal is to send emails to a list of users from a CSV. However, I am having trouble sending a single email using this cmdlet. The email I am sending from is an O365 email address and I have looked up the correct ports and smtp server name to use but for some reason I get this following error:

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 [BY5PR13CA0016.namprd13.prod.outlook.com] At line:1 char:1 + Send-MailMessage -To '**************' -From $SenderAddress -Subje ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (System.Net.Mail.SmtpClient:SmtpClient) [Send-MailMessage], SmtpException + FullyQualifiedErrorId : SmtpException,Microsoft.PowerShell.Commands.SendMailMessage

Posted below is my code. Does anyone have any idea of what I am doing wrong?

 #Sending Emails using PowerShell. 

#Note: remove spaces in Display Name column and SAM Account Name column. For simplicity the Display Name field has been renamed to DisplayName
#And the SAM account Name field has been renamed to SAM.

#Setting the default SMTPServer
$SmtpServer = 'smtp.office365.com'
#Prompt for the senders email address
$SenderAddress = Read-Host -Prompt "Enter Sender's Email Address: "
$SenderPassword = Read-Host -Prompt "Enter your password: "
#Prompt for Recipient Email Address
$RecepientAddress = Read-Host -Prompt "Enter the recipient's address: "
#Setting up authentication to SMTP server
$Credentials = New-Object System.Management.Automation.PSCredential -ArgumentList $SenderAddress, $($SenderPassword | ConvertTo-SecureString -AsPlainText -Force)

#Composing the email. 
$SubjectLine = "Test Email."
$EmailBody = "Testing CMDlet"

Send-MailMessage -To $RecepientAddress -From $SenderAddress -Subject $SubjectLine -Body $EmailBody -BodyAsHtml -Priority High -DeliveryNotificationOption OnFailure, OnSuccess -SmtpServer $SmtpServer -Port 587 -UseSsl -Credential $Credentials

Viewing all articles
Browse latest Browse all 29935

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>