因此,我有一个带有帐户创建屏幕的ASP.NET应用程序,用户可以在其中插入其数据来创建帐户。它尤其要求用户的电子邮件地址,这对于接收密码至关重要。为了验证插入的电子邮件地址,我使用了正则表达式来检查格式是否可接受。但是,用户仍然可以编写不存在的随机电子邮件(例如:“ sfsag@asfa.sfa.saf”)。无论电子邮件是否存在,SmtpClient的“发送”方法都会向其发送邮件。为了避免这些情况,我试图捕获异常(SmtpException,SmtpFailedRecipientsException和普通的Exception),但是无论电子邮件地址实际上是否有效,SmtpClient都会忽略该异常并发送电子邮件。
以下是有问题的代码:
public static bool MailPassword(String email, String pwd)
{
try
{
MailMessage msg = new MailMessage
{
From = new MailAddress("admin@candidaturas.com")
};
msg.To.Add(email);
msg.Subject = "Password de Acesso";
msg.Body = "A password de acesso para a sua conta é a seguinte: " + pwd;
msg.IsBodyHtml = true;
SmtpClient smt = new SmtpClient
{
Host = Constants.Host,
Port = Constants.Port
};
System.Net.NetworkCredential ntwd = new NetworkCredential
{
UserName = Constants.Email,
Password = Constants.Password
};
smt.UseDefaultCredentials = false;
smt.Credentials = ntwd;
smt.EnableSsl = true;
smt.Send(msg);
return true;
}
catch(SmtpFailedRecipientsException smtpe)
{
Console.WriteLine("Error: {0}", smtpe.StatusCode);
return false;
}
catch (SmtpException smtpe)
{
Console.WriteLine("Error: {0}", smtpe.StatusCode);
return false;
}
catch (Exception e)
{
Console.WriteLine("Exception caught in RetryIfBusy(): {0}", e.ToString());
return false;
}
}