Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


VB Email Check

Posted on 2014-11-13
Medium Priority
Last Modified: 2014-11-28

The product I am using has an Email test and I need it to check if an Underscore _ is used in the Customer Address and validate it but it always returns isValidEmail(CustomerEmail) as False

from the VB.Net Code

If EmailTemplates.Email.IsValidEmail(FromEmailAddress) AndAlso EmailTemplates.Email.IsValidEmail(CustomerEmail) Then
            If String.IsNullOrEmpty(AttachmentType) AndAlso String.IsNullOrEmpty(AttachmentName) AndAlso Attachment Is Nothing Then
                'There is no attachements
                EmailTemplates.Email.SendMessage(EmailTemplate.EmailBody, CustomerEmail, Nothing, Nothing, EmailTemplate.EmailSubject, FromEmailAddress, EmailRetentionPeriod, SourceSystemId)
                EmailTemplates.Email.SendMessage(EmailTemplate.EmailBody, CustomerEmail, Nothing, Nothing, EmailTemplate.EmailSubject, FromEmailAddress, EmailRetentionPeriod, SourceSystemId, AttachmentType, AttachmentName, Attachment)
            End If
        End If

It goes to a section in a File with a .cs extension

public static bool IsValidEmail(string email)

            bool isEmail = Regex.IsMatch(email, @"\A(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)\Z", RegexOptions.IgnoreCase);
            //make sure an email address was provided
            if (string.IsNullOrEmpty(email))
                isEmail = false;
                //use IsMatch to validate the address
                isEmail = check.IsMatch(email);
            //return the value to the calling method
            return isEmail;
Question by:Kevinfeening
LVL 14

Expert Comment

ID: 40442012
I havn't checked the code of yours, but .NET framework has a built in feature for checking email addresses. The MailAddress class checks the validity of the address on instantiation of the object and throws an exception if sth's wrong with it. You may additionally check for the _ outside of the function or within ...
Public Function IsValidEmailFormat(ByVal s As String) As Boolean
        Dim a As New System.Net.Mail.MailAddress(s)
        Return False
    End Try
    Return True
End Function

Open in new window

LVL 35

Accepted Solution

it_saige earned 2000 total points
ID: 40443678
I corrected a problem with the regular expression, but there seems to be a piece missing in this puzzle.  I rewrote the IsValidEmail because I could not find where check was being defined at.

Example program -
using System;
using System.Text.RegularExpressions;

namespace EE_Q28561667
	class Program
		static void Main(string[] args)
			Console.WriteLine("Using the corrected Regular Expression");
			Console.WriteLine(string.Format("Is bill@microsoft.com a valid email address: {0}", IsValidEmail("bill@microsoft.com")));
			Console.WriteLine(string.Format("Is bill@microsoft a valid email address: {0}", IsValidEmail("bill@microsoft")));
			Console.WriteLine(string.Format("Is bill@.com a valid email address: {0}", IsValidEmail("bill@.com")));
			Console.WriteLine(string.Format("Is b_ill@microsoft.com a valid email address: {0}", IsValidEmail("b_ill@microsoft.com")));
			Console.WriteLine(string.Format("Is bill@m_icrosoft.com a valid email address: {0}", IsValidEmail("bill@m_icrosoft.com")));
			Console.WriteLine(string.Format("Is an empty string a valid email address: {0}", IsValidEmail("")));
			Console.WriteLine(string.Format("Is a null string a valid email address: {0}", IsValidEmail(default(string))));

		public static bool IsValidEmail(string email)
			if (!string.IsNullOrEmpty(email))
				return Regex.IsMatch(email, @"\A(?:[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)\Z", RegexOptions.IgnoreCase);
				return false;

Open in new window

Produces the following output:Capture.JPG

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

572 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question