Link to home
Start Free TrialLog in
Avatar of K Feening
K FeeningFlag for Australia

asked on

VB Email Check

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;
        }
    }
Avatar of Frank Helk
Frank Helk
Flag of Germany image

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

ASKER CERTIFIED SOLUTION
Avatar of it_saige
it_saige
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial