Answered by:
Send Emails with PowerShell using .CSVs and HTML templates

Question
-
###################################################################### $mailfrom = "first.last@company.com" $mailserver = "smtp.office365.com" $mailcredentials = Get-Credential -UserName $mailfrom -Message "please enter your mail password" $csvInfo = import-csv "C:\Users\currentuser\Desktop\scripts\sendemails\recipients.csv" $templateInfo = Get-Content "C:\Users\currentuser\Desktop\scripts\sendemails\emailtemplate.html" -raw ###################################################################### foreach($row in $csvINfo){ $sendmailtoCC = @() $sendmailtoCC = $(($row.cc).split(";").trim()) Write-Host "sending email to $($row.name)" write-host "CCing $sendmailtoCC" $messagebody = $templateInfo $messagebody = $messagebody.Replace("TAG_NAME", $row.name) Send-MailMessage -from $mailfrom -to $row.to -Cc $sendmailtoCC -subject $row.reason -Body $messagebody -SmtpServer $mailserver -Port 587 -Credential $mailcredentials -UseSsl } Get-Date -Format G Write-Host "`nDone" -ForegroundColor Green
I have only changed the company and first and last name, rest of the script is the same. What am I doing wrong/ why wont this work? It was working before. I didn't make any changes on it, unless if did without knowing and then it saved somehow. Still new at these, went through it all to see if I could figure it out but can't.
This is the error I get:
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 [MWHPR22CA0028.namprd22.prod.outlook.com]
At C:\Users\foustiiig\Desktop\LEXMARK\Send Emails\SendEmailsandCC.ps1:28 char:5
+ Send-MailMessage -from $mailfrom -to $row.to -Cc $sendmailtoCC -s ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.Mail.SmtpClient:SmtpClient) [Send-MailMessage], SmtpException
+ FullyQualifiedErrorId : SmtpException,Microsoft.PowerShell.Commands.SendMailMessage- Moved by litdev Wednesday, September 5, 2018 6:03 PM
Wednesday, September 5, 2018 12:31 PM
Answers
-
I'd ask for help over here.
https://social.technet.microsoft.com/Forums/en-US/home?forum=winserverpowershell
Regards, Dave Patrick ....
Microsoft Certified Professional
Microsoft MVP [Windows Server] Datacenter Management
Disclaimer: This posting is provided "AS IS" with no warranties or guarantees, and confers no rights.- Proposed as answer by Richard MuellerMVP Thursday, September 6, 2018 12:02 AM
- Marked as answer by Richard MuellerMVP Wednesday, September 12, 2018 1:19 PM
Wednesday, September 5, 2018 6:58 PM
All replies
-
Hi There.
You have asked your question in Small Basic forum instead of Powershell forum. But still, I have found a thread which can send formatted HTML email in Powershell.
Thread = https://community.spiceworks.com/topic/1964063-send-formatted-html-e-mail-in-powershell
Roshan - 8th Grade Student (Age 12) - Learning Small Basic, Visual Basic and C#
Wednesday, September 5, 2018 1:42 PM -
I'd ask for help over here.
https://social.technet.microsoft.com/Forums/en-US/home?forum=winserverpowershell
Regards, Dave Patrick ....
Microsoft Certified Professional
Microsoft MVP [Windows Server] Datacenter Management
Disclaimer: This posting is provided "AS IS" with no warranties or guarantees, and confers no rights.- Proposed as answer by Richard MuellerMVP Thursday, September 6, 2018 12:02 AM
- Marked as answer by Richard MuellerMVP Wednesday, September 12, 2018 1:19 PM
Wednesday, September 5, 2018 6:58 PM