Want to work with us? We're on the lookout for digital experts.

We're hiring

Validating Email Address Against The Mail Server

Web Bureau

18 November 2011 by Web Bureau

I'm sure you have all came across the problem of automatic spamming of forms using a robot.

One solution to this is to validate the email address on the form by checking that it is a valid domain name and chacking to see if the Mail Server exists.


  • The code below extracts the domain name from the email address entered
  • Carrys out a name server lookup
  • If found you know that the email address is from a valid domain.


Dim address As String = ""
Dim host As String() = (address.Split("@"c))
Dim hostname As String = host(1)
Dim info As New ProcessStartInfo()
Dim ns As Process
Dim sout As StreamReader
Dim mailserver As String = ""
Dim response As String = ""

info.UseShellExecute = False
info.RedirectStandardInput = True
info.RedirectStandardOutput = True
info.FileName = "nslookup"
info.Arguments = "-type=MX " + sDomain.ToUpper.Trim
ns = Process.Start(info)

sout = ns.StandardOutput
Dim reg As Regex = New Regex("mail exchanger = (?<server>[^\\\s]+)")

Do While (sout.Peek() > -1)
response = sout.ReadLine()
Dim amatch As Match = reg.Match(response)
If (amatch.Success) Then
mailserver = amatch.Groups("server").Value
Exit Do
End If

Return mailserver

Grow your businessStart a project with us today.

This site uses essential cookies for parts of the site to operate and have already been set. Find out more about how we use cookies and how you may delete them. You may delete cookies, but parts of the site will not work.