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);
}
Related videos on Youtube
Author by
jz jz
Updated on June 06, 2022Comments
-
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 almost 9 yearsI 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 almost 9 yearsSo it seems there is a way to go deeper than with StatusCode. But i can't see exactly how.
-
Peter Schneider almost 9 yearsYou might also catch a
SmtpFailedRecipientException
. See this blog post. There's also another SO question...