Last post Mar 30, 2016 02:05 PM by PatriceSc
Mar 30, 2016 08:16 AM|sudip_inn|LINK
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
Mar 30, 2016 09:19 AM|navneetmitawa|LINK
1. Example 1
2. Example 2
Mar 30, 2016 10:33 AM|sudip_inn|LINK
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.
Mar 30, 2016 10:59 AM|PatriceSc|LINK
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?
Mar 30, 2016 12:41 PM|sudip_inn|LINK
just tell me how to unit test mail sending function?
Mar 30, 2016 02:05 PM|PatriceSc|LINK
If you don't have a unit test project try
The test code could look like :
public class MailTest
const string from="email@example.com";
const string recipients="firstname.lastname@example.org";
const string subject="Test subject";
const string body="Test Body";
public void Sending()
public void fromNull()
MvcApplication1.Mail.Send(null, recipients, subject, body);
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).