≡ Menu

send emails via gmail account using powershell

Copy the below powershell code into a file(say send-via-gmail.ps1) and execute it from command line.

$SmtpClient = new-object system.net.mail.smtpClient
$smtpclient.Host = ‘smtp.gmail.com’
$smtpclient.Port = 587
$smtpclient.EnableSsl = $true
$smtpClient.Credentials = [Net.NetworkCredential](Get-Credential GmailUserID)
$smtpclient.Send(‘GmailUserID@gmail.com’,$args[0] ,’test subject’, ‘The mail is sent via gmail using the powershell script grabbed from http://techibee.com’)

Command to execute:

[PS]C:>.send-via-email.ps1 toemailid@todomain.com

Happy Learning..,
Sitaram Pamarthi

{ 3 comments… add one }
  • Isabel Sammet February 6, 2014, 7:27 am

    Hi!
    I can’t understand this line:

    $smtpClient.Credentials = [Net.NetworkCredential](Get-Credential GmailUserID)

    I have to put my gmail user, or i have to write that without changes … can you explain me?

    Thanks!! Cheers!!

Leave a Comment