Click or drag to resize
MimeKit

SmtpProtocolException Class

An SMTP protocol exception.
Inheritance Hierarchy
SystemObject
  SystemException
    MailKitProtocolException
      MailKit.Net.SmtpSmtpProtocolException

Namespace: MailKit.Net.Smtp
Assembly: MailKit (in MailKit.dll) Version: 4.7.1
Syntax
C#
[SerializableAttribute]
public class SmtpProtocolException : ProtocolException

The SmtpProtocolException type exposes the following members.

Constructors
 NameDescription
Public methodSmtpProtocolException Initializes a new instance of the SmtpProtocolException class.
Public methodSmtpProtocolException(String) Initializes a new instance of the SmtpProtocolException class.
Protected methodSmtpProtocolException(SerializationInfo, StreamingContext)Obsolete.
Initializes a new instance of the SmtpProtocolException class.
Public methodSmtpProtocolException(String, Exception) Initializes a new instance of the SmtpProtocolException class.
Top
Properties
 NameDescription
Public propertyData
(Inherited from Exception)
Public propertyHelpLink
(Inherited from Exception)
Public propertyHResult
(Inherited from Exception)
Public propertyInnerException
(Inherited from Exception)
Public propertyMessage
(Inherited from Exception)
Public propertySource
(Inherited from Exception)
Public propertyStackTrace
(Inherited from Exception)
Public propertyTargetSite
(Inherited from Exception)
Top
Methods
 NameDescription
Public methodEquals
(Inherited from Object)
Protected methodFinalize
(Inherited from Object)
Public methodGetBaseException
(Inherited from Exception)
Public methodGetHashCode
(Inherited from Object)
Public methodGetObjectData
(Inherited from Exception)
Public methodGetType
(Inherited from Exception)
Protected methodMemberwiseClone
(Inherited from Object)
Public methodToString
(Inherited from Exception)
Top
Events
 NameDescription
Protected eventSerializeObjectState
(Inherited from Exception)
Top
Remarks
The exception that is thrown when there is an error communicating with an SMTP server. An SmtpProtocolException is typically fatal and requires the SmtpClient to be reconnected.
Example
C#
public static void SendMessage (MimeMessage message)
{
    using (var client = new SmtpClient ()) {
        try {
            client.Connect ("smtp.gmail.com", 465, SecureSocketOptions.SslOnConnect);
        } catch (SmtpCommandException ex) {
            Console.WriteLine ("Error trying to connect: {0}", ex.Message);
            Console.WriteLine ("\tStatusCode: {0}", ex.StatusCode);
            return;
        } catch (SmtpProtocolException ex) {
            Console.WriteLine ("Protocol error while trying to connect: {0}", ex.Message);
            return;
        }

        // Note: Not all SMTP servers support authentication, but GMail does.
        if (client.Capabilities.HasFlag (SmtpCapabilities.Authentication)) {
            try {
                client.Authenticate ("username", "password");
            } catch (AuthenticationException ex) {
                Console.WriteLine ("Invalid user name or password.");
                return;
            } catch (SmtpCommandException ex) {
                Console.WriteLine ("Error trying to authenticate: {0}", ex.Message);
                Console.WriteLine ("\tStatusCode: {0}", ex.StatusCode);
                return;
            } catch (SmtpProtocolException ex) {
                Console.WriteLine ("Protocol error while trying to authenticate: {0}", ex.Message);
                return;
            }
        }

        try {
            client.Send (message);
        } catch (SmtpCommandException ex) {
            Console.WriteLine ("Error sending message: {0}", ex.Message);
            Console.WriteLine ("\tStatusCode: {0}", ex.StatusCode);

            switch (ex.ErrorCode) {
            case SmtpErrorCode.RecipientNotAccepted:
                Console.WriteLine ("\tRecipient not accepted: {0}", ex.Mailbox);
                break;
            case SmtpErrorCode.SenderNotAccepted:
                Console.WriteLine ("\tSender not accepted: {0}", ex.Mailbox);
                break;
            case SmtpErrorCode.MessageNotAccepted:
                Console.WriteLine ("\tMessage not accepted.");
                break;
            }
        } catch (SmtpProtocolException ex) {
            Console.WriteLine ("Protocol error while sending message: {0}", ex.Message);
        }

        client.Disconnect (true);
    }
}
See Also