Thursday 25 June 2020

Test Email without sending Email to User - C# / C-Sharp - DotNet

Sometimes we may want to test email functionality without sending email to actual user. Email will be saved locally so that we can verify it sending out emails in real scenario.

In below code, SmtpClient settings are configured in App.Config. We don't need to retrieve and assign these values to SmtpClient object. These configurations should be picked up automatically by SmtpClient from App.Config. It helps to keep our code clean.

Full source code : Click here to download solution

There are two parts of Code:
  1. Code to Send Email
  2. App.Config Configuration to be used to test this code.

Code to Send Email

We need to define "PickupDirectoryLocation", which will be the path where email will be saved. This should be absolute path for the disk else it will throw exception. It can also be a shared path if you want to save emails on Shared network drive.

using System;
using System.Net.Mail;

namespace SendEmailUsingC_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            var smtpClient = new SmtpClient();
            var mailMessage = new MailMessage();
            try
            {
                mailMessage.To.Add("receiversEmail@gmail.com");
                mailMessage.From = new MailAddress("abc@xyz.com", "Test User");
                mailMessage.Subject = "Test Email using C-Sharp";
                mailMessage.Body = @"<h1>Test Email</h1>
                                    <br/>
                                    This is Test Email.";

                mailMessage.IsBodyHtml = true;
                try
                {
                    smtpClient.Send(mailMessage);
                    Console.WriteLine("Email sent successfully.");
                }
                catch (SmtpException smtpSpecificException)
                {
                    Console.WriteLine(smtpSpecificException.Message);
                }
            }
            catch (Exception ex) //General Exception
            {
                Console.WriteLine(ex.Message);
            }
            Console.ReadLine(); // This is just to pause Console Screen.
        }
    }
}



App.Config Configuration 

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
  </startup>
  <system.net>
    <!--Pickup Directory Configuration. Pickup Directory Location must be an absolute/full path to directory or It can also be network shared path.-->
    <mailSettings>
      <smtp deliveryMethod="SpecifiedPickupDirectory"  from="FromAddress@gmail.com">
        <specifiedPickupDirectory pickupDirectoryLocation="F:\Ovais\Blog\Emails"/>
      </smtp>
    </mailSettings>
   
    <!--Original Email Configuration. Keep only one configuration Uncommented.-->
    <!--<mailSettings>
      <smtp deliveryMethod="Network"  from="FromAddress@gmail.com">
        <network defaultCredentials="true" host="smtp.gmail.com" enableSsl="true" port="587" userName="username@gmail.com" password="userPassword"/>
      </smtp>
    </mailSettings>-->
  </system.net>
</configuration>

No comments:

Post a Comment

Test Email without sending Email to User - C# / C-Sharp - DotNet

Sometimes we may want to test email functionality without sending email to actual user. Email will be saved locally so that we can verify ...