Click or drag to resize
MimeKit

SmtpCommandException Class

An SMTP protocol exception.
Inheritance Hierarchy
SystemObject
  SystemException
    MailKitCommandException
      MailKit.Net.SmtpSmtpCommandException

Namespace: MailKit.Net.Smtp
Assembly: MailKit (in MailKit.dll) Version: 4.3.0
Syntax
C#
[SerializableAttribute]
public class SmtpCommandException : CommandException

The SmtpCommandException type exposes the following members.

Constructors
 NameDescription
Protected methodSmtpCommandException(SerializationInfo, StreamingContext) Initializes a new instance of the SmtpCommandException class.
Public methodSmtpCommandException(SmtpErrorCode, SmtpStatusCode, String) Initializes a new instance of the SmtpCommandException class.
Public methodSmtpCommandException(SmtpErrorCode, SmtpStatusCode, MailboxAddress, String) Initializes a new instance of the SmtpCommandException class.
Public methodSmtpCommandException(SmtpErrorCode, SmtpStatusCode, String, Exception) Initializes a new instance of the SmtpCommandException class.
Public methodSmtpCommandException(SmtpErrorCode, SmtpStatusCode, MailboxAddress, String, Exception) Initializes a new instance of the SmtpCommandException class.
Top
Properties
 NameDescription
Public propertyData
(Inherited from Exception)
Public propertyCode exampleErrorCode Gets the error code which may provide additional information.
Public propertyHelpLink
(Inherited from Exception)
Public propertyHResult
(Inherited from Exception)
Public propertyInnerException
(Inherited from Exception)
Public propertyCode exampleMailbox Gets the mailbox that the error occurred on.
Public propertyMessage
(Inherited from Exception)
Public propertySource
(Inherited from Exception)
Public propertyStackTrace
(Inherited from Exception)
Public propertyCode exampleStatusCode Gets the status code returned by the SMTP server.
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 When overridden in a derived class, sets the SerializationInfo with information about the exception.
(Overrides ExceptionGetObjectData(SerializationInfo, StreamingContext))
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 an SMTP command fails. Unlike a SmtpProtocolException, a SmtpCommandException does not require 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