我使用SmtpClient.Send Method發送電子郵件,但是當我嘗試發送到一個不存在的電子郵件帳戶時,Send方法不會引發異常,我怎麼能告訴收件人郵件地址有效,或郵件不會到達,或檢測到此問題。當收到不正確的電子郵件地址時,Smtp.Send拋出錯誤
代碼:
Properties.Settings appSettings = new Properties.Settings();
MailMessage message = new MailMessage();
message.From = new MailAddress(appSettings.senderMail);
message.Subject = appSettings.mailsubj;
message.Body = appSettings.mailbody;
Attachment attach = new Attachment(sOutput);
message.Attachments.Add(attach);
SmtpClient client = new SmtpClient(appSettings.SMTP);
client.SendCompleted += this.SendCompletedCallback;
foreach (String clientEmail in clientEmailList)
{
message.To.Clear();
message.To.Add(clientEmail);
//client.Send(message);
try
{
client.Send(message);
AddToLog(String.Format("\t{0}{1}", Properties.Resources.ResourceManager.GetString("MAIL_SEND_SUCCESS"), clientEmail));
}
catch (SmtpFailedRecipientException ex)
{
AddToLog(String.Format("\t{0}{1}:\n\t{2}", Properties.Resources.ResourceManager.GetString("MAIL_SEND_ERROR"), clientEmail, ex.Message));
}
catch (SmtpException smtp_Ex)
{
AddToLog(String.Format("\t{0}{1}:\t{2}", Properties.Resources.ResourceManager.GetString("MAIL_SEND_ERROR"), clientEmail, "Cannot connect to SMTP server."));
}
catch (Exception ex)
{
AddToLog(String.Format("\t{0}{1}:\n\t{2}", Properties.Resources.ResourceManager.GetString("MAIL_SEND_ERROR"), clientEmail, ex.Message));
}
// client.SendAsync(message, clientEmail);
}
[在.NET中捕獲SMTP錯誤]的可能的重複(http://stackoverflow.com/questions/5052282/capture-smtp-errors-in-net) –
參見http://stackoverflow.com/a/ 16724477/56778 –