Note
The SmtpClient class used in this article is no longer recommended for new development. See the remarks section of the SmtpClient class reference for details. For new development, consider a library such as MailKit instead.
Sometimes you want to send a lot of emails at once, for example when testing email delivery. In that case, you can quickly send emails from PowerShell using SmtpClient.
But you may also want to avoid sending emails with the same sender address or subject every time. So here is a PowerShell script for that, mostly as a note to myself.
1 | function Get-RandomString($Length) |
Save this as a .ps1 file and run it, and it sends a quick test email. This article does not explain the basics of SmtpClient, so you will need to change the recipient address and SMTP server to match your environment. The sender address uses a domain name that does not actually exist, so whether the email is actually delivered depends on the receiving SMTP server.