Send test emails in asp .net without internet connectivity.

 In this article we will see how to send emails using SmtpDeliveryMethod.SpecifiedPickupDirectory in Asp .Net.This technique is very useful when we have to create and test email functionality in Asp .Net where we do not have access to the internet [It might feel funny that no internet connection in this era but there are some cases where we cannot use internet for security reasons like banking sectors or Military sectors] or we do not have any SMTP server configured on our machine. In these situations we can easy test our email code, email body formatting using SmtpDeliveryMethod.SpecifiedPickupDirectory.
 

Step1: Add Namespaces.

 

using System.Net.Mail;
using System.IO;

 


Step2:  Created Method to send emails.

 

 public void SendEmailsUsingPickupDirectory()
    {

        //create the mail message
        MailMessage mail = new MailMessage();

        //set the addresses
        mail.From = new MailAddress("[email protected]");
        mail.To.Add("[email protected]");

        //set the content
        mail.Subject = "Test Email Using PickupDirectory in Asp.Net";
        mail.Body = "This is email body to test emails on local computer using asp .net.";

        //==== Check if directory exists or not. If not then create new directory/folder.
        DirectoryInfo dirInfo = new DirectoryInfo("C:\\TestEmails");
        if (!dirInfo.Exists)
        {
            Directory.CreateDirectory("C:\\TestEmails"); //=== Create new folder
        }

        SmtpClient smtp = new SmtpClient();
        smtp.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
        smtp.PickupDirectoryLocation = "C:\\TestEmails";
        smtp.Send(mail);
    }

 


Step3: Call Method from page Load or button click.

 

 protected void Page_Load(object sender, EventArgs e)
    {
        SendEmailsUsingPickupDirectory();
    }

 

 

Final Output:

Send Emails in asp .net without internet connectivity using SpecifiedPickupDirectory

 

About Us | Terms of Use | Privacy Policy | Disclaimer | Contact Us Copyright © 2012-2024 CodingFusion
50+ C# Programs for beginners to practice