Check Email Address

Check Email Address

You may require to verify the email address entered by end user or you may want to get all
Emaill addresses from text. Even you can programmatically crawl the website for email address content.

Here we will see both the scenarios.

  1. Validate Email address.

    Lets see the Code:
using System.Text.RegularExpressions;
protected void Button2_Click(object sender, EventArgs e)
{
string email = "my_email_address@gmail.com";
if(VerifyEmailAddress(email))
{
Response.Write(String.Format("The email address {0} is valid email",email));
}
}
bool VerifyEmailAddress(string email)
{
string emailExpression = @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";     
Regex re = new Regex(emailExpression);
return re.IsMatch(email);
}

Output: The email address my_email_address@gmail.com is valid email

Description: The function VerifyEmailAddress accepts string as an email address parameter.
It validates the given email string against regular expression email pattern.
If it matches the email pattern then it returns true as a Boolean value.
The regular expression for email address is “\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"
Regex class needs System.Text.RegularExpression namespace.

  1. Get Email address from specified URL of website.
    Lets see below example.
protected void Button3_Click(object sender, EventArgs e)
{
string url = "http://www.consumercomplaints.in";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
string rawOutput = reader.ReadToEnd();
MatchCollection mc = GetEmailAddress(rawOutput);
foreach (Match m in mc)
{
Response.Write(m.Value + "</br>");
}
}
MatchCollection GetEmailAddress(string input)
{
string emailExpression = @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";
Regex re = new Regex(emailExpression);
return re.Matches(input);
}

Note: Above example uses System.Net namespace for HttpWebRequest and HttpWebResponse. 
           System.IO is used for StreamReader class.
           You may require to use WebProxy, if you are accessing internet via proxy server of your company
           LAN.

Description: We have used same regular expression which we recently used in listening 1.
The Method GetEmailAddress accepts input string and finds email patterns in it.
It returns all matched collection in input string.

HtttpWebRequest creates request with specified URL from your server to consumercomplaints server.
At server side we are getting response from consumercomplaints.in. This response we are passing
to GetEmailAddress which returns all matched collection of email address.

You can alter regular expression as per your requirement.

Tags:

Checkbox Check all GridviewTemporary disposable emailGridview select emailYahoo email settingsC# send email using System.Net SMTP ClientHow to change your IPasp.net Gridview checkboxRead pop3 email attachments componentC-Send-emailBulk EmailGodaddy email configuration settings

Author

My name is Satalaj, but people call me Sat. Here is my homepage: . I live in Pune, PN and work as a Software Engineer. I'm former MVP in ASP.net year 2010.
Disclaimer: Views or opinion expressed here are my personal research and it has nothing to do with my employer. You are free to use the code, ideas/hints in your projects. However, you should not copy and paste my original content to other web sites. Feel free to copy or extend the code.
If you want to fight with me, this website is not for you.
 

I'm Satalaj.