How to Test Email Without Configure SMTP in ASP.NET

Introduction

This article describes how to send email with localhost from a web application and determine whether your application has sent the email successfully when it is published.

Usually when we use the email module in a project, we need to configure the SMTP Server to test it. We cannot test, without configuration, that the email is being sent successfully or not.

But, there are many ways to test the email module in the web application without the need to configure the SMTP Server. Here, I will discuss with you one way that is very easy and requires no installation.

First of all, we need to apply some settings in the Web Config file; see:

<system.net>

    <mailSettings>

      <smtp deliveryMethod="SpecifiedPickupDirectory">

        <specifiedPickupDirectory pickupDirectoryLocation="C:\Mails\"/>

      </smtp>

    </mailSettings>

  </system.net>


In the code above, I set the SMTP deliveryMethod and provide the path of the "pickupDirectoryLocation" where the sent email will be saved.

Note: The pickupDirectoryLocation must already exist in the system where the mail is to be placed, otherwise it gives an error.

Now, we write the code to send the email in C#. See:

public static void SendEmail()
{

   try

   {

      MailMessage mailMessage = new MailMessage();

      MailAddress fromAddress = new MailAddress("[email protected]");

      mailMessage.From = fromAddress;

      mailMessage.To.Add("[email protected]");

      mailMessage.Body = "This is Testing Email Without Configured SMTP Server";

      mailMessage.IsBodyHtml = true;

      mailMessage.Subject = " Testing Email";

      SmtpClient smtpClient = new SmtpClient();

      smtpClient.Host = "localhost";

      smtpClient.Send(mailMessage);

    }

    catch (Exception)

    {

    }

}



Run the application.

Now, go to pickupDirectoryLocation, you will see that there is mail in the directory like this:

Email-In-Asp.net.png


When you open it, you will see all the information that you sent in your email.

Read-Email-in-outlook.png

Up Next
    Ebook Download
    View all
    Learn
    View all