c# MailMessage 类 - 如何验证服务器是否执行了消息

发布于 2024-11-27 10:09:18 字数 684 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

忆梦 2024-12-04 10:09:18

Send() 方法放入 try..catch 块中并捕获 SmtpFailedRecipientsException

try
{
   client.Send(message);
}
catch(SmtpFailedRecipientsException smtpFex)
{
    string failedRecipient = smtpFex.FailedRecipient;
    // it will return FailedRecipient's emailID. Now check the value of failedRecipient 
    //and take the action/s.
}

Put Send() method inside try..catch block and catch SmtpFailedRecipientsException:

try
{
   client.Send(message);
}
catch(SmtpFailedRecipientsException smtpFex)
{
    string failedRecipient = smtpFex.FailedRecipient;
    // it will return FailedRecipient's emailID. Now check the value of failedRecipient 
    //and take the action/s.
}
绮筵 2024-12-04 10:09:18

您无法真正确定消息是否已成功。没有确切的方法来验证消息是否已送达。将语句包含在 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.

晌融 2024-12-04 10:09:18

无法在不发送消息的情况下测试 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)

情痴 2024-12-04 10:09:18

您可以使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文