locked
Give me some idea like how to test mail sending routine RRS feed

  • Question

  • User264732274 posted

    i have one routine which send mail but i have no idea how i can use Visual studio own unit test api to write unit test code for mail sending routine.

    discuss with some code and idea. thanks

    Wednesday, March 30, 2016 8:16 AM

Answers

  • User753101303 posted

    If you don't have a unit test project try https://msdn.microsoft.com/en-us/library/ms182532.aspx

    The test code could look like :

        [TestClass]
        public class MailTest
        {
            const string from="one@example.com";
            const string recipients="two@example.com";
            const string subject="Test subject";
            const string body="Test Body";
            [TestMethod]
            public void Sending()
            {
                MvcApplication1.Mail.Send(from,recipients,subject,body);
            }
            
            [TestMethod,ExpectedException(typeof(ArgumentNullException))]
            public void fromNull()
            {
                MvcApplication1.Mail.Send(null, recipients, subject, body);
            }
            
            //etc...
        }
    

    I added also an app.config file to the unit test project to configure mailSettings. For now the method is just a wrapper around SmtpClient.Send.

    If it doesn't help you'll have to be more explicit about the problem you have (for now it sounds like you have done some testing already but that you have a particular problem with sending mails, could it be that SMTP settings are not configured? Or this is about the kind of test you could do (malformed mail address, cr/lf in subject etc...?).

    And so tests are ok (one because there is no exception, the other one because an exception is thrown).

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 30, 2016 2:05 PM

All replies

  • User-1768369891 posted

    Check It 

    1. Example 1

    2. Example 2

    Wednesday, March 30, 2016 9:19 AM
  • User264732274 posted

    i am not looking for knowledge to send mail by code rather i am looking for code which i can use for unit testing for send mail.

    Wednesday, March 30, 2016 10:33 AM
  • User753101303 posted

    Hi,

    It all depends on what you want to check exactly with this test Do you want to just check you don't have any exception or maybe you want to "send" this mail to a disk file and check if it is created and even perhaps its content?

    You can configure the SmtpClient class either using the web.config or programmatically to deliver mail to a disk folder if this is what you are looking for. Or is this is general problem about testing?

    Wednesday, March 30, 2016 10:59 AM
  • User264732274 posted

    just tell me how to unit test mail sending function?

    Wednesday, March 30, 2016 12:41 PM
  • User753101303 posted

    If you don't have a unit test project try https://msdn.microsoft.com/en-us/library/ms182532.aspx

    The test code could look like :

        [TestClass]
        public class MailTest
        {
            const string from="one@example.com";
            const string recipients="two@example.com";
            const string subject="Test subject";
            const string body="Test Body";
            [TestMethod]
            public void Sending()
            {
                MvcApplication1.Mail.Send(from,recipients,subject,body);
            }
            
            [TestMethod,ExpectedException(typeof(ArgumentNullException))]
            public void fromNull()
            {
                MvcApplication1.Mail.Send(null, recipients, subject, body);
            }
            
            //etc...
        }
    

    I added also an app.config file to the unit test project to configure mailSettings. For now the method is just a wrapper around SmtpClient.Send.

    If it doesn't help you'll have to be more explicit about the problem you have (for now it sounds like you have done some testing already but that you have a particular problem with sending mails, could it be that SMTP settings are not configured? Or this is about the kind of test you could do (malformed mail address, cr/lf in subject etc...?).

    And so tests are ok (one because there is no exception, the other one because an exception is thrown).

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 30, 2016 2:05 PM