确定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错误。