The following vb.net 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.
Sections:
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.
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.
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 2.0, 3.5, 4.0, 4.5, 4.6.1, 4.7.2, 4.8.1, .NET 5.0, .NET 6.0, .NET 7.0, .NET 8.0, .NET Standard 2.0 and .NET Compact Framework 2.0, 3.5.
File | .NET Framework Version |
Lib\[net20|40|45|461|472|481]\EASendMail.dll |
Built with .NET Framework 2.0, 4.0, 4.5, 4.6.1, 4.7.2, 4.8.1
It requires .NET Framework 2.0, 3.5 or later version. |
Lib\[net5.0|6.0|7.0|8.0]\EASendMail.dll |
Built with .NET 5.0, .NET 6.0, .NET 7.0, .NET 8.0
It requires .NET 5.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|net35-cf]\EASendMail.dll |
Built with .NET Compact Framework 2.0, 3.5
It requires .NET Compact Framework 2.0, 3.5 or later version. |
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.
' Add EASendMail namespace
Imports EASendMail
Module Module1
Sub Main()
Try
Dim oMail As 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
Dim oServer As New SmtpServer("")
Console.WriteLine("start to test email address ...")
Dim oSmtp As New SmtpClient()
oSmtp.TestRecipients(oServer, oMail)
Console.WriteLine("email address was verified!")
Catch ep As Exception
Console.WriteLine("failed to test email address with the following error:")
Console.WriteLine(ep.Message)
End Try
End Sub
End Module
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.
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.
Send the email to the recipient without testing. If you don’t get Transport Error and Failure Report in 24 hours, that means the recipient is valid. If you get Failure Report, you should consider to remove this recipient from your mail listing.
Appendix
Comments
If you have any comments or questions about above example codes, please click here to add your comments.