Sending email with PowerShell is easy with Send-MailMessage. Here is a basic example:
$server = "smtp.sendgrid.net" $Username ="test" $Password = ConvertTo-SecureString "Password" -AsPlainText -Force $credential = New-Object System.Management.Automation.PSCredential $Username, $Password $from = "[email protected]" $to = "[email protected]" $subject = "Test Message" $mailbody = @" Hi Paulie, This is a test message. See you soon "@ Send-MailMessage -smtpServer $server -Credential $credential -Usessl -Port 587 -from $from -to $to -subject $subject -Body $mailbody
The PowerShell example above sends the email via Sendgrid and works perfectly. But the problem is that the “from” address appears exactly as “[email protected]” and there is no obvious way to specify the display name.
Specifying the Display Name with Powershell
To specify the display name when sending email via Powershell. You can build the from address by creating a System.Net.Mail.MailAddress Like this:
$from = new-object System.Net.Mail.MailAddress("[email protected]", "Paulie")
During my testing whenever I sent a message without a display name, the received message went straight to my “clutter” folder in Outlook. But with the display name included in came to my inbox.
Carlos Gallardo says
Works!
Tony Sheen says
Brilliant – thank you for taking the time to post this !
Nikhil Kadi says
Just putting From mail in below format works just fine too even in Send-MailMessage
‘Nikhil Kadi ‘
Nikhil Kadi says
“Nikhil Kadi “
shafic says
Paul, looking for this, works! thanks
mbourgon says
This is awesome. Was trying to figure out how to do it since our corporate email server is pretty strict about how to send.