开发者

c# MailMessage class - How to verify server carried out the message

开发者 https://www.devze.com 2023-03-25 06:01 出处:网络
using (MailMessage message = new MailMessage()) { message.From = new MailAddress(userEmail.InnerText); message.To.Add(new MailAddress(\"myemail@email.com\"));
using (MailMessage message = new MailMessage())
                {
                    message.From = new MailAddress(userEmail.InnerText);
                    message.To.Add(new MailAddress("myemail@email.com"));
                                      message.Subject = selection.Text;
                    message.Body = htmlBody();
                    message.IsBodyHtml = true;
          开发者_C百科          SmtpClient client = new SmtpClient("142.16.0.142"); 
                    client.Send(message);

                }

How do I verify client.send(message) succeded?


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


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.


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)


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消