Find EMail Address in Text - July 2013

If you download this tip please consider making a modest donation.

I created a web application that sends emails to a list of about 10,000 customers.  A small percentage of the emails are rejected for one reason or another. I use the text returned by the server and extract the email address then use that address to report the customer's email address has bounced. The email address is used to find the customer in the database.

I needed some VB.Net code to extract the email address from the rejection message. I searched Google and found some VB.Net code that uses regular expressions but none that use native VB.Net code. I am reluctant to use regular expressions. So I wrote some VB.Net code and encapsulated it in a class module named cFindEMailAddressInText.vb. It becomes the July 2013 tip-of-the-month. I wrote a demo to show how to use the code. Click here to run the demo.

The Demo Application

The Asp.Net, VB.Net, VS/2008 project FindEMailAddress is documented here.

This is the FindEMailAddress initial page.
Enter some text that contains an email address then click the Find... button.

The resulting email address is displayed.

Errata

First EMail Address - Only the first email address is returned.

How Does It Work - The text is searched for an At Sign (@). If found then a forward search from that position is performed looking for a blank. This results in the last part of the email being captured. Then a backward search from the At Sign is performed looking for a blank. This results in the first part of the email being captured. Special characters are removed from the start of the first part and the end of the last part. Characters period, comma, single and double quote, and question mark are removed. The less than sign and greater than signs are also removed.

Surround EMail Address - If checked the less than and greater than signs are added to the returned email address.

Calling the Function - The function signature:

Public Function FindEMailAddress(ByVal sText As String, _
                   Optional ByVal bAddLessThanAndGreaterThan As Boolean = False) As String

Validate the EMail Address - The function calls class module cEmail.vb which validates the email address using method IsValidEMail.

Include in Your Application - Include class modules cFindEMailAddressInText.vb and cEmail.vb in your application.

Possible Error Messages

Error Message
No text entered.
No At Sign (@) found in the text.
EMail address not found.
Email address contains spaces
Email address contains an ending special character.
EMail address is invalid.
EMail address is nothing.

Download the VB.Net Code

Download the source code by clicking on the zipped file name. Click here to run the demo application.

Download - Source Zipped File Size
VS/2008 VB.Net, ASP.Net Solution FindEMailAddress.zip 84k

Previous Tip    Tip Index    Next Tip

Home

Updated 2013/08/17