这是我的代码。我用它来验证电子邮件地址。
using System;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using Vishwaprani.ContextClass;
using Vishwaprani.Models;
using System.Net.Mail;
using System.Threading;
namespace Vishwaprani.CustomValidator
{
public class EmailAddresValidationAttribute : ValidationAttribute
{
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
if (value != null)
{
string mailadress = Convert.ToString(value);
MailMessage email = new MailMessage(new MailAddress("emailvalidation123456@gmail.com", "Email validation"), new MailAddress(mailadress, "Validating email address"));
email.Subject = "email validation";
email.Body = @"Test for validation";
var client = new SmtpClient("smtp.gmail.com") ;
client.Port=587;
client.EnableSsl=true;
client.Credentials = new System.Net.NetworkCredential("emailvalidation123456", "U50h2#AP2XR@");
try
{
client.Send(email);
}
catch(SmtpFailedRecipientException ex)
{
if(ex.StatusCode==SmtpStatusCode.GeneralFailure||ex.StatusCode==SmtpStatusCode.LocalErrorInProcessing||ex.StatusCode==SmtpStatusCode.ServiceNotAvailable||ex.StatusCode==SmtpStatusCode.TransactionFailed)
{
Thread.Sleep(5000);
try
{
client.Send(email);
}
catch(SmtpFailedRecipientException exc)
{
if (exc.StatusCode == SmtpStatusCode.GeneralFailure || exc.StatusCode == SmtpStatusCode.LocalErrorInProcessing || exc.StatusCode == SmtpStatusCode.ServiceNotAvailable || exc.StatusCode == SmtpStatusCode.TransactionFailed)
{
return new ValidationResult("There is a problem with your email address that can not be resolved. Please check it and try again later or enter another one.");
}
}
}
else if(ex.StatusCode==SmtpStatusCode.ExceededStorageAllocation||ex.StatusCode==SmtpStatusCode.InsufficientStorage)
{
return new ValidationResult("There is a storage issue with the email account. Please resolve that.");
}
else if(ex.StatusCode==SmtpStatusCode.MailboxBusy)
{
Thread.Sleep(5000);
try
{
client.Send(email);
}
catch(SmtpFailedRecipientException exct)
{
if (exct.StatusCode == SmtpStatusCode.MailboxBusy)
{
return new ValidationResult("Mail box is busy two times try again later.");
}
}
}
else if(ex.StatusCode==SmtpStatusCode.MailboxUnavailable)
{
return new ValidationResult("Mail box is unavailable.");
}
else
{
return new ValidationResult("There is an error with the email address you provided. Please check it.");
}
}
return ValidationResult.Success;
}
else return null;
}
}
}
问题是,当我将邮件发送到错误的电子邮件地址时,代码不会抛出SmtpFailedRecipientException
并转到validationresult。成功。无论如何,我对这个SmtpClient的东西很新。有谁知道如何解决这个问题? 。这是我用于验证的错误电子邮件地址:
dffhgjwghxjgwjhxg@gjh3egxejh.comedkjk.
正如你所看到的那样,人们根本不可能存在那种电子邮件地址。
证明(与我的一起检查):
答案 0 :(得分:0)
try
{
address = new MailAddress("emailvalidation123456@gmail.com", "Email validation").Address;
}
catch(FormatException) {
//Invalid email address
}
MailAdress.Address属性在您尝试获取时抛出FormatException。
修改强>
在发送电子邮件之前,您无法验证电子邮件地址是否存在。您可以使用SmtpClient.SendCompleted Event查看投放状态。
client.SendCompleted += new
SendCompletedEventHandler(SendCompletedCallback);
private static void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
{
// Get the unique identifier for this asynchronous operation.
String token = (string)e.UserState;
if (e.Cancelled)
{
Console.WriteLine("[{0}] Send canceled.", token);
}
if (e.Error != null)
{
Console.WriteLine("[{0}] {1}", token, e.Error.ToString());
}
else
{
Console.WriteLine("Message sent.");
}
mailSent = true;
}