Testing smtp code locally via smtp4dev with no smtp server setup

Testing smtp code locally via smtp4dev with no smtp server setup

1102038_25845967 Just another plug for a very helpful tool that I’ve been using for testing SMTP code locally. Smtp4dev simulates an SMTP server on your local machine, allowing you to send email via a local SMTP connection in your code when you do not have a SMTP server setup. You can even inspect the messages to ensure that they are exactly as intended, with the subject, body, etc. Very simple, but useful tool that I find myself using quite often. http://smtp4dev.codeplex.com/ (see below for a basic example of sending a message via smtp locally)

public static void SendEmail(string aFrom, string aTo, string aSubject, string aBody)
{
    //create the mail message
    MailMessage mail = new MailMessage();

    //set the addresses
    mail.From = new MailAddress(aFrom);
    mail.To.Add(aTo);

    //set the content
    mail.Subject = aSubject;
    mail.Body = aBody;

    //send the message
    SmtpClient smtp = new SmtpClient("localhost");
    smtp.UseDefaultCredentials = true;
    smtp.Send(mail);
}
By | 2018-03-07T02:52:08+00:00 November 27th, 2011|Categories: ASP.NET|Tags: , , , |0 Comments

About the Author:

Leave A Comment