14 July 2010

Sending Emails using QTP via Gmail, Hotmail and other Email Service Providers; Using Windows CDO (Collaboration Data Objects)

This is a really simple post about a very simple concept of sending an email using QTP. Sending emails in Software Testing is of utmost importance. You are required to send test reports and various attachments supporting the defects, tests, etc. A common scenario is when you are required to send an attachment with an email reporting the status of the end at its end. It becomes simple if you are using Quality Center with QTP as you can use the inbuilt solutions. I am trying to highlight the concept of sending an email from QTP using Microsoft's Collaboration Data Objects (CDO).


In order to achieve the goal of sending the email via QTP, we will use the methods of IMessage Interface and IConfiguration Interface of Microsoft's CDOSYS Library. You would require the SMTP server settings of the email service that you wish to use for sending the email. If you are using Gmail, the server is smtp.gmail.com (Port 25) and for hotmail, it is smtp.live.com (Port 25). In case you are using your company's SMTP server, you would require its correct SMTP details before you can get your emails delivered.

The code below has been tested on QTP via Gmail and Hotmail.
For Gmail,

Sub sendmail()
  Set iMsg = CreateObject("CDO.Message")
  Set iConf = CreateObject("CDO.Configuration")

  iConf.Load -1 ' CDO Source Defaults
  Set Flds = iConf.Fields

  With Flds
   .Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
   'Enable SSL Authentication
 
   .Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
   'Value of 1 enables basic authnetication, 
   '2 enables NTLM Authentication, 
   '0 disables Authentication

   .Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "xyz@gmail.com"
   .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") =  "Your Email's Password"
   .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"

   .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
   'Value of 2 means send using port
   'value of 1 means send using a local SMTP server
   'value of 3 means send using Exchange Server

    .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
   'The SMTP Port which must be enabled in your network by ISP or local Firewall

    .Update
  End With

  With iMsg
   Set .Configuration = iConf
   .To = "xyz@gmail.com"
   .CC = ""
   .BCC = ""
   .From = "xyz@gmail.com"
   .Subject = "Test email sent from QTP Professional"
   .TextBody = "This is a test email sent using Gmail's SMTP Server with port 25. Blog info is - LearnQTP.info"
   .AddAttachment "c:\LearnQTP.info.pdf"
   'Local path of the file to attached
   'For attaching another file, 
   'repeat the line with new path
   .Send
  End With
End Sub


For Hotmail, Simple change the SMTP server address to 'smtp.live.com' and your email address.


Explanation of some of the lines in the code -

Set iConf = CreateObject("CDO.Configuration")
iConf.Load -1 ' CDO Source Defaults

In the code above, we are creating an object of the IConfiguration Interface of CDOSYS Library and then loading all the default fields for configuring the SMTP server (by putting value as -1) using the method Load. Value of 1 loads the default values from IIS (Microsoft Internet Information Server). Value of 2 loads the values from Microsoft Outlook.

5 comments:

  1. very nice...works great!

    Thanks!

    Matt

    ReplyDelete
  2. Its works well on system connected to public network. However in corporate network(which has proxy server settings) it doesn't work. How can we instruct the code to send this smtp request thru a specific proxy and not thru default one?

    ReplyDelete
  3. Your article is superbly awesome. I’m so satisfied with the information. I was searching this topic, Thank you so much, please keep writing such articles. bulk email marketing

    ReplyDelete
  4. Sending an email using QTP seems very tedious. I myself had difficulty sending emails using this technique. Anyway, it's good that you have posted this simple tutorial. Many users will surely benefit from this.

    email encryption software

    ReplyDelete
  5. Tnx a lot...
    Very nice, works great.

    ReplyDelete