0

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)
    {

    }

}
Chandan Kumar
  • 4,570
  • 4
  • 42
  • 62
lolek
  • 59
  • 3
  • 12
  • General questions about how to improve code should go to http://codereview.stackexchange.com/ Stackoverflow is for more specific problems. – Oswald Dec 17 '14 at 13:39
  • 1
    I don't understand the question. Can you explain what is the problem or question? also what you do isn't unit testing. Unit testing means testing in isolation. It shouldn't depend on anything as you're doing. – Sriram Sakthivel Dec 17 '14 at 13:40
  • 3
    You're really unit testing an outside service? Seems a bit excessive. Going to unit test IIS too? – Brad Christie Dec 17 '14 at 13:40
  • @Oswald I'm pretty sure this isn't a code review question. – Sriram Sakthivel Dec 17 '14 at 13:42
  • @SriramSakthivel i am newbie in unit testing and at this point most important question is How can i perform unit test for sending email? – lolek Dec 17 '14 at 13:46
  • Please refer [this answer](http://stackoverflow.com/questions/4904096/whats-the-difference-between-unit-functional-acceptance-and-integration-test) and get some idea. Typically unit tests uses the mocks for the dependencies. `StackOverflow` answer can't explain these. Learn about unit testing, and if you got struck with specific problem, we're happy to help. – Sriram Sakthivel Dec 17 '14 at 13:50
  • You wouldn't unit test sending mail -- mail represents an external dependency. You'd unit test the code that depends on sending mail, providing that class with a mock email sender that simulates sending mail without actually doing so. – Daniel Mann Dec 17 '14 at 14:26

1 Answers1

1

What you probably need is dummy dev SMTP server.

You can get it from here

Once it's running on your dev machine you can send emails setting smtp.Host to 'localhost' and using dummy email addresses.

However as mentioned in comments such unit test isn't useful at all as whether email is sent successful or not depends on many external factors like e.g network connection. If you past test on your local machine it does not mean sending email going to be successful every time on production server.

PrzemG
  • 775
  • 6
  • 7
  • 3
    It's not a unit test, it's an integration test. And it's useless because it's testing .NET framework functionality. You test your code, not Microsoft's code. – Daniel Mann Dec 17 '14 at 14:34
  • Thanks for answer after this i decide to drop that idea which seems dumb right now... Thanks – lolek Dec 18 '14 at 08:22