Wouldn’t it be great if Test Report are sent automatically across team as soon the Test Execution is complete??
Now, let’s see how to achieve this using Office 365.com, Outlook.com and Gmail.com account as the SMTP host.
Include the below code in your OneTImeTearDown (NUnit) or AfterClass (TestNG) method: we attain this feature using library => System.Net.Mail;
using System.Net.Mail;
namespace MailConfiguration
{
static void Main(string[] args)
{
string _sender = “youraccount@outlook.com”;
string _password = “your password”;
string _recipient = “RecipientId@domain.com”;
string subject = “Test Mail”;
string message = “Hello!!”;
//Outlook SMTP client configuration
SmtpClient client = new SmtpClient(“smtp.office365.com”);
//Gmail SMTP client configuration
//SmtpClient client = new SmtpClient(“smtp.gmail.com”);
//SMTP server uses port è 25, 587(recommended)
client.Port = 587;
//Authorize connection
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
System.Net.NetworkCredential credentials = new System.Net.NetworkCredential(_sender, _password);
client.EnableSsl = true;
client.Credentials = credentials;
//Send Mail
try
{
var mail = new MailMessage(_sender.Trim(), recipient.Trim());
mail.Subject = subject;
mail.Body = message;
//Remote Certificate Authentication
ServicePointManager.ServerCertificateValidationCallback =
delegate (object s, X509Certificate certificate,
X509Chain chain, SslPolicyErrors sslPolicyErrors)
{ return true; };
//Add Attachment/s
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment(@”Attachment Location”);
mail.Attachments.Add(attachment);
//Send Mail
client.Send(mail);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
throw ex;
}
}
}
Please do subscribe to our blogs & keep yourself updated with new posts.
Happy Reporting