By Anish Pillai Anish Pillai Posted under Advanced Concepts

How to send emails using QTP from Gmail and Yahoo

1 Flares Twitter 0 Facebook 0 Google+ 1 LinkedIn 0 Email -- 1 Flares ×

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.

Send Email from Gmail and Yahoo Mail

Send Email from Gmail and Yahoo Mail

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("") = True
'Enable basic smtp authentication
myMail.Configuration.Fields.Item("") = 1
'Specify SMTP server and port
myMail.Configuration.Fields.Item ("")=""
myMail.Configuration.Fields.Item ("") = 25
myMail.Configuration.Fields.Item ("") = 2
'Specify user id and password
myMail.Configuration.Fields.Item("") = ""
myMail.Configuration.Fields.Item("") = "**********"
'Update the configuration fields

'Specify email properties
myMail.Subject = "Sending Email from QTP"
myMail.From = ""
myMail.To = ""
myMail.CC = ""
myMail.BCC = ""
myMail.TextBody = "This is the Text Body"
'Send mail
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 ("")=""
myMail.Configuration.Fields.Item ("") = 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 = ";"
myMail.CC = ""

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 = ""
myMail.To = ""
myMail.TextBody= "Mail Content"
'Send 2 attachments in the mail
myMail.AddAttachment "D:\Attachment1.txt"
myMail.AddAttachment "D:\Attachment2.txt"

set myMail=nothing
Files Attached in Gmail

Files Attached in Gmail

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.

If you liked this article, you can join our blog to get the new posts delivered directly in your inbox.

1 Flares Twitter 0 Facebook 0 Google+ 1 LinkedIn 0 Email -- 1 Flares ×
  • Pingback: How to send emails using QTP from Microsoft Outlook - Automation Repository - Automation Repository()

  • qtp

    Awesome sir its great creation in QTP,i like so much,i use that queries in qtp automation,its working, its really super,thanks for sharing.To know more about QTP and its related stuffs refer <a href="; target="_blank">qtp

    book</a> to get full information about QTP.

  • Pingback: Part 5: Automating Outlook using QTP | Emails in Outlook - Automation Repository - Automation Repository()

  • Rahul

    can u plz suggest how to check whether the mails have been delivered to the concerned address ,

    • Anish10110

      Not sure how this can be effectively checked from the Sender's system. We can try to verify the following two things –

      1) The mail has reached the Sent Items folder from the Outbox. (This shows the mail has left your system)
      2) We can try to verify that there is no failure message received in our Inbox (in case the mail is not received). This would be difficult to track as there can be multiple types of error messages. Also we will not be sure when the error message arrives in our inbox. It can be 1 minute, 10 minutes, 1 hour etc. Because of this, this process is not very reliable.

      Other than this I'm not aware of any other ways to verify this from the Sender side.

  • deepak

    hello sir
    I am kind very new to the world of qtp……….. and i eagerly want to generate automated emails using qtp.
    so if you can tell me how i should get the desired result out of the above mentioned code. Do i have to add objects from and should write some codes for entering uid and password or executing this code will be enough.

    Please be kind with my questions ………….. i am not a professional ……i am using qtp since last week only and trying to learn it from my own and internet ofcourse;-)
    thank you

    • Anish10110

      Hi Deepak,

      The code will run on its own. You dont need to do anything other than providing proper username and password. No need to add objects from

  • deepak

    hello sir
    sorry to bother you again…….
    actually i have just pasted the entire code on the qtp coding area with a valid email id and password and its perfectly executing without showing any error. But no email is received at the receiving end.

    I am doing it the wrong way or there is something else i need to do with it .

    Also can you also give some good links through which i can thoroughly study the concept of cdo and stuff like that

    thank you 😉

    • Anish10110

      Hi Deepak,

      When you posted the entire code, did you also call the function?? Call the function and then see if it works. I will drop you an email for that.

  • Kirti

    Hi, I am trying to delete an email from my gmail account. Can you help me in that direction.

    • Anish10110

      What sort of information are you looking for here??

  • Kirti

    Anish, I am trying to click delete (Trash Can button) QTP is unable to record that step. I have tried descriptive programming. As gmail is very dynamic and use a lot of web element due to that I am unable to click that button.

    I have tried below codes

    If Browser("name:=.*").page("Title:=.*").webtable("class:=Bs nH iY").Exist Then
    Browser("name:=.*").page("Title:=.*").webelement("Class:= T-I J-J5-Ji nX T-I-ax7 T-I-Js-Gs ar7").Click
    End If

    With Browser("title:=Gmail.*").Page("micclass:=Page")
    .WebTable("class:=F cf zt").ChildItem(1, 1, "WebCheckBox", 0).Set "ON"
    Setting.WebPackage("ReplayType") = 2
    .WebElement("innertext:=Delete", "index:=0").Click ' gives error here
    Setting.WebPackage("ReplayType") = 1
    End With

    If Browser("name:=Gmail.*").page("Title:=Gmail.*").webtable("class:=Bs nH iY").Exist Then
    Browser("name:=Gmail.*").page("Title:=Gmail.*").webtable("class:=Bs nH iY").Webtable("class:=cf gJ").Image("class:=hA T-I-J3").Click
    ' Click delete this message on the fly out
    'below line of code doesn't work
    'Browser("name:=Gmail.*").page("Title:=Gmail.*").webtable("class:=Bs nH iY").webelement("class:=cj", "outtertext:= Delete this message").Click
    End If

    Browser("name:=Gmail.*").page("Title:=Gmail.*").webtable("height:=29","index:=45").highlight 'i have got delete button index by hit and trial and then trying to click

    Browser("name:=Gmail.*").page("Title:=Gmail.*").webtable("height:=29","index:=45").click 'here it doesn't click, tried fireevent("onclick"), Submit method no luck..Now I am going to try.

    Set obj=createobject("")
    obj.sendkeys "{ENTER}"
    set obj = Nothing

    • Anish10110

      If you spy on the Delete object, you will find outerhtml property, which contains some unique values to identify the object. This is what I found –

      "<DIV class="T-I J-J5-Ji nX T-I-ax7 T-I-Js-Gs ar7" role=button tabIndex=0 closure_uid_738834594="843" unselectable="on" aria-label="Delete" data-tooltip="Delete" act="10"><DIV class=asa unselectable="on"><SPAN class="J-J5-Ji ask" unselectable="on"> </SPAN><DIV class="ar9 T-I-J3 J-J5-Ji" unselectable="on"></DIV></DIV></DIV>"

      Example –

      Set obj = Description.Create
      obj("micclass").Value = "WebElement"
      obj("html tag").Value = "DIV"
      obj("outerhtml").Value = ".*tooltip=.*Delete.*"

      Set objDelete = Browser("name:=.*Gmail.*").Page("title:=.*Gmail.*").ChildObjects(obj)

      The above code will return only one instance of the object which is the Delete button itself. So you can use the 0th index to click on the button.

  • Pingback: Designing Hybrid Framework in QTP - Part 4 [Final Part] - Automation Repository - Automation Repository()

  • yzhang018

    I got 'failed to connect to server' at line 30.

1 Flares Twitter 0 Facebook 0 Google+ 1 LinkedIn 0 Email -- 1 Flares ×