In this tutorial, we will learn how to send emails using PowerShell command lines or using a PowerShell script.
This tutorial is useful for anyone who wants to send emails automatically without using the browser or without using any application.
Sometimes it is necessary to create automatic alerts and send information about these events to your email or to other people’s emails. There are several ways to do this, on this tutorial we will use SMTP servers. These servers allow you to access the email account from which the email will be sent. Depending on the email account you will be using to send the email, the SMTP server will differ for each company or identity. For instance, the SMTP server of Gmail is smtp.gmail.com.
The first commands are very simple commands because they only create variables, objects and store the current date-time. The last one is not mandatory, it is only used for debugging and to demonstrate correct operationality.
$EmailFrom = "Source mail" # Put source mail between " "
$EmailTo = "Destiny mail" # Put destiny mail
$Subject = "Email Subject Here" # Insert the email subject
$Today = (Get-Date).DateTime # Get datetime not mandatory (only for mail information debug)
$Body = "This is mail body and the datetime is: " + $Today # Write message presented in mail
$SMTPServer = "smtp.emailproperty.com" # Put de SMTP server name (in my case is smtp.gmail.com)
The following command lines are responsible for creating the SMTP client object on port 587 and enabling the SSL certificate protocol. The port may be necessary to change depending on the email server that you will be using to send.
$SMTPClient = New-Object Net.Mail.SmtpClient($SMTPServer, 587) #Create object for SMTP server on 587 port
$SMTPClient.EnableSsl = $true # Enable SSL certificate Protocol
The following command lines are responsible for creating the credentials object, then you need to insert your source mail and your password account. This step will not give an error but may be necessary to change the password to an application password for security reasons that I will explain below.
# Create credentials object
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("Source mail", "Mail password");
The next is the last command line, it is responsible for connecting to the SMTP server and sending the mail.
# Send email using the created objects with all necessary data
$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)
It is very likely that you will return a connection error if you have not entered all the data correctly or if the server does not allow you to connect in this way. If this last hypothesis happens then you should go to your email account and lower the security levels. If you don´t want to lower the security levels of your account, then you must create an application password and insert this password in the credentials command line. If you need help, please comment below.
The next code block is the summary of all the code you need, you can copy and paste this code into a file with extension “.ps1“.
$EmailFrom = "Source mail" # Put source mail between " "
$EmailTo = "Destiny Mail" # Put destiny mail
$Subject = "Email Subject Here" # Insert the email subject
$Today = (Get-Date).DateTime # Get datetime not mandatory (only for mail information debug)
$Body = "This is mail body and the datetime is: " + $Today # Write message presented in mail
$SMTPServer = "smtp.emailproperty.com" # Put de SMTP server name (in my case is smtp.gmail.com)
$SMTPClient = New-Object Net.Mail.SmtpClient($SMTPServer, 587) #Create object for SMTP server on 587 port
$SMTPClient.EnableSsl = $true # Enable SSL certificate Protocol
# Create credentials object
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("Source mail", "your password");
# Send email using the created objects with all necessary data
$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)
You can use the commands directly on the command line by copying and pasting each command to the PowerShell window and press “Enter”, or you can use these commands in script form. The last way is powerful if you want to call this script automatically, so you can copy and paste it into a file with the extension “.ps1”. This type of extension is used for PowerShell scripts, for your information.
In the first time that you use these commands, I recommend using a command at a time, inserting it in the PowerShell window, and pressing “Enter”, because some unexpected errors may occur, and you will be able to read the error and understand what is happenning.