Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test asp.net email is being sent

I have some code in my asp.net which sends an email:

public void SendEmail(string message)
{
    var body = message;

    var email = new MailMessage(ConfigurationManager.AppSettings["SenderEmail"],
                            ConfigurationManager.AppSettings["RecipientEmail"],
                            "Email Test", body);

    var client = new SmtpClient();
    client.Host = Properties.Settings.Default.smtp;
    client.DeliveryMethod = SmtpDeliveryMethod.Network;
    client.Credentials = CredentialCache.DefaultNetworkCredentials;

    client.Send(email);
}

I'm wanting to know how to test this. Whether it is a unit test or integration test I really just don't care. I'm NOT wanting to mock this out. I'm actually wanting to write a test that an email is sent with the correct message.

Can anyone help me with this?

like image 946
Mr Cricket Avatar asked Sep 14 '10 03:09

Mr Cricket


1 Answers

Just create a folder called "maildrop" on your c:/ drive and use the following in your Web.config file:

<mailSettings>
    <smtp deliveryMethod='SpecifiedPickupDirectory'>
        <specifiedPickupDirectory pickupDirectoryLocation="c:\maildrop" />
    </smtp>
</mailSettings>

More information:

http://weblogs.asp.net/gunnarpeipman/archive/2010/05/27/asp-net-using-pickup-directory-for-outgoing-e-mails.aspx

like image 148
IrishChieftain Avatar answered Oct 04 '22 05:10

IrishChieftain