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.
MailServer policy, which validates an address by connecting to the mail server, is available in Aspose.Email for .NET only. In Aspose.Email for Java the strictest policy is SyntaxAndDomain.
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());
}
Validating Domain
Aspose.Email can verify the validity of the domain part of an address. Pass ValidationPolicy.SyntaxAndDomain to the validate overload that takes a policy: the address syntax is checked first and then the MX records of its domain are resolved. The DNS servers used for the lookup and the lookup timeout can be configured on the validator itself.
EmailValidator validator = new EmailValidator();
// Optional: use specific DNS servers and a custom timeout (in milliseconds)
validator.setDnsServers(new String[] { "8.8.8.8", "8.8.4.4" });
validator.setTimeout(5000);
ValidationResult[] result = new ValidationResult[] { null };
validator.validate("user@domain.com", ValidationPolicy.SyntaxAndDomain, result);
if (result[0].getReturnCode() == ValidationResponseCode.ValidationSuccess) {
System.out.println("The domain is valid.");
} else {
System.out.println("The domain check failed: " + 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.");
}