How do I determine if an SMTP server "advertises" StartTLS?

Robert Gustafson 606 Reputation points
2021-01-21T02:12:22.607+00:00

WHAT I HAVE:
Visual Basic 2019, .NET 4.6.1, WinForms

MY PROBLEM:
I am using SmtpClient to send emails, and the documentation says that if EnableSsl is True when the server doesn't advertise StartTLS, an SmtpException is thrown. I'd like to handle that scenario intelligently.

  1. What is the StatusCode property value for when a send fails for that specific reason? The help info is a little ambiguous on that.
  2. Better yet, is there a way to determine in advance whether a server supports StartTLS and therefore preempt this kind of Smtp exception by setting EnableSsl accordingly?

Please answer both questions ASAP. I'd prefer it in VB.NET, and as simple as possible. (That's what the "S" in SMTP stands for, after all!)

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,903 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,737 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.