views:

112

answers:

5

Hi, 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?

A: 

Can't you place this within a module and call it from a test and set the recipient to say you're email address. if you get the email then i'd say it's working.

griegs
+2  A: 

Send an email to yourself and see if you received it?

If you don't know how to do that you probably want to go back to basics.

Bimmy
A: 

to setup an automated test you'll want to have a test email address on a server you can query (since topic is asp.net, we'll assume exchange server), then query the mailbox for the email you're looking for using:

opt 1: exchange sdk

opt 2: through web requests (if the exchange server's http connector is enabled

opt 3: write your own simple pop3 client/cli/api

ref for opt 3: http://www.codeproject.com/KB/IP/popapp.aspx

alien052002
+4  A: 

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

IrishChieftain
+1 This has the clear advantage that you don't need to use differing code between your release application and test application.
Brian
A: 

http://ssfd.codeplex.com/

jgauffin