Selenium WebDriver Java Framework Course Limited Time Offer for $20

Selenium WebDriver Java Framework Course Limited Time Offer for $20

 

Print

Send Selenium Test Result Through Outlook Attachment

You need to use these assemblies below:

using System;
using System.Text;
using System.Net.Mail;
using System.Net;
using System.IO;
using Microsoft.Office.Interop.Outlook;
using System.Net.Mime;
using System.Collections;
using Attachment = Microsoft.Office.Interop.Outlook.Attachment;
using Exception = System.Exception;

public static void OutlookSendEmail(string recepientAddress,string messageSubject,string messageBody,string attachmentfile,string attachmentDisplayName)
{
            try
            {
                // Create the Outlook application by using inline initialization.
                Application oApp = new Application();

                //Create the new message by using the simplest approach.
                MailItem oMsg = (MailItem)oApp.CreateItem(OlItemType.olMailItem);

                //Add a recipient.
                // TODO: Change the following recipient where appropriate.
                Recipient oRecip = (Recipient)oMsg.Recipients.Add(recepientAddress);
                oRecip.Resolve();

                //Set the basic properties.
                oMsg.Subject = messageSubject;
                oMsg.Body = messageBody;

                //Add an attachment.
                // TODO: change file path where appropriate
                String sSource = attachmentfile;
                String sDisplayName = attachmentDisplayName;
                int iPosition = (int)oMsg.Body.Length + 1;
                int iAttachType = (int)OlAttachmentType.olByValue; 
                Attachment oAttach = oMsg.Attachments.Add(sSource,iAttachType,iPosition,sDisplayName);

                // If you want to, display the message.
                //oMsg.Display(true);  //modal

                //Send the message.
                oMsg.Save();
                oMsg.Send();
             

                //Explicitly release objects.
                oRecip = null;
                oAttach = null;
                oMsg = null;
                oApp = null;
            }

                // Simple error handler.
            catch (Exception e)
            {
                Console.WriteLine("{0} Exception caught: ", e);
            }

            //Default return value.
           
        }

Usage:

In your main class, just call the OutlookSendEmail("recepientAddress","messageSubject","messageBody","attachmentfile","attachmentDisplayName") method at the end of your test.