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:
When you open it, you will see all the information that you sent in your email.


Milad KheradmandPosted Feb 17, 2023, 7:30 PM
This is great for test purpose. Thanks so much.
Devendra MilmilePosted Mar 4, 2018, 11:09 AM
Now I want to show that email in same application can I?
Manav PandyaPosted Sep 28, 2016, 12:41 PM
Can i get this code pls ...
Mostafa EyniPosted Jul 20, 2013, 5:59 PM
Thank you very much
Prabhakar MauryaPosted Apr 11, 2013, 6:04 AM
Nice article
Nitin BhardwajPosted Apr 11, 2013, 6:03 AM
very helpful article
Anubhav ChaudharyPosted Apr 10, 2013, 11:25 PM
Nice Article, Gaurav.