I need a powershell script to send an email.

Jim 306 Reputation points
2023-03-25T14:56:19.1133333+00:00

Powershell 7.3.3 OR 5.1.22621.963 (I have both) running as admin

Gmail SMTP server

I just want a known good script that somebody has and work from there.

Windows for business Windows Server User experience PowerShell
Windows for business Windows Client for IT Pros User experience Other
{count} vote

Accepted answer
  1. Sedat SALMAN 14,180 Reputation points MVP
    2023-03-25T18:55:54.9233333+00:00

    sorry for the outdated information

    Google offers app passwords because of 2-Step-Verification instead of Less secure app access now you can check the following

    https://support.google.com/accounts/answer/185833

    and Send-MailMessage is obsolete but the other alternatives are not one line by default

    maybe you can use the following

    # Install the MailKit module if it is not already installed
    Install-Module MailKit -Scope CurrentUser
    # Import the MailKit module
    Import-Module MailKit
    # Define email properties
    $From = "******@gmail.com"
    $To = "******@gmail.com"
    $Subject = "Test email"
    $Body = "This is a test email sent from PowerShell."
    # Create a new SMTP client
    $SMTPClient = New-Object MailKit.Net.Smtp.SmtpClient
    # Connect to the Gmail SMTP server
    $SMTPClient.Connect("smtp.gmail.com", 587, [MailKit.Security.SecureSocketOptions]::StartTls)
    # Authenticate with the Gmail SMTP server using the App Password
    $SMTPClient.Authenticate("******@gmail.com", "<App Password>")
    # Create a new email message
    $EmailMessage = New-Object MailKit.Mime.Message
    $EmailMessage.From.Add([MailKit.InternetAddress]::Parse($From))
    $EmailMessage.To.Add([MailKit.InternetAddress]::Parse($To))
    $EmailMessage.Subject = $Subject
    $EmailMessage.Body = New-Object MailKit.Mime.TextPart("plain")
    $EmailMessage.Body.Text = $Body
    # Send the email message
    $SMTPClient.Send($EmailMessage)
    # Disconnect from the Gmail SMTP server
    $SMTPClient.Disconnect($true)
    
    
    2 people found this answer helpful.
    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Sedat SALMAN 14,180 Reputation points MVP
    2023-03-25T14:59:41.83+00:00
    Send-MailMessage -To "recipient@example.com" -Subject "Test Email" -Body "This is a test email." -SmtpServer "smtp.gmail.com" -Port 587 -From "******@gmail.com" -Credential (Get-Credential) -UseSsl
    
    

    he Get-Credential cmdlet prompts you for your Gmail username and password. Make sure to enable "Less secure apps" in your Gmail account settings or create and use an App Password for better security.

    Keep in mind that storing credentials in plain text or hardcoding them in a script is not recommended

    1 person found this answer helpful.
    0 comments No comments

  2. Jim 306 Reputation points
    2023-03-25T15:41:23.22+00:00

    OK, so what I get with that is:

    WARNING: The command 'Send-MailMessage' 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 at this time. See https://aka.ms/SendMailMessage for more information.
    Send-MailMessage:SendEmail.ps1:1
    Line |
       1 |  Send-MailMessage -From "******@domain.com" -To "******@domain.com …
         |  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
         | A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
         | [::ffff:142.251.163.108]:25
    

    So I assume this is because I don't have "Less secure apps" in my Gmail account settings.

    Looks like you can't do that anymore:

    Less secure app access
    To protect your account, apps and devices that use less secure sign-in technology are blocked. To keep your account secure, Google will automatically turn this setting OFF if it’s not being used.
    
    This setting is no longer available. 
    

    Now to use App Password I need to use 2-step verification which I don't want to do because I don't want to add this account to my phone for a number of reasons.

    My conclusion is this is not the solution to my problem and I'll need to more likely use my 365 Small Business account to send the email. I guess I need to find out how to do that as it has 2 step already on it. I am the admin there so I'll start researching how to create an app password.

    Thanks for your help on this.

    If you do have a pointer to a good doc on using 365 Exchange to do this that would be great.

    1 person found this answer helpful.
    0 comments No comments

  3. Emir Besic 5 Reputation points
    2023-10-02T09:35:58.52+00:00

    For Gmail:

    $From = "******@gmail.com"

    $To = "******@domain.com"

    $Subject = "Email subject goes here"

    $Body = "Email body goes here"

    The password is an app-specific password if you have 2-factor-auth enabled

    $Password = "app-specific-password-here" | ConvertTo-SecureString -AsPlainText -Force

    $Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $From, $Password

    Send-MailMessage -From $From -To $To -Subject $Subject -Body $Body -SmtpServer "smtp.gmail.com" -port 587 -UseSsl -Credential $Credential

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.