如何解决确定System.Net.Mail.SmtpClient.Send结果
|| 我试图使用一个枚举来总结ѭ0的结果。这样一来,我知道是否应该重试发送电子邮件,并希望防止重复发送电子邮件。public enum MailSendStatus {
None,Sent,ErrorCannotSend,TryAgain,SentMaybe
}
我已经捕获了来自ѭ2的所有异常,并从http://msdn.microsoft.com/en-us/library/system.net.mail.smtpstatuscode(v=vs.80).aspx拆分了SmtpException.StatusCode
。细分看起来正确吗?还是有更好的方法来做到这一点?
try {
smtp.Send(msg);
} catch (ArgumentNullException e) {
return MailSendStatus.ErrorCannotSend;
} catch (ObjectdisposedException e) {
return MailSendStatus.ErrorCannotSend;
} catch (InvalidOperationException e) {
return MailSendStatus.ErrorCannotSend;
} catch (SmtpFailedRecipientsException e) {
return MailSendStatus.ErrorCannotSend;
} catch (SmtpException e) {
switch(e.StatusCode) {
case SmtpStatusCode.BadCommandSequence:
case SmtpStatusCode.MailBoxNameNotAllowed:
case SmtpStatusCode.HelpMessage:
case SmtpStatusCode.SyntaxError:
case SmtpStatusCode.SystemStatus:
return MailSendStatus.ErrorCannotSend;
case SmtpStatusCode.CannotVerifyUserWillAttemptDelivery:
case SmtpStatusCode.UserNotLocalWillForward:
return MailSendStatus.SentMaybe;
case SmtpStatusCode.ClientNotPermitted:
case SmtpStatusCode.CommandNotImplemented:
case SmtpStatusCode.CommandParameterNotImplemented:
case SmtpStatusCode.CommandUnrecognized:
case SmtpStatusCode.ExceededStorageAllocation:
case SmtpStatusCode.GeneralFailure:
case SmtpStatusCode.InsufficientStorage:
case SmtpStatusCode.LocalErrorInProcessing:
case SmtpStatusCode.MailBoxBusy:
case SmtpStatusCode.MailBoxUnavailable:
case SmtpStatusCode.MustIssueStartTlsFirst:
case SmtpStatusCode.ServiceClosingTransmissionChannel:
case SmtpStatusCode.ServiceNotAvailable:
case SmtpStatusCode.ServiceReady:
case SmtpStatusCode.StartMailInput:
case SmtpStatusCode.TransactionFailed:
case SmtpStatusCode.UserNotLocalTryAlternatePath:
return MailSendStatus.TryAgain;
case SmtpStatusCode.Ok:
break;
}
} catch (Exception e) {
return MailSendStatus.SentMaybe;
}
return MailSendStatus.Sent;
解决方法
catch (ArgumentNullException e) { return MailSendStatus.ErrorCannotSend;} catch
(ObjectDisposedException e) { return MailSendStatus.ErrorCannotSend;} catch
(InvalidOperationException e) { return MailSendStatus.ErrorCannotSend;
我不喜欢这样。 ArgumentNull,ObjectDisposed是编程错误(InvalidOperation也是)。您不应将其分解为SMTP错误,而应将其修复。对此,使程序崩溃是好的(并提供堆栈跟踪)。接近“快速失败”。不要取消不知道如何处理的异常,InvalidOperationException,ObjectDisposedException指示状态有问题,ArbumentNullException是用法/ ui错误。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。