Email validation with System.Net.Mail.MailAddress

sergiotome

New member
Joined
Dec 10, 2007
Messages
1
Programming Experience
3-5
Hi everybody,

I have a web site running on .net 1.1 but I have an interface with a 3rd party product where through a web service I pass an email address and some additional information and will generate a send surveys to selected customers. This 3rd party product runs on .net 2.0 and uses the System.Net.Mail.MailAddress class to validate whether an email address is valid or not, something like this:

System.Net.Mail.MailAddress mailAddress;
try
{
mailAddress = new System.Net.Mail.MailAddress(emailReceiver);
}
catch (System.FormatException e)
{
//"No of invalid email addresses found in this batch job" displayed in tasklog is bumped up
//Email is not created
//return
}

What I would like to do it to recreate that exact same validation but using the object available in the 1.1 framework. Does anyone know how the System.Net.Mail.MailAddress class validates whether an email address is valid or not? do they use some sort of regular expression available anywhere?

Thanks,
Sergio
 
You may use RegularExpression for email validation.

For eg,

VB.NET:
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim regEx As New System.Text.RegularExpressions.Regex("^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$")
        If regEx.IsMatch("test@test.com") = True Then
            MessageBox.Show("Email is valid")
        Else
            MessageBox.Show("Email is Invalid")
        End If
    End Sub

End Class

The above example shows a basic email validation in .NET 2.0. You may also search for other patterns in http://regexlib.com/Search.aspx?k=email . Also check http://www.regular-expressions.info/ Hope this will help you.
 
Also remember to post VB.NET code here, sergiotome. Welcome to VB.Net Forums. Thread moved to more appropriate forum.
 
Back
Top