Quantcast
Channel: .NET Framework Class Libraries forum
Viewing all articles
Browse latest Browse all 8156

Handling "Server does not support secure connections" using an SmtpException.StatusCode?

$
0
0

I would like to attempt to send mail via SSL first, and then only if that fails, revert back to EnableSsl = false and try again.  I would prefer to check the exception for a StatusCode that indicates this, however the StatusCode that is returned is StatusCode.GeneralFailure.  The only way I can determine that it failed because of lack of secure connection support is to check the Exception.Message, which is not ideal.

The resulting code is below.  Any ideas for improvement?

try
        {
            client.Send(oMail);
        }
        catch (System.Net.Mail.SmtpException e)
        {
            //If the server does not support TLS, try again without it.
            //Exception will have a SmtpException.StatusCode.GeneralFailure, so only way to check is to check the message
            if (((System.Net.Mail.SmtpException)e).Message.Contains("secure connections"))
            {
                try
                {
                    client.EnableSsl = false;
                    client.Send(oMail);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            else
            {
                throw e;
            }

        }
        catch (Exception e)
        {
            throw e;
        }


Viewing all articles
Browse latest Browse all 8156

Trending Articles