c# MailMessage 类 - 如何验证服务器是否执行了消息
using (MailMessage message = new MailMessage()) { message.From = new MailAddress(userEmail.InnerText); message.To.Add(new MailAddress("[email protected]")); message.Subject = selection.Text; message.Body = htmlBody(); message.IsBodyHtml = true; SmtpClient client = new SmtpClient("142.16.0.142"); client.Send(message); }
如何验证 client.send(message) 是否成功?
using (MailMessage message = new MailMessage()) { message.From = new MailAddress(userEmail.InnerText); message.To.Add(new MailAddress("[email protected]")); message.Subject = selection.Text; message.Body = htmlBody(); message.IsBodyHtml = true; SmtpClient client = new SmtpClient("142.16.0.142"); client.Send(message); }
How do I verify client.send(message) succeded?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
将
Send()
方法放入 try..catch 块中并捕获SmtpFailedRecipientsException
:Put
Send()
method inside try..catch block and catchSmtpFailedRecipientsException
:您无法真正确定消息是否已成功。没有确切的方法来验证消息是否已送达。将语句包含在 try catch 块中,然后查看它是否捕获异常。
“ Send 将电子邮件发送到接受的收件人,然后引发 SmtpFailedRecipientsException。该异常将包含被拒绝的收件人的列表。”您可以参考http://msdn.microsoft.com/en-us/library/ swas0fwc.aspx 了解更多信息。
You can not really be sure that the message was succeeded. There is no exact way to verify that the message was delivered. Enclose the statements in a try catch block and see if it catches an exception.
" Send sends e-mail to the accepted recipients and then a SmtpFailedRecipientsException is thrown. The exception will contain a listing of the recipients that were rejected." You can refer http://msdn.microsoft.com/en-us/library/swas0fwc.aspx for more.
无法在不发送消息的情况下测试 SMTP 服务器是否已启动并运行。
您唯一可以做的就是将
.Send
包装在 try/catch 块中,以便在失败时采取操作(日志记录/等)there is no way to test ifthe SMTP server is up and running with out sending a message.
the only thing you can to is wrap the
.Send
in a try/catch block to take an action in the case it fails(logging/etc)您可以使用 try-catch 块来捕获引发的任何异常,其中包括 SMTP 拒绝消息。但是,如果 SMTP 服务器接受邮件进行投递,则连接将关闭并显示成功,但无法知道邮件是否已实际投递。在邮件客户端中,已读回执是检查邮件是否已成功发送的唯一真正方法。
You can use a try-catch block to catch any exceptions thrown which will include SMTP refusal messages. However, if the SMTP server accepts the mail for delivery, then the connection is closed and appears successful but there is no way to know if it was actaully delivered. In mail clients, read-receipts are the only true way to check if a mail was delivered successfully.