Smtp Command ExceptionResponse Text Property
Get the response text returned by the SMTP server.
Definition
Namespace: MailKit.Net.Smtp
Assembly: MailKit (in MailKit.dll) Version: 5.0.0
The response text, if available; otherwise, .
Assembly: MailKit (in MailKit.dll) Version: 5.0.0
C#
public string? ResponseText { get; }Property Value
StringThe response text, if available; otherwise, .
Remarks
Gets the raw response text (without the status code) returned by the SMTP server.
Unlike Message, this value is never decorated with additional text and so it can be reliably used for logging or for parsing enhanced status codes.
This value will be if the error was not the result of a server response or if the exception was created using a constructor that does not take an SmtpResponse argument.
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);
}
}