确定 System.Net.Mail.SmtpClient.Send 结果
我试图使用枚举总结 System.Net.Mail.SmtpClient.Send
的结果。这样我就知道是否应该重试发送电子邮件并希望防止发送重复的电子邮件。
public enum MailSendStatus {
None,
Sent,
ErrorCannotSend,
TryAgain,
SentMaybe
}
我已捕获来自 Send
的所有异常,并从 http://msdn.microsoft.com/en-us/library/system.net.mail.smtpstatuscode(v =vs.80).aspx。分解看起来正确吗?或者有更好的方法来做到这一点吗?
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;
I am trying to summarize the outcomes from System.Net.Mail.SmtpClient.Send
using an enum. This is so I know whether I should retry sending the email and hopefully prevent duplicate emails being sent.
public enum MailSendStatus {
None,
Sent,
ErrorCannotSend,
TryAgain,
SentMaybe
}
I have caught all the exceptions from Send
and split out the SmtpException.StatusCode
s from http://msdn.microsoft.com/en-us/library/system.net.mail.smtpstatuscode(v=vs.80).aspx. Does the breakdown look right? Or is there a better way to do this?
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;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不喜欢这个。 ArgumentNull、ObjectDispose 是编程错误(InvalidOperation 也是如此)。您不应将它们分解为 SMTP 错误,而应将其修复。因此,让程序崩溃是件好事(并输出堆栈跟踪)。方法是“快速失败”。不要重新抛出您不知道如何处理的异常,InvalidOperationException、ObjectDisposeException 表示状态有问题,ArbumentNullException 是用法/ui 错误。
I dont like this. ArgumentNull, ObjectDisposed are programming errors (as is InvalidOperation). You should not break them down to a SMTP error but have them fixed. Fpr this, crashing the program is good (and putting out a stack trace). Approach "fail fast". Dont rethriow exceptions you dont know how to handle, and InvalidOperationException, ObjectDisposedException indicate something is wrong with the state, ArbumentNullException is a usage / ui error.