确定System.Net.Mail.SmtpClient.Send结果 [英] Determining System.Net.Mail.SmtpClient.Send result

查看:300
本文介绍了确定System.Net.Mail.SmtpClient.Send结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用枚举来总结 System.Net.Mail.SmtpClient.Send 的结果。这是我知道我是否应该重试发送电子邮件,并希望可以防止发送重复的电子邮件。

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
}

我已经捕获了发送中的所有异常,并分出了 SmtpException.StatusCode s http: //msdn.microsoft.com/en-us/library/system.net.mail.smtpstatuscode(v=vs.80).aspx 。分解是否正确?还是有更好的方法呢?

I have caught all the exceptions from Send and split out the SmtpException.StatusCodes 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;


推荐答案

catch (ArgumentNullException e) {    return MailSendStatus.ErrorCannotSend;} catch 
(ObjectDisposedException e) {    return MailSendStatus.ErrorCannotSend;} catch 
(InvalidOperationException e) {    return MailSendStatus.ErrorCannotSend;

我不喜欢这个。 ArgumentNull,ObjectDisposed是编程错误(和InvalidOperation一样)。您不应该将它们分解为SMTP错误,而是将其修复。 Fpr这个,崩溃的程序是好的(并放出一个堆栈跟踪)。方法快速失败。不要重新排除异常,你不知道如何处理,而InvalidOperationException,ObjectDisposedException表示状态有问题,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.

这篇关于确定System.Net.Mail.SmtpClient.Send结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