How to send mail SMTP with PowerShell

by Daniel Pham
Published: Updated:

How to send email SMTP with PowerShell? With a Linux user for long time like me, came back to Windows is some new for me.

In working, sometime you will face some stuck like don’t hell know why the application couldn’t send the email. You will want to test the mail connection from your computer, to make sure it’s working.

Okay, now we gonna use PowerShell to test send mail SMTP with authentication.

Get SMTP information first

First of all, we gonna test send an email SMTP with an account to authenticate the mail server. So, you have to get the information like below:

  • Username: username use to login to mail server.
  • Password: your password use to login to mail server.
  • SMTP server: the address of mail server.
  • SMTP Port: the port will connect to test send the email.

For example, I get some like below:

  • Username: usertest
  • Password: Passtest123
  • SMTP server: retail.smtp.com (I use smtp.com)
  • SMTP Port: 25

Send email SMTP with PowerShell

how to send mail smtp with powershell
Send mail SMTP with PowerShell.

Now, you go to execute the follow command, remember to change the values as your prefer:

$username = 'usertest'
$password = 'Passtest123'
$secpasswd = ConvertTo-SecureString $password -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential ($username, $secpasswd)
Send-MailMessage -Credential $mycreds -SmtpServer retail.smtp.com -Port 25 -From usertest@devopslite.com -To ghost@gmail.com -Subject test -Body test
  • $username: this line define the username.
  • $password: this line define the password.
  • $secpasswd: call a function to convert the plaintext password to secret string.
  • $mycreds: make a new object with username and secret password, this one will pass into the Send-MailMessage command.
  • Send-MailMessage: the command use to send the email from the address [email protected] to [email protected]. You can also change the contect follow the option -Subject and -Body.

If you’re not use the smtp port 25, you can change the command above to use the port 587 with TLS connection.

$username = 'usertest'
$password = 'Passtest123'
$secpasswd = ConvertTo-SecureString $password -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential ($username, $secpasswd)
Send-MailMessage -Credential $mycreds -SmtpServer retail.smtp.com -Port 587 -UseSsl -From usertest@devopslite.com -To ghost@gmail.com -Subject test -Body test

Conclusion

Just with some simple commands, you can test to send the email SMTP with PowerShell in Windows. In my demo, I use the Windows 11 OS so it would be working fine with the others.

0 0 votes
Article Rating
Subscribe
Notify of
guest
2 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
Mario
Mario
Guest
1 year ago

Hello, good example. You know how send an email with anonymous authentication.

DevOps Lite is a personal blog specializing in technology with main topics about DevOps, DevSecOps, SRE and System Administrator. Articles are shared for free and contributed to the community.

SUPPORT US

FOLLOW US

Subscribe my Newsletter for new blog posts. Stay updated from your inbox!

© 2021-2024 DevOpsLite.com – All rights reserved.

Please write sources “DevOpsLite.com” when using articles from this website.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

0
Would love your thoughts, please comment.x
()
x

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.