EmailAddressValidatorVerifying Event |
Occurs when an e-mail address in a bulk series is about to be verified.
Namespace: MailBee.AddressCheckAssembly: MailBee.NET (in MailBee.NET.dll) Version: 12.5.0 build 687 for .NET 4.5
Syntax Remarks You can use this event to filter any e-mail addresses from the bulk of addresses to be verified, such
as to implement a whitelist or blacklist. Set VerifyIt to skip the particular address from verification.
Note |
---|
This event is never raised by Verify(String) overload which processes a single address only (rather than a bulk). |
Note |
---|
This event is not available for .NET Core and UWP. |
Examples This sample demonstrates how to implement blacklist and whitelist. Data source is DataTable.
To see how to use this event in case of IDataReader, see Verify(IDataReader, String) sample.
Note |
---|
For clarity, this sample marks e-mail addresses as bad or good based on a single verification attempt.
In real apps, you should use multi-pass approach. See Getting started with Address Validator guide
(Examining the results section) for a more accurate and advanced sample. |
using System;
using System.Data;
using MailBee;
using MailBee.AddressCheck;
class Program
{
const string EmailColumn = "email";
const string StatusColumn = "good";
private static DataTable GetDataTable()
{
DataTable workTable = new DataTable();
workTable.Columns.Add(EmailColumn, typeof(string));
workTable.Columns.Add(StatusColumn, typeof(bool));
string[] emails = new string[] { "jdoe@good.com", "no.dot.in@domain",
"bob@example.com", "user@local-domain", "alice@bad.com" };
foreach (string email in emails)
{
DataRow row = workTable.NewRow();
row[EmailColumn] = email;
row[StatusColumn] = false;
workTable.Rows.Add(row);
}
return workTable;
}
private static void valid_Verifying(object sender, VerifyingEventArgs e)
{
try
{
Console.WriteLine("*** Verifying ***");
Console.WriteLine("Email: " + e.Email);
if (e.Email.EndsWith("@bad.com"))
{
e.VerifyIt = false;
e.Row[StatusColumn] = false;
}
else if (e.Email.EndsWith("@good.com") ||
e.Email.EndsWith("@local-domain"))
{
e.VerifyIt = false;
e.Row[StatusColumn] = true;
}
if (!e.VerifyIt)
{
Console.WriteLine(
string.Format("{0} status detected as {1} without verification.",
e.Email, (bool)e.Row[StatusColumn] ? "good" : "bad"));
}
else
{
Console.WriteLine(
string.Format("{0} is neither on blacklist nor on whitelist.", e.Email));
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
Console.WriteLine();
}
private static void valid_Verified(object sender, VerifiedEventArgs e)
{
try
{
Console.WriteLine("*** Verified ***");
Console.WriteLine("Email: " + e.Email);
Console.WriteLine("Result: " + e.Result.ToString());
if (e.Reason != null)
{
Console.WriteLine("Reason: " + e.Reason.Message);
}
bool isAddressOK = (e.Result == AddressValidationLevel.OK);
if ((bool)e.Row[StatusColumn] != isAddressOK)
{
e.Row[StatusColumn] = isAddressOK;
}
Console.WriteLine(string.Format("{0} status detected as {1} after verification.",
e.Email, (bool)e.Row[StatusColumn] ? "good" : "bad"));
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
Console.WriteLine();
}
static void Main(string[] args)
{
EmailAddressValidator valid = new EmailAddressValidator("MN110-0123456789ABCDEF-0123");
valid.DnsServers.Autodetect();
valid.Log.Enabled = true;
valid.Log.Filename = @"C:\Temp\log.txt";
valid.Log.Clear();
valid.Verifying += new VerifyingEventHandler(valid_Verifying);
valid.Verified += new VerifiedEventHandler(valid_Verified);
valid.Verify(GetDataTable(), EmailColumn);
}
}
Imports System.Data
Imports MailBee
Imports MailBee.AddressCheck
Class Program
Const EmailColumn As String = "email"
Const StatusColumn As String = "good"
Private Shared Function GetDataTable() As DataTable
Dim workTable As New DataTable()
workTable.Columns.Add(EmailColumn, GetType(String))
workTable.Columns.Add(StatusColumn, GetType(Boolean))
Dim emails As String() = New String() {"jdoe@good.com", "no.dot.in@domain", _
"bob@example.com", "user@local-domain", "alice@bad.com"}
For Each email As String In emails
Dim row As DataRow = workTable.NewRow()
row(EmailColumn) = email
row(StatusColumn) = False
workTable.Rows.Add(row)
Next
Return workTable
End Function
Private Shared Sub valid_Verifying(ByVal sender As Object, ByVal e As VerifyingEventArgs)
Try
Console.WriteLine("*** Verifying ***")
Console.WriteLine("Email: " & e.Email)
If e.Email.EndsWith("@bad.com") Then
e.VerifyIt = False
e.Row(StatusColumn) = False
ElseIf e.Email.EndsWith("@good.com") OrElse e.Email.EndsWith("@local-domain") Then
e.VerifyIt = False
e.Row(StatusColumn) = True
End If
If Not e.VerifyIt Then
Console.WriteLine( _
String.Format("{0} status detected as {1} without verification.", _
e.Email, If(CBool(e.Row(StatusColumn)), "good", "bad")))
Else
Console.WriteLine( _
String.Format("{0} is neither on blacklist nor on whitelist.", e.Email))
End If
Catch ex As Exception
Console.WriteLine(ex.ToString())
End Try
Console.WriteLine()
End Sub
Private Shared Sub valid_Verified(ByVal sender As Object, ByVal e As VerifiedEventArgs)
Try
Console.WriteLine("*** Verified ***")
Console.WriteLine("Email: " & e.Email)
Console.WriteLine("Result: " & e.Result.ToString())
If Not (e.Reason Is Nothing) Then
Console.WriteLine("Reason: " & e.Reason.Message)
End If
Dim isAddressOK As Boolean = (e.Result = AddressValidationLevel.OK)
If CBool(e.Row(StatusColumn)) <> isAddressOK Then
e.Row(StatusColumn) = isAddressOK
End If
Console.WriteLine(String.Format("{0} status detected as {1} after verification.", _
e.Email, If(CBool(e.Row(StatusColumn)), "good", "bad")))
Catch ex As Exception
Console.WriteLine(ex.ToString())
End Try
Console.WriteLine()
End Sub
Shared Sub Main(ByVal args As String())
Dim valid As New EmailAddressValidator("MN110-0123456789ABCDEF-0123")
valid.DnsServers.Autodetect()
valid.Log.Enabled = True
valid.Log.Filename = "C:\Temp\log.txt"
valid.Log.Clear()
AddHandler valid.Verifying, AddressOf valid_Verifying
AddHandler valid.Verified, AddressOf valid_Verified
valid.Verify(GetDataTable(), EmailColumn)
End Sub
End Class
See Also