Facebook X (Twitter) Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook X (Twitter) Instagram
    TecAdmin
    You are at:Home»Windows Tutorials»How to Send Emails from the Windows Command Line with PowerShell

    How to Send Emails from the Windows Command Line with PowerShell

    By RahulApril 12, 20233 Mins Read

    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.

    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:

    1
    2
    3
    4
    $From = "[email protected]"
    $To = "[email protected]"
    $Subject = "This is the subject of the email"
    $Body = "This is the body of the email"

    Replace “[email protected]” with the email address of the sender and “[email protected]” 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:

    1
    2
    3
    4
    $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:

    1
    2
    3
    4
    5
    $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:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    # Define the email properties
    $From = "[email protected]"
    $To = "[email protected]"
    $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.

    Email PowerShell smtp windows
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    What is SMTP (Simple Mail Transfer Protocol)?

    A Step-by-Step Guide to Installing Nginx on Windows

    Python Script to Send an Email via SMTP Server

    View 8 Comments

    8 Comments

    1. David on November 8, 2022 5:59 pm

      This actually works,
      You however have to create an app password for you to be able to use your gmail account

      Reply
    2. Adam on August 12, 2022 12:16 am

      It is true, the Send-MailMessage cmdlet is obsolete. Try this instead: https://sourceforge.net/projects/mail-alert/

      Reply
      • Bob on May 10, 2023 10:06 pm

        Sending a single log file is a bit limiting. I suggest CMail (https://www.inveigle.net/cmail) instead as it can handle multiple attachments.

        Reply
    3. egon on December 23, 2019 8:51 pm

      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”

      Reply
    4. Julien Béliveau on June 6, 2019 5:53 pm

      WIN+R –> CMD
      THEN TYPE:
      start mailto:[email protected]?subject=”ANY%20SUBJECTS”

      Reply
    5. Dikshit on April 25, 2019 8:48 am

      Excellent help, thank you.

      Reply
    6. Tim on April 3, 2019 12:12 pm

      Wait, what? Why wouldn’t you use the Send-MailMessage cmdlet, which was introduced in PowerShell 2.0.

      Reply
      • Raffael on September 30, 2021 7:58 am

        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.

        Reply

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • Using .env File in FastAPI
    • Setting Up Email Notifications for Django Error Reporting
    • How to Enable Apache Rewrite (mod_rewrite) Module
    • What are Microservices?
    • Variable Expansion in ZSH
    Facebook X (Twitter) Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.