Send Gmail Using Powershell

less than 1 minute read

Description:

This script template will allow you to send emails from a Powershell script.

To Resolve:

  1. Script

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    
    $From = "from@gmail.com"
    $To = "to@gmail.com"
    $Subject = "Read this"
    $Body = "<HTML><HEAD><META http-equiv=""Content-Type"" content=""text/html; charset=iso-8859-1"" /><TITLE></TITLE></HEAD>"
    $Body += "<BODY bgcolor=""#FFFFFF"" style=""font-size: Small; font-family: TAHOMA; color: #000000""><P>"
    $Body += "Dear <b><font color=red>customer</b></font><br>"
    $Body += "This is an <b>HTML</b> email<br>"
    $Body += "Click <a href=http://www.google.com target=""_blank"">here</a> to open google <br>"
    $SMTPServer = "smtp.gmail.com"
    $SMTPPort = "587"
    # $Cc = "YourBoss@YourDomain.com"
    # $Attachment = "C:\temp\Some random file.txt"
    
    # Optionally include:
    # -Attachments $Attachment
    # -Cc $Cc
    
    Send-MailMessage -From $From -to $To -Subject $Subject `
       -BodyAsHTML -Body $Body -SmtpServer $SMTPServer -port $SMTPPort -UseSsl `
       -Credential (Get-Credential -Credential "from@gmail.com")
    
  2. Source is maintained under gwNetworking

-----------------------------------------------------------
Spotted a mistake in this article? Why not suggest an edit!
Did you like the article? Donations are always welcome!
-----------------------------------------------------------

Comments