Today I am trying to create a unit test in visual studio for sending a email i am struggling with finding the best way to do it and using SendCompleted
event as a validation for message been actually send.
Here i code i apply to send email but after using many different ways i just paste here clean code and maybe you can tell me what i am doing wrong or give me better way to solve it.
Here is my code what i have tried so far :
[TestClass]
public class sendEmailTest
{
[TestMethod]
public void sendAsyncEmailTest()
{
string from = "sender@test.com";
string to = "receiver@test.com";
MailMessage mail = new MailMessage(from, to);
mail.Subject = "Unit Test MVC";
mail.Body = "Unit Test for sending mail in MVC app";
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.test.com";
smtp.EnableSsl = true;
NetworkCredential networkCredential = new NetworkCredential(from, "testpassword");
smtp.UseDefaultCredentials = true;
smtp.Credentials = networkCredential;
smtp.Port = 587;
smtp.SendAsync(mail, null);
smtp.SendCompleted += new SendCompletedEventHandler(smtp_SendCompleted);
}
static void smtp_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
}
}