Solved

VB Email Check

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

860 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