≡ 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 https://techibee.com’)

Command to execute:

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

Happy Learning..,
Sitaram Pamarthi

Comments on this entry are closed.

  • Isabel Sammet February 6, 2014, 7:27 am

    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!!

    • Sitaram Pamarthi February 6, 2014, 6:47 pm

      Sammet, you have to put your gmail id and the password there.