Find knowledge base article(s) by searching for keywords in the title e.g. type linux in the search box below
Find knowledge base article(s) by browsing the subject categories of articles
Technology quick references, cheatsheets, user manuals etc.
Shop Online through ShopifyLite
Tutorials on various IT applications.
Search Title    (UL:0 |SS:f)

Protocols >> SMTP >> Scripts to test sending email using gmail server as SMTP Relay host with TLS and Submission port 587


Pre-requisites

Your gmail account must have been set with Securty -> Less secure app access -> On

 

Powershell

Make sure we can excute powershell script, from a powershell session launched with "Run as administrator", set the execution policy (if not already set)

PS C:\WINDOWS\system32> set-executionpolicy -ExecutionPolicy RemoteSigned

 

Then create the following script

smtp-relay-test.ps1

param ( $Server = "smtp.gmail.com", $Port = "587", $From = "yourgmailid@gmail.com", $Username = "yourgmailid@gmail.com", $Password = "YourPasswordHere" ) "You entered $Server, $Port, $From, $Username, $Password" $EmailTo = "abdul.rahman.awad@gmail.com" $SendTime = (get-date).ToString("u") $Subject = "From $From - powershell - " + $SendTime $Body = "Hi, let me know if you receive this email via $Server" $MessageSubject = "From $From - powershell - " + $SendTime $Message = New-Object System.Net.Mail.MailMessage $From,$EmailTo $Message.IsBodyHTML = $false $Message.Subject = $Subject $Message.Body = @" Hi Let me know if you receive this email via $Server "@ # "Sending" $Message # $SMTPClient = New-Object Net.Mail.SmtpClient($Server, $Port) $SMTPClient.EnableSsl = $true $SMTPClient.Credentials = New-Object System.Net.NetworkCredential($Username, $Password); #$SMTPClient.Send($From, $EmailTo, $Subject, $Body) $SMTPClient.Send($Message)

 

Python

smtp-relay-test.py

import smtplib, ssl from datetime import datetime now = datetime.now().strftime("%Y-%m-%d %H:%M") import sys NumArgs = len(sys.argv) ScriptName = sys.argv[0] if NumArgs < 5: print("\nSyntax: {} SMTPServer Port From Username Password\n".format(ScriptName)) smtp_server = "smtp.gmail.com" port = 587 # For starttls sender_email = "your-gmail-id@gmail.com" username = "your-gmail-id@gmail.com" password = "YourPasswordHere" print("\nUsing default:") else: smtp_server = sys.argv[1] port = sys.argv[2] sender_email = sys.argv[3] username = sys.argv[4] password = sys.argv[5] print("\nUsing entered parameters:") print("SMTPServer={}".format(smtp_server)) print("Port={}".format(port)) print("From={}".format(sender_email)) print("Username={}".format(username)) print("Password={}".format(password)) receiver_email = "recipient@somedomain.com" message = """\ From: {} To: {} Subject: From {}- python - {} Hi, let me know if you receive this email via {}. """.format(sender_email,receiver_email,sender_email,now,smtp_server) print(message) context = ssl.create_default_context() with smtplib.SMTP(smtp_server, port) as server: server.ehlo() # Can be omitted server.starttls(context=context) server.ehlo() # Can be omitted server.login(username, password) server.sendmail(sender_email, receiver_email, message)

 

 

 

 

 

 

 

[ © 2008-2021 myfaqbase.com - A property of WPDC Consulting ]