Friday, February 26, 2010

QTP - Send email with attachments

QTP - How to send email with attachments without installing MS Outlook.

Good framework should automatically send the test results to all the stakeholders, once the test is complete.
Best approach would be using CDO object, it is not required to install MS Outlook on the PC where test is getting executed.

Attaching email notification function with error logic that can validate whether email sent successfully.

Note: Replace the variable names with your program variable names or constants.
Function funSendEmail()
        If gsEmailNotification = "ON" Then

                Const cdoSendUsingPickup = 1
                Const cdoSendUsingPort = 2
                Const cdoAnonymous = 0
                ' Use basic (clear-text) authentication.
                Const cdoBasic = 1
                ' Use NTLM authentication
                Const cdoNTLM = 2 'NTLM
                
                ' Create the message object.
                Set objMessage = CreateObject("CDO.Message")
                'Set the from address this would be your email address.
                objMessage.From = gsEmailListFrom
                ' Set the TO Address separate multiple address with a comma
                objMessage.To = gsEmailListTo
                objMessage.Cc = gsEmailListCc                
                ' Set the Subject.
                objMessage.Subject = gsEmailSubject & " - " & Environment("envsPlace")
                ' Now for the Message Options Part.
                ' Use standared text for the body.
                objMessage.TextBody =  gsEmailBody
                
                ' Or you could use HTML as:
                ' objMessage.HTMLBody = strHTML
                
                ' ATTACHMENT : Add an attachment Can be any valid url

                Set fso = CreateObject("Scripting.FileSystemObject")
'                Test Log
'                Set folder = fso.GetFolder(gsFolderPath & "ExecutionLog")
'                Set files = folder.Files
'                
'                For each folderIdx In files
'                        objMessage.AddAttachment(folderIdx)
'                Next
                objMessage.AddAttachment(gsTestSummaryFilePath)

'                Control file
                objMessage.AddAttachment(gsDataFileSelect)
                                
'                TestCase File                
                Set folder = fso.GetFolder(gsFolderPath & "AutomationTestCases")
                Set files = folder.Files
                
                For each folderIdx In files
                        objMessage.AddAttachment(folderIdx)
                Next

'                Response Time xls file
                If  gsResponseTimeXlsPath = Empty Then
                Else
                        objMessage.AddAttachment(gsResponseTimeXlsPath)
                End If
                

                Set folder = Nothing
                Set folder = Nothing
                Set files = Nothing
    
                
                ' This section provides the configuration information for the SMTP server.
                ' Specifie the method used to send messages.
                objMessage.Configuration.Fields.Item _
                ("http://schemas.microsoft.com/cdo/configuration/sendusing") = _
                cdoSendUsingPort
                
                ' The name (DNS) or IP address of the machine
                ' hosting the SMTP service through which
                ' messages are to be sent.
                objMessage.Configuration.Fields.Item _
                ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = _
                "mail.xxxx.com" 
                
                ' Specify the authentication mechanism
                ' to use.
                objMessage.Configuration.Fields.Item _
                ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = _
                cdoBasic
                
                ' The username for authenticating to an SMTP server using basic (clear-text) authentication
                objMessage.Configuration.Fields.Item _
                ("http://schemas.microsoft.com/cdo/configuration/sendusername") = _
                "Username"
                
                ' The password used to authenticate
                ' to an SMTP server using authentication
                objMessage.Configuration.Fields.Item _
                ("http://schemas.microsoft.com/cdo/configuration/sendpassword") = _
                "Password"
                
                ' The port on which the SMTP service
                ' specified by the smtpserver field is
                ' listening for connections (typically 25)
                objMessage.Configuration.Fields.Item _
                ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = _
                25
                
                'Use SSL for the connection (False or True)
                objMessage.Configuration.Fields.Item _
                ("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = _
                False
                
                ' Set the number of seconds to wait for a valid socket to be established with the SMTP service before timing out.
                objMessage.Configuration.Fields.Item _
                ("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = _
                60
                
                ' Update configuration
                objMessage.Configuration.Fields.Update
                
                ' Use to show the message.
'                 MsgBox objMessage.GetStream.ReadText


                On Error Resume Next

                ' Send the message.
                objMessage.Send                

'                Setting global message list to empty as we are appending the existing file
                If  err.number = 0 Then
                        sMessages =  "Email sent Successfully "
                Else
                        sMessages =  "***Email NOT sent Successfully"
                End If

                Call funMessages(sMessages)
    
                On Error Goto 0
'                This will delete the test log and recreate again (Text file append is not working, generating write permission error) 
                Call funCreateTestLog()      
                
                Set objMessage = Nothing
            
        End If
   
End Function







---


2 comments:

  1. hi Bharath,

    I want to verify the content(body) of mail so I am following below steps:
    1. Getting mail body as HTMLBody
    2. Writing the same thing to one file such as .htm

    But I am getting error while writing plz can u help me on this.

    ReplyDelete