I want to check whether an SMTP mailbox is available for a given username and password. I am using the SmtpClient.Send method to send the email, but before sending it I want to check the whether the credentials provided are correct and also to check whether the SMTP server is valid.
How can I do this from C#?
I don’t think the API provides the equivalent of an “is valid” type of query. However, when you look at the documentation, it states that an
SmtpExceptionwill be thrown if the send fails; two of the reasons for failure include what you were looking forSo handling that exception and inspecting it for whether the failure was indeed due to an authentication failure or a server connection failure should provide you with what you need.