Solved

VB Email Check

Posted on 2014-11-13
2
242 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 13

Expert Comment

by:frankhelk
Comment Utility
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 32

Accepted Solution

by:
it_saige earned 500 total points
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

743 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