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:

  1. <system.net>  
  2.     <mailSettings>  
  3.       <smtp deliveryMethod="SpecifiedPickupDirectory">  
  4.         <specifiedPickupDirectory pickupDirectoryLocation="C:\Mails\"/>  
  5.       </smtp>  
  6.     </mailSettings>  
  7. </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:

  1. public static void SendEmail()  
  2. {  
  3.    try  
  4.    {  
  5.       MailMessage mailMessage = new MailMessage();  
  6.       MailAddress fromAddress = new MailAddress("gauravgupta295@gmail.com");  
  7.       mailMessage.From = fromAddress;  
  8.       mailMessage.To.Add("g.gupta002@yahoo.com");  
  9.       mailMessage.Body = "This is Testing Email Without Configured SMTP Server";  
  10.       mailMessage.IsBodyHtml = true;  
  11.       mailMessage.Subject = " Testing Email";  
  12.       SmtpClient smtpClient = new SmtpClient();  
  13.       smtpClient.Host = "localhost";  
  14.       smtpClient.Send(mailMessage);  
  15.     }  
  16.     catch (Exception)  
  17.     {  
  18.     }  
  19. } 

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