Thursday, April 12, 2012

Developing email functionality using smtp4dev

In order to develop and - most probably - to test some email sending functionality you will definitely need a SMTP server - running locally or outside. Under Windows XP the SMTP server installation is available as a IIS server component, which is now gone in Windows 7 (more information is available here).

The other option is to use third party SMTP server software. One of such applications is smtp4dev available at http://smtp4dev.codeplex.com/. It is nice, because:

  • No installation required (standalone executable is available)
  • No setup required
  • Messages are not being delivered and can be previewed locally in the SMTP server using smtp4dev GUI

In order to test email sending functionality, run this snippet:


using System.Net.Mail;

namespace Sender
{
    class Program
    {
        static void Main(string[] args)
        {
            MailMessage message = new MailMessage();
            message.To.Add("recipient@test.com");
            message.From = new MailAddress("sender@test.com");
            message.Subject = "Message Subject";
            message.Body = "Message Body";
            SmtpClient smtp = new SmtpClient("localhost");
            smtp.Send(message);
        }
    }
}