Click or drag to resize
MimeKit

SmtpClient Class

An SMTP client that can be used to send email messages.
Inheritance Hierarchy
SystemObject
  MailKitMailService
    MailKitMailTransport
      MailKit.Net.SmtpSmtpClient

Namespace: MailKit.Net.Smtp
Assembly: MailKit (in MailKit.dll) Version: 4.3.0
Syntax
C#
public class SmtpClient : MailTransport, ISmtpClient, 
	IMailTransport, IMailService, IDisposable

The SmtpClient type exposes the following members.

Constructors
 NameDescription
Public methodCode exampleSmtpClient Initializes a new instance of the SmtpClient class.
Public methodCode exampleSmtpClient(IProtocolLogger) Initializes a new instance of the SmtpClient class.
Top
Properties
 NameDescription
Public propertyCode exampleAuthenticationMechanisms Get the authentication mechanisms supported by the SMTP server.
(Overrides MailServiceAuthenticationMechanisms)
Public propertyCode exampleCapabilities Get the capabilities supported by the SMTP server.
Public propertyCheckCertificateRevocation Get or set whether connecting via SSL/TLS should check certificate revocation.
(Inherited from MailService)
Public propertyClientCertificates Get or set the client SSL certificates.
(Inherited from MailService)
Public propertyCode exampleDeliveryStatusNotificationType Get or set how much of the message to include in any failed delivery status notifications.
Public propertyIsAuthenticated Get whether or not the client is currently authenticated with the SMTP server.
(Overrides MailServiceIsAuthenticated)
Public propertyCode exampleIsConnected Get whether or not the client is currently connected to an SMTP server.
(Overrides MailServiceIsConnected)
Public propertyIsEncrypted Get whether or not the connection is encrypted (typically via SSL or TLS).
(Overrides MailServiceIsEncrypted)
Public propertyIsSecure Get whether or not the connection is secure (typically via SSL or TLS).
(Overrides MailServiceIsSecure)
Public propertyIsSigned Get whether or not the connection is signed (typically via SSL or TLS).
(Overrides MailServiceIsSigned)
Public propertyLocalDomain Get or set the local domain.
Public propertyLocalEndPoint Get or set the local IP end point to use when connecting to the remote host.
(Inherited from MailService)
Public propertyCode exampleMaxSize Get the maximum message size supported by the server.
Protected propertyPreferSendAsBinaryData Get whether or not the BDAT command is preferred over the DATA command.
Protected propertyProtocol Get the protocol supported by the message service.
(Overrides MailServiceProtocol)
Public propertyProtocolLogger Get the protocol logger.
(Inherited from MailService)
Public propertyProxyClient Get or set the proxy client to use when connecting to a remote host.
(Inherited from MailService)
Public propertyCode exampleServerCertificateValidationCallback Get or set a callback function to validate the server certificate.
(Inherited from MailService)
Public propertyCode exampleSslCipherAlgorithm Get the negotiated SSL or TLS cipher algorithm.
(Overrides MailServiceSslCipherAlgorithm)
Public propertyCode exampleSslCipherStrength Get the negotiated SSL or TLS cipher algorithm strength.
(Overrides MailServiceSslCipherStrength)
Public propertyCode exampleSslHashAlgorithm Get the negotiated SSL or TLS hash algorithm.
(Overrides MailServiceSslHashAlgorithm)
Public propertyCode exampleSslHashStrength Get the negotiated SSL or TLS hash algorithm strength.
(Overrides MailServiceSslHashStrength)
Public propertyCode exampleSslKeyExchangeAlgorithm Get the negotiated SSL or TLS key exchange algorithm.
(Overrides MailServiceSslKeyExchangeAlgorithm)
Public propertyCode exampleSslKeyExchangeStrength Get the negotiated SSL or TLS key exchange algorithm strength.
(Overrides MailServiceSslKeyExchangeStrength)
Public propertyCode exampleSslProtocol Get the negotiated SSL or TLS protocol version.
(Overrides MailServiceSslProtocol)
Public propertySslProtocols Get or set the set of enabled SSL and/or TLS protocol versions that the client is allowed to use.
(Inherited from MailService)
Public propertySyncRoot Gets an object that can be used to synchronize access to the SMTP server.
(Overrides MailServiceSyncRoot)
Public propertyTimeout Get or set the timeout for network streaming operations, in milliseconds.
(Overrides MailServiceTimeout)
Top
Methods
 NameDescription
