Click or drag to resize
MimeKit

SmtpClientIsConnected Property

Get whether or not the client is currently connected to an SMTP server.

Namespace: MailKit.Net.Smtp
Assembly: MailKit (in MailKit.dll) Version: 4.3.0
Syntax
C#
public override bool IsConnected { get; }

Property Value

Boolean
true if the client is connected; otherwise, false.

Implements

IMailServiceIsConnected
IMailServiceIsConnected
Remarks

The IsConnected state is set to true immediately after one of the Connect methods succeeds and is not set back to false until either the client is disconnected via Disconnect(Boolean, CancellationToken) or until an SmtpProtocolException is thrown while attempting to read or write to the underlying network socket.

When an SmtpProtocolException is caught, the connection state of the SmtpClient should be checked before continuing.

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