18.1. Using System.Net.Mail

System.Net.Mail is a namespace in the .NET 2.0 Framework that provides classes dealing with all the intricacies involved with sending e-mail. Those classes know how to create a mail message, how to encode it and its attachments, and how to talk the server into sending it to the correct recipients. The basic mail sending functionality is quite simple to achieve: we create a MailMessage object; set it up with the correct recipient address, subject, and body; and pass it to an SmtpClient that actually sends the message over the wire. The following sample script, send-plain.ps1, does exactly that:

#create the mail message $mail = New-Object System.Net.Mail.MailMessage #set the addresses $mail.From = New-Object System.Net.Mail.MailAddress("test@deshev.com") ...

Get Pro Windows PowerShell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.