sending mail in powershell: mail message objects

I’ve made a previous post about sending emails in PowerShell. Some additional notes I have found include creating the mail message as an object rather than straight strings. I also wanted to make multi-lined emails (carriage return, line feed, second line…), which seems easier when creating the message as an object. One could properly declare the email address string as a mail address object, but I just let PowerShell auto convert it for me.

$smtp = new-object Net.Mail.SmtpClient
$smtp.DeliveryMethod = “PickupDirectoryFromIis”
$objMailMessage = New-Object System.Net.Mail.MailMessage
$objMailMessage.From = “michael@server.com”
$objMailMessage.To.Add(“michael@server.com”)
$objMailMessage.Subject = “Subject line.”
$objMailMessage.Body = “Hello `nThis is a second line.”
$smtp.send($objMailMessage)