User PowerShell to send a test email that allows you to insert custom header information

Scenario:  You want to test the ability to send a test email that includes custom header information within the message.  You plan on using this custom header information to perform a logical action based off the header value.

Scriptlet:

[string[]] $To = "steve@domain.com","chris@domain.com”
[string] $Subject = "Testing for custom header info"
[string] $Body = "testing 123 "
[string] $SmtpHost = "smtp.domain.com"
[string] $From = steve@domain.com
$email = New-Object System.Net.Mail.MailMessage
foreach($mailTo in $to)
{
    $email.To.Add($mailTo)
}
$email.From = $from
$email.Subject = $subject
$email.Body = [string]::Format("{0}",$body);
$email.Headers.Add("X-Test", "true");
$email.Headers.Add("X-Test2", "false");
## Send the mail
$client = New-Object System.Net.Mail.SmtpClient $smtpHost
$client.UseDefaultCredentials = $true
$client.Send($email)
Advertisement

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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: