Sending emails is an essential task in today’s digital age, and as a system administrator, you may need to send emails from the command line to automate this task. In this article, we’ll show you how to send emails from the Windows command line with PowerShell, streamlining your email communication and improving your productivity.

Advertisement

Prerequisites

Before we begin, make sure that you have the following:

  • A Windows computer with PowerShell installed
  • Access to an SMTP server with your login credentials
  • The SMTP server address and port number

Step 1: Open PowerShell

First, open PowerShell by searching for “PowerShell” in the Start Menu or by pressing the Windows key + X and selecting “Windows PowerShell” from the menu.

Step 2: Define the Email Properties

Next, define the properties of your email, including the sender, recipient, subject, and body. Use the following commands to do this:


$From = "sender@example.com"
$To = "recipient@example.com"
$Subject = "This is the subject of the email"
$Body = "This is the body of the email"

Replace “sender@example.com” with the email address of the sender and “recipient@example.com” with the email address of the recipient. You can also change the subject and body of the email to whatever you like.

Step 3: Define the SMTP Server Details

Now, define the SMTP server details, including the server address, port number, and credentials. Use the following commands to do this:


$SMTPServer = "smtp.example.com"
$SMTPPort = 587
$SMTPUsername = "username"
$SMTPPassword = "password"

Replace “smtp.example.com” with the address of your SMTP server, and change the port number to the appropriate value for your server. Replace “username” and “password” with the credentials for your SMTP server.

Step 4: Create and Send the Email

Finally, use the following commands to create and send the email:


$Email = New-Object System.Net.Mail.MailMessage($From, $To, $Subject, $Body)
$SMTPClient = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential($SMTPUsername, $SMTPPassword)
$SMTPClient.Send($Email)

This will create a new email object, set the SMTP client details, and then send the email using the Send method.

Complete PowerShell Script to Send the Email

Here’s the complete example of the PowerShell script that will send an email via SMTP:


# Define the email properties
$From = "sender@example.com"
$To = "recipient@example.com"
$Subject = "This is the subject of the email"
$Body = "This is the body of the email"

# Define the SMTP server details
$SMTPServer = "smtp.example.com"
$SMTPPort = 587
$SMTPUsername = "username"
$SMTPPassword = "password"

# Create the email object
$Email = New-Object System.Net.Mail.MailMessage($From, $To, $Subject, $Body)

# Set the SMTP client details and send the email
$SMTPClient = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential($SMTPUsername, $SMTPPassword)
$SMTPClient.Send($Email)

Here’s what each section of the script does:

  • # Define the email properties: This section defines the sender, recipient, subject, and body of the email.
  • # Define the SMTP server details: This section defines the address, port number, and login credentials of the SMTP server that will be used to send the email.
  • # Create the email object: This section creates a new email object using the properties defined in the first section.
  • # Set the SMTP client details and send the email: This section sets the SMTP client details, including SSL and login credentials, and sends the email using the Send method.

To use this script, simply replace the placeholders with your own email properties and SMTP server details. Once you’ve done that, save the script with a .ps1 extension, and then run it from the PowerShell console or Windows Command Prompt by typing .\scriptname.ps1 (where scriptname is the name you saved the file as).

By using this script, you can easily send emails via SMTP without having to manually open your email client, saving you time and making your email communication more efficient.

Conclusion

In conclusion, sending emails from the Windows command line with PowerShell is a simple and effective way to streamline your email communication and improve your productivity as a system administrator. By following the steps outlined in this article, you can quickly and easily send emails from your Windows computer using PowerShell, saving time and improving your workflow.

Remember to replace the email addresses, subject, body, SMTP server details, and credentials with the appropriate values for your email communication needs. With this powerful tool at your disposal, you can automate your email tasks and streamline your workflow, making you a more efficient and productive system administrator.

Share.

8 Comments

  1. this script does not work anymore

    Exception calling “Send” with “4” argument(s): “The SMTP server requires a secure connection or the client was not
    authenticated. The server response was: 5.7.0 Authentication Required. Learn more at”

    • Warning

      The Send-MailMessage cmdlet is obsolete. This cmdlet does not guarantee secure connections to SMTP servers. While there is no immediate replacement available in PowerShell, we recommend you do not use Send-MailMessage. For more information, see Platform Compatibility note DE0005.

Leave A Reply


Exit mobile version