Sending Mail -> CSV -> Exchange

Hello everybody!
There is a file - C:\Temp\Exchange.csv next cut:

#TYPE Selected.System.Management.Automation.PSCustomObject
"TestUsers";"";"Hello! TextTextTextTextTextTextTextText";
"TestUsers3";"";"Hello, User!";
"TestUsers4";"";"Hello! TextTextTextTextTextTextTextText";

Is it possible to implement sending to recipients of the column: “EmailAddresses” with column text content: “Text”?

Of course it is. :wink:
First: Have you tried to search for some examples for tasks like this? I’m sure there are several of them.
Second: What have you tried so far? Please share your code if you have some and explain what’s not working as you may expect it and share the error if there are some.

It’s probably just a copy and paste error but your example CSV content is not valid. You have trailing semikolons at the end of the lines with values.

$csv = Import-Csv -Path "C:\CSV.csv"
$Credential = Get-Credential
        Foreach($Message in $csv){
                $Recipient = $Message.EmailAddress
                $GivenName = $Message.GivenName
                $Surname = $Message.Surname
                $Course = $Message.Course
                $Grade = $Message.Grade
                $Subject = "Thema - $Surname $GivenName"
        Write-Host "Sending email to $GivenName $LastName"
        Write-Host "Email Address: $Recipient"
                            $mailBody = 
                                     <b>Hello,</b> $GivenName.</br>

Send-MailMessage -Body $mailBody -BodyAsHtml -From "" -Cc "" -To $Recipient -Subject $subject -Encoding $([System.Text.Encoding]::UTF8) -Credential $Credential -SmtpServer ""

OK. And what is the question? Do you get some errors? Does it not work as expected?

I’d recommend to read up about splatting in PowerSehll. That could make your code a little easier to read.

$csv = Import-Csv -Path 'C:\CSV.csv'
$Credential = Get-Credential

Foreach ($Message in $csv) {
    $mailBody = 
<b>Hello,</b>  $($Message.GivenName).</br>

    $SendMailMessageParams = @{
        Body       = $mailBody
        BodyAsHtml = $true
        From       = ''
        To         = $Message.EmailAddress
        Cc         = ''
        Subject    = "Thema - $($Message.Surname) $($Message.GivenName))"
        Encoding   = 'UTF8'
        Credential = $Credential
        SmtpServer = ''

    Send-MailMessage @SendMailMessageParams

… of course that’s untested. :wink: so try before with test data!

1 Like