none
Send Emails with PowerShell using .CSVs and HTML templates RRS feed

  • 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

All replies