How to send emails using QTP from Gmail and Yahoo
This article shows how you can use QTP to send mails to any email address using email service providers such as GMail, Yahoo Mail etc. Automatically sending mails through QTP can be a very nice enhancement to your automation framework. For example, after you run your test scripts, you need to email test run reports (number of test scripts passed / failed etc) to the stakeholders. You can automate this mailing process and send custom reports to the required people.
Before going through the code, lets go through some important points regarding the code –
1. The below given email code is not directly related to QTP as such. The code is actually a VBScript function which you can use in QTP. So even if you don’t have QTP, you can use this VBScript code directly.
2. The email functionality uses Microsoft’s CDO (Collaboration Data Objects) technology to send mails. You can find more details about Microsoft CDO in Microsoft’s MSDN Library.
3. It is not necessary that you have to use this code using VBScript only. Microsoft CDO can be programmed using any language that creates COM object such as ASP, VB, Visual C++, C# etc.
4. The below code has been tested on Gmail and Yahoo mail. If you want to use this code in your company’s SMTP server, you need to contact your network team to get details about the SMTP address, port number etc.
5. To use this code, you don’t need to have Microsoft Outlook installed on your system. You can also use MS Outlook to send emails (we will discuss this in a separate post Read how to send mails from MS Outlook using QTP).
Let’s now see the code for sending emails using QTP (VBScript) through Gmail.
Function fnSendEmail() 'Create an object of CDO type Set myMail=CreateObject("CDO.Message") 'Enable SSL Authentication myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True 'Enable basic smtp authentication myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1 'Specify SMTP server and port myMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver")="smtp.gmail.com" myMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 myMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 'Specify user id and password myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "firstname.lastname@example.org" myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "**********" 'Update the configuration fields myMail.Configuration.Fields.Update 'Specify email properties myMail.Subject = "Sending Email from QTP" myMail.From = "email@example.com" myMail.To = "firstname.lastname@example.org" myMail.CC = "" myMail.BCC = "" myMail.TextBody = "This is the Text Body" 'Send mail myMail.Send Set myMail = Nothing End Function
Using the above code, you can send emails to any email id using your gmail address. Just specify your gmail user id and password in lines 14 & 15. Also mention the email id to which you want to send mails in lines 22, 23 and/or 24.
How to send mails from your Yahoo mail id
To send mails using your yahoo mail id, you need to replace the Gmail SMTP address and port number with Yahoo’s SMTP address and port number.
'Specify Yahoo SMTP server and Port Number myMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver")="smtp.mail.yahoo.com" myMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
How to send mails to multiple people in one go.
To send mails to multiple email ids in one go, you need to just enter multiple mail ids separated by a semi colon(;). Example:
myMail.To = "email@example.com; firstname.lastname@example.org" myMail.CC = "email@example.com"
How to send html content in the mail.
Normally you would not send plain text in emails. You would usually be sending some html content in mails. To do so, you need to use HTMLBody in place of TextBody and include html tags in your text body.
'myMail.TextBody = "This is the Text Body" -> TextBody not to be used myMail.HTMLBody = "<html><table border=1><tr>Row1</tr><tr>Row2</tr></table></html>"
How to send attachments using the email code.
To add an attachment in you mail, you need to include myMail.AddAttachment line in your code and specify the correct path of the file to be attached. To add more than one attachment, repeat myMail.AddAttachment code required number of times.
'Specify email mail properties myMail.Subject = "Sending Attachment in Mail from QTP" myMail.From = "firstname.lastname@example.org" myMail.To = "email@example.com" myMail.TextBody= "Mail Content" 'Send 2 attachments in the mail myMail.AddAttachment "D:\Attachment1.txt" myMail.AddAttachment "D:\Attachment2.txt" myMail.Send set myMail=nothing
Try out the above code and see if it works for you. In case if you have any queries regarding the code, please use the comments section or the contact form to let us know about the same.