How to test dot net smtpclient email functionality in localhost?

0 votes
asked Jul 4, 2013 in C#.NET by anonymous

I have .net application that supposes to send email notification during the process. I have used Smtpclient and provided my host server. But during my unit testing phase I am not able to test this functionality. So, I am really wondering is there a way I can test this smtp email functionality in localhost?

Share

1 Answer

+1 vote
answered Jul 6, 2013 by Aadhira (1,213 points)
selected Jul 8, 2013 by administrator
 
Best answer

This can be done easily by using Smtp4dev utility without having any smtp server setup.
Download the smtp dev utility from below url. That has a single executable file and no need of any installation for this. Once you start running the exe file, you will be able to see the application open.

You can minimize that application that will be added into system tray and will be listening for any email that comes to localhost.

smtp dev localhost email sytem tray icon screenshot

Don’t worry, you do not need to setup anything for smtp localhost. All you need to do is, run the Smtp4Dev exe and send email using SmtpClient by pointing to the server address as “127.0.0.1” or “localhost”.

You can From address and To address as any valid email address. Also, subject and messages are free text.

Once you send the email using SmtpClient, you will see the email notification from the System tray. Click on that popup to view the email in the Smtp4dev client. That’s all.

smtp dev localhost email notification popup screenshot

smtp dev localhost email client window screenshot

Below code will give you better idea.

class Program
{
    static void Main(string[] args)
    {
        SMTPDevEmailDemo smtpDevEmailDemo = new SMTPDevEmailDemo();
        smtpDevEmailDemo.SendEmail(new EmailRequst() { From = "smtpdevfrom@smtpdev.com", To = "smtpdevto@smtpdev.com", Subject = "Test email", Message = "Hello to local email" });
    }
}

namespace DemoCodes
{
    public class SMTPDevEmailDemo
    {
        public void SendEmail(EmailRequst emailRequest)
        {
            SmtpClient smtpClient = new SmtpClient("127.0.0.1");
            smtpClient.Send(
                emailRequest.From,
                emailRequest.To,
                emailRequest.Subject,
                emailRequest.Message);
        }
    }

    public class EmailRequst
    {
        public string From { get; set; }

        public string To { get; set; }

        public string Subject { get; set; }

        public string Message { get; set; }
    }
}

Your answer

Preview

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.
site design / logo / content © 2013 - 2015 pinfaq.com
...