确定System.Net.Mail.SmtpClient.Send结果

编程入门 行业动态 更新时间:2024-10-27 23:27:06
本文介绍了确定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/en-us/library/system.mail.smtpstatuscode(v=vs.80).aspx 。分解是否正确?还是有更好的方法呢?

I have caught all the exceptions from Send and split out the SmtpException.StatusCodes from msdn.microsoft/en-us/library/system.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结果

本文发布于:2023-08-07 10:19:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1318586.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Net   System   Mail   Send   SmtpClient

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!