Send Email using PowerShell from CSV data file

Just finished phase 1 of a project which required sending out an automated survey email to a dynamic list of people. The email message is the same in all of these except for an ID number embedded in a link within the message. Currently the list of emails along with the ID is automatically dumped out of the database nightly into a CSV file so I didn’t need to do anything there just yet.

Here’s the PowerShell script that I pieced together from various sites to get this to work

$EmailFileLocation = “d:\scripts\email\dailyemaillist.csv”
function SendSurvey($SendTo,$EmailID)
$SmtpClient = new-object
$MailMessage = New-Object
$SmtpClient.Host = “”
$FromAddress = new-object System.Net.Mail.MailAddress(“”, “Your Name”)
$mailmessage.sender = $FromAddress
$mailmessage.from = $FromAddress
$mailmessage.Subject = “Your Message Subject Here”
$mailmessage.IsBodyHtml = 1

# The line below will pull the message body from the specified file
# This is currently commented out to have the message body created within this script but can be changed easily.
# $mailmessage.Body = Get-Content .\emailbody.htm
$mailmessage.Body = “<html><body>
<p>This is an example message.</p>
<p>If this had been an actual message there would be something worth reading here.</p>
<p>bla bla bla…here’s the link example:
$EmailID </p>
<p><br>End of message</p>
$Credentials = new-object System.Net.networkCredential
$Credentials.UserName =
$Credentials.Password = “Password”
$SMTPClient.Credentials = $Credentials
$SMTPClient.Port = 25

$Guests = import-csv $EmailFileLocation -erroraction stop
foreach ($guest in $guests) {
SendSurvey $ $guest.number

Hope this helps. One other note, if you’re setting this script to run automatically, you will either need to sign the script or change the permissions in PowerShell to allow it to run. You can use the following command to make the change:

set-executionpolicy RemoteSigned

Last but not least, here’s a few of the sites I found info on to get this working:

Explore posts in the same categories: PowerShell, Tech

One Comment on “Send Email using PowerShell from CSV data file”

  1. Jeffrey Snover Says:

    You are going like CTP 3 (hopefully this week). When you get it, check out the new Send-mailMessage cmdlet:

    Sends an e-mail message.
    (1)-Subject | -sub
    (2)[-Body ]
    (3)[-SmtpServer | -ComputerName ]
    [-Attachments | -PsPath (ByValue)]
    [-Bcc ]
    [-BodyAsHtml | -BAH ]
    [-Cc ]
    [-Credential ]
    [-DeliveryNotificationOption | -DNO ]
    [-Encoding | -BE ]
    [-Priority ]
    [-UseSsl ]

    You can set an variable $PSEmailServer and that will be used if you don’t specify one.

    I would greatly appreciate it if you bashed the heck out of that command and let me if it is something you would use or not and if not, why not.

    Jeffrey Snover [MSFT]
    Windows Management Partner Architect
    Visit the Windows PowerShell Team blog at:
    Visit the Windows PowerShell ScriptCenter at:

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: