Solved

VB Email Check

Posted on 2014-11-13
2
246 Views
Last Modified: 2014-11-28
Hi

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)
            Else
                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;
            }
            else
            {
                //use IsMatch to validate the address
                isEmail = check.IsMatch(email);
            }
            //return the value to the calling method
            return isEmail;
        }
    }
0
Comment
Question by:Kevinfeening
2 Comments
 
LVL 14

Expert Comment

by:frankhelk
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
    Try
        Dim a As New System.Net.Mail.MailAddress(s)
    Catch
        Return False
    End Try
    Return True
End Function

Open in new window

0
 
LVL 33

Accepted Solution

by:
it_saige earned 500 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))));
			Console.ReadLine();
		}

		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);
			else
				return false;
		}
	}
}

Open in new window


Produces the following output:Capture.JPG
-saige-
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

920 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now