Solved

VB Email Check

Posted on 2014-11-13
2
259 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Suggested Solutions

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 …
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

733 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