C# - Validate email address syntax and test email address

The following c# example codes demonstrate how to test email address without specified SMTP server.

Validate Email Address Syntax

Regular expression can be used to validate if an email address is in correct format. For example: you can use this ^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,6}$ pattern to verify if the email address has valid format.

Test Email Address

Even the email address has a valid format, it doesn’t mean the email address is existed in real world. TestRecipients method can be used to test if the email address is valid.

How does it work? Firstly, SmtpClient performs a DNS MX record query. If it retrieves the recipient’s local SMTP server successfully, SmtpClient will try to connect to this server. SmtpClient then performs “RCPT TO” command to test if this SMTP server accepts this email address.

Please always pass null (Nothing in Visual Basic) to SmtpServer paramter except you want to test whether an email address will be accepted by a specified SMTP server.

Installation

Before you can use the following codes, please download EASendMail SMTP Component and install it on your machine at first. Full sample proejcts are included in this installer.

Install from NuGet

You can also install the run-time assembly by NuGet. Run the following command in the NuGet Package Manager Console:

Install-Package EASendMail

Note

If you install it by NuGet, no sample projects are installed, only .NET assembly is installed.

Add reference

To use EASendMail SMTP Component in your project, the first step is Add reference of EASendMail to your project. Please create or open your project with Visual Studio, then go to menu -> Project -> Add Reference -> .NET -> Browse..., and select Installation Path\Lib\net[version]\EASendMail.dll from your disk, click Open -> OK, the reference of EASendMail will be added to your project, and you can start to use it to send email in your project.

add reference in c#/vb.net/c++/cli/clr

.NET assembly

Because EASendMail has separate builds for .Net Framework, please refer to the following table and choose the correct dll.

Separate builds of run-time assembly for .NET Framework 1.1, 2.0, 3.5, 4.0, 4.5, 4.6.1, .NET Core 3.1, .NET 5.0, .NET Standard 2.0 and .NET Compact Framework 2.0, 3.5.

File .NET Framework Version
Lib\net20\EASendMail.dll Built with .NET Framework 2.0
It requires .NET Framework 2.0, 3.5 or later version.
Lib\net40\EASendMail.dll Built with .NET Framework 4.0
It requires .NET Framework 4.0 or later version.
Lib\net45\EASendMail.dll Built with .NET Framework 4.5
It requires .NET Framework 4.5 or later version.
Lib\net461\EASendMail.dll Built with .NET Framework 4.6.1
It requires .NET Framework 4.6.1 or later version.
Lib\netcoreapp3.1\EASendMail.dll Built with .NET Core 3.1
It requires .NET Core 3.1 or later version.
Lib\net5.0\EASendMail.dll Built with .NET 5.0
It requires .NET 5.0 or later version.
Lib\net6.0\EASendMail.dll Built with .NET 6.0
It requires .NET 6.0 or later version.
Lib\netstandard2.0\EASendMail.dll Built with .NET Standard 2.0
It requires .NET Standard 2.0 or later version.
Lib\net20-cf\EASendMail.dll Built with .NET Compact Framework 2.0
It requires .NET Compact Framework 2.0, 3.5 or later version.
Lib\net35-cf\EASendMail.dll Built with .NET Compact Framework 3.5
It requires .NET Compact Framework 3.5 or later version.

C# - Test email address - example

The following example codes demonstrate testing email address using MX DNS lookup. In order to run it correctly, please change SMTP server, user, password, sender, recipient value to yours.

Note

To get full sample projects, please download and install EASendMail on your machine.

using System;
using System.Collections.Generic;
using System.Text;

// Add EASendMail namespace
using EASendMail;

namespace mysendemail
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                SmtpMail oMail = new SmtpMail("TryIt");

                // Set sender email address, please change it to yours
                oMail.From = "test@emailarchitect.net";

                // Set recipient email address, please change it to yours
                oMail.To = "support@emailarchitect.net";

                // Do not set SMTP server address
                SmtpServer oServer = new SmtpServer("");

                Console.WriteLine("start to test email address ...");

                SmtpClient oSmtp = new SmtpClient();
                oSmtp.TestRecipients(oServer, oMail);

                Console.WriteLine("email address was verified!");
            }
            catch (Exception ep)
            {
                Console.WriteLine("failed to test email with the following error:");
                Console.WriteLine(ep.Message);
            }
        }
    }
}

How does it work?

Firstly, SmtpClient performs a DNS MX record query. If it retrieves the recipient’s local SMTP server successfully, SmtpClient will try to connect to this server. SmtpClient then performs “RCPT TO” command to test if this SMTP server accepts this email address.

Why we don’t recommend testing email address?

Because it totally depends on your networking connection, if your networking connection to the recipient server is bad or your IP address is blocked by the recipient server, test will be failed, but it doesn’t mean this email address is invalid.

Moreover, to prevent email address testing, many email providers accept the recipient address at first no matter if the address is valid or invalid, only after you sent the email data to the server, then the server rejects it if the recipient address is invalid.

Appendix

Comments

If you have any comments or questions about above example codes, please click here to add your comments.