Smtp Command Enumeration
An enumeration of the SMTP commands that the SmtpClient may send.
Definition
Namespace: MailKit.Net.Smtp
Assembly: MailKit (in MailKit.dll) Version: 5.0.0
Assembly: MailKit (in MailKit.dll) Version: 5.0.0
C#
public enum SmtpCommandRemarks
An enumeration of the SMTP commands that the SmtpClient may send.
This value is used by SmtpCommandException, SmtpProtocolException, and SmtpServiceNotAuthenticatedException to identify which command (or which response) the SmtpClient was processing when the error occurred.
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.Contains (SmtpCapability.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);
}
}
Members
| Unknown | 0 | The command is unknown. |
| Connect | 1 | Not an actual command, this value represents the greeting sent by the SMTP server upon connecting. |
| Ehlo | 2 | The EHLO command. |
| Helo | 3 | The HELO command. |
| StartTls | 4 | The STARTTLS command. |
| Auth | 5 | The AUTH command (including any SASL challenge responses). |
| MailFrom | 6 | The MAIL FROM command. |
| RcptTo | 7 | The RCPT TO command. |
| Data | 8 | The DATA command. |
| MessageData | 9 | Not an actual command, this value represents the message content that is sent after the server accepts the DATA command, terminated by a line consisting of a single period. |
| Bdat | 10 | The BDAT command. |
| Rset | 11 | The RSET command. |
| Noop | 12 | The NOOP command. |
| Quit | 13 | The QUIT command. |
| Vrfy | 14 | The VRFY command. |
| Expn | 15 | The EXPN command. |
| Custom | 16 | A custom command sent via SendCommand(String, CancellationToken) or SendCommandAsync(String, CancellationToken). |