E-Mail Validierung hinzugefügt
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Mail;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using DnsClient;
|
||||
|
||||
namespace System
|
||||
{
|
||||
@@ -137,5 +140,50 @@ namespace System
|
||||
|
||||
return decimal.Parse(decString, culture);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks whether the given <see cref="string"/> is a valid <see cref="MailAddress"/>.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// You can enhance the check by requesting the MX record of the domain.
|
||||
/// </remarks>
|
||||
/// <param name="email">The email address as string to validate.</param>
|
||||
/// <param name="checkRecordExists">A value indicating whether to resolve a MX record.</param>
|
||||
/// <returns><c>true</c> when the email address is valid, other wise <c>false</c>.</returns>
|
||||
public static bool IsValidEmailAddress(this string email, bool checkRecordExists = false)
|
||||
=> email.IsValidEmailAddress(checkRecordExists ? new LookupClient() : null);
|
||||
|
||||
/// <summary>
|
||||
/// Checks whether the given <see cref="string"/> is a valid <see cref="MailAddress"/>.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The check is enhanced by a request for MX records on the defined <paramref name="nameservers"/>.
|
||||
/// </remarks>
|
||||
/// <param name="email">The email address as string to validate.</param>
|
||||
/// <param name="nameservers">A list of <see cref="IPEndPoint"/>s of nameservers.</param>
|
||||
/// <returns><c>true</c> when the email address is valid, other wise <c>false</c>.</returns>
|
||||
public static bool IsValidEmailAddress(this string email, IEnumerable<IPEndPoint> nameservers)
|
||||
=> email.IsValidEmailAddress(new LookupClient(nameservers.ToArray()));
|
||||
|
||||
private static bool IsValidEmailAddress(this string email, LookupClient lookupClient)
|
||||
{
|
||||
try
|
||||
{
|
||||
var mailAddress = new MailAddress(email);
|
||||
bool isValid = mailAddress.Address == email;
|
||||
|
||||
if (isValid && lookupClient != null)
|
||||
{
|
||||
var result = lookupClient.Query(mailAddress.Host, QueryType.MX);
|
||||
isValid &= result.Answers.MxRecords().Any();
|
||||
}
|
||||
|
||||
return isValid;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user