Saturday 15 March 2014

How to send an email in selenium webdriver in C#?

When working with the selenium webdriver web application testing framework, the stakeholders are interested in the reports of execution of test cases.

We can send an email to stakeholders when the execution is completed.

Below code shows how we can send an email in C# with attachment. Please note that you will have to add the outlook library reference to the project.


Microsoft.Office.Interop.Outlook.Application oApp = new Microsoft.Office.Interop.Outlook.Application();
               
                Microsoft.Office.Interop.Outlook.MailItem oMsg = (Microsoft.Office.Interop.Outlook.MailItem)oApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
               
                oMsg.HTMLBody = "Selenium Webdriver Test Execution Report";
                //Add an attachment.
                String attach= "Attachment to add to the Mail";
                int x= (int)oMsg.Body.Length + 1;
                int y= (int)Microsoft.Office.Interop.Outlook.OlAttachmentType.olByValue;

                //Attach the file here
                Microsoft.Office.Interop.Outlook.Attachment oAttach = oMsg.Attachments.Add(@"c:\sagar\reports.html", y, x, attach);

                //here you can add the Subject of mail item
                oMsg.Subject = "Automation Reports of Test Execution";
                // Here you can Add the mail id to which you want to send mail.
                Microsoft.Office.Interop.Outlook.Recipients oRecips = (Microsoft.Office.Interop.Outlook.Recipients)oMsg.Recipients;
                
                Microsoft.Office.Interop.Outlook.Recipient oRecip = (Microsoft.Office.Interop.Outlook.Recipient)oRecips.Add("sagar.salunke@xyz.com");
                oRecip.Resolve();
               
                

               //send the mail using send method
               oMsg.Send();

What do you think on above selenium topic. Please provide your inputs and comments. You can write to me at reply2sagar@gmail.com

No comments:

Post a Comment

Buy Best Selenium Books

Contributors