Public methodAuthenticate(ICredentials, CancellationToken) Authenticate using the supplied credentials.
(Inherited from MailService)
Public methodAuthenticate(SaslMechanism, CancellationToken) Authenticate using the specified SASL mechanism.
(Overrides MailServiceAuthenticate(SaslMechanism, CancellationToken))
Public methodAuthenticate(Encoding, ICredentials, CancellationToken) Authenticate using the supplied credentials.
(Overrides MailServiceAuthenticate(Encoding, ICredentials, CancellationToken))
Public methodCode exampleAuthenticate(String, String, CancellationToken) Authenticate using the specified user name and password.
(Inherited from MailService)
Public methodAuthenticate(Encoding, String, String, CancellationToken) Authenticate using the specified user name and password.
(Inherited from MailService)
Public methodAuthenticateAsync(ICredentials, CancellationToken) Asynchronously authenticate using the supplied credentials.
(Inherited from MailService)
Public methodAuthenticateAsync(SaslMechanism, CancellationToken) Asynchronously authenticate using the specified SASL mechanism.
(Overrides MailServiceAuthenticateAsync(SaslMechanism, CancellationToken))
Public methodAuthenticateAsync(Encoding, ICredentials, CancellationToken) Asynchronously authenticate using the supplied credentials.
(Overrides MailServiceAuthenticateAsync(Encoding, ICredentials, CancellationToken))
Public methodAuthenticateAsync(String, String, CancellationToken) Asynchronously authenticate using the specified user name and password.
(Inherited from MailService)
Public methodAuthenticateAsync(Encoding, String, String, CancellationToken) Asynchronously authenticate using the specified user name and password.
(Inherited from MailService)
Public methodCode exampleConnect(Uri, CancellationToken) Establish a connection to the specified mail server.
(Inherited from MailService)
Public methodConnect(String, Int32, Boolean, CancellationToken) Establish a connection to the specified mail server.
(Inherited from MailService)
Public methodCode exampleConnect(String, Int32, SecureSocketOptions, CancellationToken) Establish a connection to the specified SMTP or SMTP/S server.
(Overrides MailServiceConnect(String, Int32, SecureSocketOptions, CancellationToken))
Public methodConnect(Socket, String, Int32, SecureSocketOptions, CancellationToken) Establish a connection to the specified SMTP or SMTP/S server using the provided socket.
(Overrides MailServiceConnect(Socket, String, Int32, SecureSocketOptions, CancellationToken))
Public methodConnect(Stream, String, Int32, SecureSocketOptions, CancellationToken) Establish a connection to the specified SMTP or SMTP/S server using the provided stream.
(Overrides MailServiceConnect(Stream, String, Int32, SecureSocketOptions, CancellationToken))
Public methodConnectAsync(Uri, CancellationToken) Asynchronously establish a connection to the specified mail server.
(Inherited from MailService)
Public methodConnectAsync(String, Int32, Boolean, CancellationToken) Asynchronously establish a connection to the specified mail server.
(Inherited from MailService)
Public methodCode exampleConnectAsync(String, Int32, SecureSocketOptions, CancellationToken) Asynchronously establish a connection to the specified SMTP or SMTP/S server.
(Overrides MailServiceConnectAsync(String, Int32, SecureSocketOptions, CancellationToken))
Public methodConnectAsync(Socket, String, Int32, SecureSocketOptions, CancellationToken) Asynchronously establish a connection to the specified SMTP or SMTP/S server using the provided socket.
(Overrides MailServiceConnectAsync(Socket, String, Int32, SecureSocketOptions, CancellationToken))
Public methodConnectAsync(Stream, String, Int32, SecureSocketOptions, CancellationToken) Asynchronously establish a connection to the specified SMTP or SMTP/S server using the provided socket.
(Overrides MailServiceConnectAsync(Stream, String, Int32, SecureSocketOptions, CancellationToken))
Public methodCode exampleDisconnect Disconnect the service.
(Overrides MailServiceDisconnect(Boolean, CancellationToken))
Public methodCode exampleDisconnectAsync Asynchronously disconnect the service.
(Overrides MailServiceDisconnectAsync(Boolean, CancellationToken))
Public methodDispose Releases all resource used by the MailService object.
(Inherited from MailService)
Protected methodDispose(Boolean) Releases the unmanaged resources used by the SmtpClient and optionally releases the managed resources.
(Overrides MailServiceDispose(Boolean))
Public methodEquals
(Inherited from Object)
Public methodCode exampleExpand Expand a mailing address alias.
Public methodCode exampleExpandAsync Asynchronously expand a mailing address alias.
Protected methodFinalize Releases unmanaged resources and performs other cleanup operations before the MailService is reclaimed by garbage collection.
(Inherited from MailService)
Protected methodCode exampleGetDeliveryStatusNotifications Get the types of delivery status notification desired for the specified recipient mailbox.
Protected methodCode exampleGetEnvelopeId Get the envelope identifier to be used with delivery status notifications.
Public methodGetHashCode
(Inherited from Object)
Protected methodGetSize Get the size of the message.
Protected methodGetSizeAsync Asynchronously get the size of the message.
Public methodGetType
(Inherited from Object)
Protected methodMemberwiseClone
(Inherited from Object)
Public methodNoOp Ping the SMTP server to keep the connection alive.
(Overrides MailServiceNoOp(CancellationToken))
Public methodNoOpAsync Asynchronously ping the SMTP server to keep the connection alive.
(Overrides MailServiceNoOpAsync(CancellationToken))
Protected methodOnAuthenticated Raise the authenticated event.
(Inherited from MailService)
Protected methodOnConnected Raise the connected event.
(Inherited from MailService)
Protected methodOnDisconnected Raise the disconnected event.
(Inherited from MailService)
Protected methodOnMessageSent Raise the message sent event.
(Inherited from MailTransport)
Protected methodOnNoRecipientsAccepted Invoked only when no recipients were accepted by the SMTP server.
Protected methodOnRecipientAccepted Invoked when a recipient is accepted by the SMTP server.
Protected methodOnRecipientNotAccepted Invoked when a recipient is not accepted by the SMTP server.
Protected methodOnSenderAccepted Invoked when the sender is accepted by the SMTP server.
Protected methodOnSenderNotAccepted Invoked when a recipient is not accepted by the SMTP server.
Protected methodPrepare Prepare the message for transport with the specified constraints.
Public methodCode exampleSend(MimeMessage, CancellationToken, ITransferProgress) Send the specified message.
(Inherited from MailTransport)
Public methodCode exampleSend(FormatOptions, MimeMessage, CancellationToken, ITransferProgress) Send the specified message.
(Overrides MailTransportSend(FormatOptions, MimeMessage, CancellationToken, ITransferProgress))
Public methodSend(MimeMessage, MailboxAddress, IEnumerableMailboxAddress, CancellationToken, ITransferProgress) Send the specified message using the supplied sender and recipients.
(Inherited from MailTransport)
Public methodSend(FormatOptions, MimeMessage, MailboxAddress, IEnumerableMailboxAddress, CancellationToken, ITransferProgress) Send the specified message using the supplied sender and recipients.
(Overrides MailTransportSend(FormatOptions, MimeMessage, MailboxAddress, IEnumerableMailboxAddress, CancellationToken, ITransferProgress))
Public methodSendAsync(MimeMessage, CancellationToken, ITransferProgress) Asynchronously send the specified message.
(Inherited from MailTransport)
Public methodCode exampleSendAsync(FormatOptions, MimeMessage, CancellationToken, ITransferProgress) Asynchronously send the specified message.
(Overrides MailTransportSendAsync(FormatOptions, MimeMessage, CancellationToken, ITransferProgress))
Public methodSendAsync(MimeMessage, MailboxAddress, IEnumerableMailboxAddress, CancellationToken, ITransferProgress) Asynchronously send the specified message using the supplied sender and recipients.
(Inherited from MailTransport)
Public methodSendAsync(FormatOptions, MimeMessage, MailboxAddress, IEnumerableMailboxAddress, CancellationToken, ITransferProgress) Asynchronously send the specified message using the supplied sender and recipients.
(Overrides MailTransportSendAsync(FormatOptions, MimeMessage, MailboxAddress, IEnumerableMailboxAddress, CancellationToken, ITransferProgress))
Protected methodSendCommand Send a custom command to the SMTP server.
Protected methodSendCommandAsync Asynchronously send a custom command to the SMTP server.
Public methodToString
(Inherited from Object)
Public methodCode exampleVerify Verify the existence of a mailbox address.
Public methodCode exampleVerifyAsync Asynchronously verify the existence of a mailbox address.
Top
Events
 NameDescription
