How do you unit test a mail sending function

后端 未结 6 2116
礼貌的吻别
礼貌的吻别 2021-01-31 17:41

I have a function which creates a report and sends it to a user. What I\'ve done is create a mock for the email function, and verify if the \'send\' function of the email class

6条回答
  •  攒了一身酷
    2021-01-31 18:44

    I recently ran into this issue myself. Try using http://netdumbster.codeplex.com/. This library allows you to spin up a local SMTP server and check which mails it received. This is all handled in memory AFAIK so the perfomance impact on your test should be minimal.

    In your test you create the server like this

    SimpleSmtpServer server = SimpleSmtpServer.Start(25);
    

    Then just change the config of your mail sender class to use the local smtp server (localhost:25). After the mail is send you can access it like this

    server.ReceivedEmail[0]
    

    And use the Data or MessageParts property to check for subject, body and attachments.

    I hope this helps.

提交回复
热议问题