djeeg
djeeg

Reputation: 6765

Determining System.Net.Mail.SmtpClient.Send result

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.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;

Upvotes: 3

Views: 7158

Answers (1)

TomTom
TomTom

Reputation: 62093

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

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.

Upvotes: 2

Related Questions