Public eventAuthenticated Occurs when the client has been successfully authenticated.
(Inherited from MailService)
Public eventConnected Occurs when the client has been successfully connected.
(Inherited from MailService)
Public eventDisconnected Occurs when the client gets disconnected.
(Inherited from MailService)
Public eventMessageSent Occurs when a message is successfully sent via the transport.
(Inherited from MailTransport)
Top
Remarks

The SmtpClient class supports both the "smtp" and "smtps" protocols. The "smtp" protocol makes a clear-text connection to the SMTP server and does not use SSL or TLS unless the SMTP server supports the STARTTLS extension. The "smtps" protocol, however, connects to the SMTP server using an SSL-wrapped connection.

The connection established by any of the Connect methods may be re-used if an application wishes to send multiple messages to the same SMTP server. Since connecting and authenticating can be expensive operations, re-using a connection can significantly improve performance when sending a large number of messages to the same SMTP server over a short period of time.

Example
C#
public static void SendMessages (IList<MimeMessage> messages)
{
    using (var client = new SmtpClient ()) {
        client.Connect ("smtp.gmail.com", 465, SecureSocketOptions.SslOnConnect);

        client.Authenticate ("username", "password");

        foreach (var message in messages) {
            client.Send (message);
        }

        client.Disconnect (true);
    }
}
See Also