Handling SmtpClient.Send exceptions

11,675

You can evaluate the StatusCode property (See SmtpStatusCode Enumeration):

try
{
    client.Send(message);
}
catch (SmtpException e)
{
    Console.WriteLine("Error: {0}", e.StatusCode);
}
Share:
11,675

Related videos on Youtube

jz jz
Author by

jz jz

Updated on June 06, 2022

Comments

  • jz jz
    jz jz almost 2 years

    I'm a little confused about how to handle the SmtpClient.send exceptions.

    We have for example the "SmtpException" but what would be the correct way to handle the specific exceptions within. Using the HResult property?

    If so, how could I catch the "invalid user" exception or the "smtp server unreachable" exception?

  • jz jz
    jz jz almost 9 years
    I have tried with the StatusCode evaluation. For example with a wrong smtp ip and a wrong smtp port i get StatusCode:GeneralFailure in both cases. But the InnerException messages are diferent: {"The remote name could not be resolved: 'smtp.gmail.com4'"} and {"Unable to connect to the remote server"}
  • jz jz
    jz jz almost 9 years
    So it seems there is a way to go deeper than with StatusCode. But i can't see exactly how.
  • Peter Schneider
    Peter Schneider almost 9 years
    You might also catch a SmtpFailedRecipientException. See this blog post. There's also another SO question...

Related