asp.net – 有没有办法使用System.Net.Mail.SendAsync()捕获异常
发布时间:2020-12-16 06:23:21 所属栏目:asp.Net 来源:网络整理
导读:我已经有几种方法可以同步发送电子邮件. 如果电子邮件失败,我使用这个相当标准的代码: static void CheckExceptionAndResend(SmtpFailedRecipientsException ex,SmtpClient client,MailMessage message) { for (int i = 0; i ex.InnerExceptions.Length -1;
我已经有几种方法可以同步发送电子邮件.
如果电子邮件失败,我使用这个相当标准的代码: static void CheckExceptionAndResend(SmtpFailedRecipientsException ex,SmtpClient client,MailMessage message) { for (int i = 0; i < ex.InnerExceptions.Length -1; i++) { var status = ex.InnerExceptions[i].StatusCode; if (status == SmtpStatusCode.MailboxBusy || status == SmtpStatusCode.MailboxUnavailable || status == SmtpStatusCode.TransactionFailed) { System.Threading.Thread.Sleep(3000); client.Send(message); } } } 但是,我正在尝试使用SendAsync()实现相同的目标.这是我到目前为止的代码: public static void SendAsync(this MailMessage message) { message.ThrowNull("message"); var client = new SmtpClient(); // Set the methods that is called once the event ends client.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback); // Unique identifier for this send operation string userState = Guid.NewGuid().ToString(); client.SendAsync(message,userState); // Clean up message.Dispose(); } static void SendCompletedCallback(object sender,AsyncCompletedEventArgs e) { // Get the unique identifier for this operation. String token = (string)e.UserState; if (e.Error.IsNotNull()) { // Do somtheing } } 问题是使用令牌和/或e.Error如何获取异常,以便我可以对StatusCode进行必要的检查然后重新发送? 我整个下午一直在谷歌搜索,但没有找到任何积极的东西. 任何建议表示赞赏 解决方法
e.Error已经在发送电子邮件异步时发生了异常.您可以检查Exception.Message,Exception.InnerException,Exception.StackTrace等,以获取更多详细信息.
更新: 检查Exception是否为SmtpException类型,如果是,则可以查询StatusCode.就像是 if(e.Exception is SmtpException) { SmtpStatusCode code = ((SmtpException)(e.Exception)).StatusCode; //and go from here... } 和check here了解更多细节. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – Sitecore:添加到此处按钮未显示在占位符上
- asp.net-mvc-3 – 在Asp.net MVC中为optgroup功能使用Helpe
- asp.net – 如何处理从“DBNull”类型到“String”类型的转
- 我可以这样修改ASP.NET会话对象吗?
- asp.net – 在VS2008发布网站后,全球资源无法解决
- asp.net – 在asp:超链接中分配声明值的问题 错误:这不是
- 【总结 Anchor-free1】Anchor-Free Keypoint方法总结以及思
- asp.net-web-api – spotify请求的限制
- asp.net-mvc – F#Global.asax – 语言不受支持?
- asp.net – system.web.compilation.debug与system.codedom
推荐文章
站长推荐
- asp.net – ASP:ItemTemplate中的DropDownList:
- WeihanLi.Npoi 1.10.0 更新日志
- asp.net – 为jQuery寻找一个好的数据网格插件
- asp.net-mvc – 如何显示图像从路径在asp.net mv
- vbscript – 服务器端注释:ASP Classic中相当于
- .net – 基于属性值的Ninject条件绑定
- 代码翻译:ASP.NET Server.Transfer in PHP
- 将ASP.NET应用程序连接到QuickBooks Online Edit
- asp.net – 为什么removeServerHeader在Azure We
- asp.net – 生成本地资源后缺少服务器标签
热点阅读