Configuring System.Net.Mail for nDumbster

The most common way to send email in .NET is to use System.Net.Mail which was introduced in .NET 2.0. For example

using (MailMessage message = new MailMessage())
{
    message.From = new MailAddress("sender@contoso.com");
    message.To.Add(new MailAddress("recipient1@contoso.com"));
    message.CC.Add(new MailAddress("carboncopy@fabrikam.com"));
    message.Subject = "This is my subject";
    message.Body = "This is the content";

    using (SmtpClient client = new SmtpClient())
    {
        client.Send(message);
    }
}
System.Net.Mail is configured in your app.config/web.config file by adding a <mailSettings> section to the system.net configuration section, for example

<system.net>
  <mailSettings>
    <smtp>
      <network host="smtpserver1" port="25" />
    </smtp>
  </mailSettings>
</system.net>
So for testing purposes you simply need to change your application configuration file appropriately, or create a new one in your test project, using localhost as the network host and the port number you will run nDumpster on during your tests. If you decide to let nDumpster run on a priviliged ports (i.e. a port number below 1024) then your test runner may prompt you with a UAC dialog the first time it is ran. For testing purposes, especially unattended test runs such as those on continuous integration servers you should choose a high port, such as 2525 for nDumpster by specifying the port when starting an instance of the class;

this.smtpServer = SimpleSmtpServer.Start(2525);
and change the application configuration file in your test project to match, for example

<system.net>
  <mailSettings>
    <smtp>
      <network host="localhost" port="2525" />
    </smtp>
  </mailSettings>
</system.net>

Last edited Apr 15, 2011 at 8:56 PM by blowdart, version 2

Comments

No comments yet.