Search code examples
powershellsmtpgmailpowershell-ise

Learning an Email script using Gmail SMTP authentication


I'm new to power shell scripting.I wrote a script which should send an email to my inbox

I found few solution and improved my script

$cred = Get-Credential
Send-MailMessage -SmtpServer "smtp-relay.gmail.com" -Credential $cred -UseSsl -Port 587 -From "[email protected]" -To "[email protected]" -Subject "test" -Body "test"

I got an error message as follows below

Send-MailMessage : Mailbox unavailable. The server response was: 5.7.0 Mail relay denied 
[117.193.79.33]. bd10sm271633igb.4 - gsmtp
At line:2 char:1
+ Send-MailMessage -SmtpServer "smtp-relay.gmail.com" -Credential $cred -UseSsl -P ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.Mail.SmtpClient:SmtpClient) [Send-M 
   ailMessage], SmtpException
    + FullyQualifiedErrorId : SmtpException,Microsoft.PowerShell.Commands.SendMailMessage

Again I changed the port number 465 or 25,it get operation timeout.Help me out!!

PS:Telnet on 25 or 587 is working and no AV program in computer

Thanks in advance.


Solution

  • $cred = Get-Credential Send-MailMessage -SmtpServer "smtp.gmail.com" -Credential $cred -UseSsl -Port 587 -From "[email protected]" -To "[email protected]" -Subject "test" -Body "test"

    This Testing with Gmail works,after i followed this steps 1.Disable two way verification 2.Allow less secure apps in settings Lesssecure Apps