Programming Email Verification

Using EmailValidator

EmailValidator provides full support for validating email addresses. With the help of the EmailValidator class, different types of validation can be performed, including email syntax checking, email domain checking and checking user accounts with mail servers. The ValidationPolicy enumeration is used to set the validation policy level:

  • SyntaxOnly validates the email address syntax.
  • SyntaxAndDomain validates the email address syntax, then validate the domain.

Basic Validation Functionality

Use EmailValidator to verify the validity of email addresses.

Validating Emails

Aspose.Email’s validation functionality can be used to validate email addresses, domain names and mail servers. The following code snippet shows you how to use EmailValidator to validate an email address.

EmailValidator ev = new EmailValidator();
ValidationResult[] result = new ValidationResult[] { null };
ev.validate("user@domain.com", result);
if (result[0].getReturnCode() == ValidationResponseCode.ValidationSuccess)
{
    System.out.println("the email address is valid.");
}
else
{
    System.out.println("the mail address is invalid,for the " + result[0].getMessage());
}

Validate Email Messages

This functionality allows users to validate message files, ensuring adherence to specified formats and structures. It supports validation for files/streams in the following formats:

  • MIME Formats: eml, emlx, mht
  • MAPI Formats: msg, oft

Aspose.Email provides the following tools to perform the task:

  • MessageValidator.validate method - validate messages using this method, providing a file path or stream as input.
  • MessageValidationResult class - encapsulates the results of the message validation process. Provides insights into the success of the validation, format type, and any encountered errors.
  • MessageValidationErrorType Enum - Enumerates different types of validation errors.

The code sample below demonstrates how to use these tools for message validation:

MessageValidationResult result = MessageValidator.validate(fileName);

// Check if validation is successful
if (!result.isSuccess()) {
    System.out.println("Validation failed.");

    // Check the format type
    if (result.getFormatType() == FileFormatType.Mht) {
        System.out.println("Format type is Mht.");
    }

    // Check and display errors
    System.out.println("Number of errors: " + result.getErrors().size());

    for (MessageValidationError error : result.getErrors()) {
        System.out.println("Error Type: " + error.getErrorType());
        System.out.println("Description: " + error.getDescription());
    }
} else {
    System.out.println("Validation successful.");
}