// // ImapCommandException.cs // // Author: Jeffrey Stedfast // // Copyright (c) 2013-2020 .NET Foundation and Contributors // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. // using System; #if SERIALIZABLE using System.Security; using System.Runtime.Serialization; #endif namespace MailKit.Net.Imap { /// /// An exception that is thrown when an IMAP command returns NO or BAD. /// /// /// The exception that is thrown when an IMAP command fails. Unlike a , /// a does not require the to be reconnected. /// #if SERIALIZABLE [Serializable] #endif public class ImapCommandException : CommandException { #if SERIALIZABLE /// /// Initializes a new instance of the class. /// /// /// Creates a new from the serialized data. /// /// The serialization info. /// The streaming context. /// /// is null. /// [SecuritySafeCritical] protected ImapCommandException (SerializationInfo info, StreamingContext context) : base (info, context) { Response = (ImapCommandResponse) info.GetValue ("Response", typeof (ImapCommandResponse)); ResponseText = info.GetString ("ResponseText"); } #endif /// /// Create a new based on the specified command name and state. /// /// /// Create a new based on the specified command name and state. /// /// A new command exception. /// The command name. /// The command state. internal static ImapCommandException Create (string command, ImapCommand ic) { var result = ic.Response.ToString ().ToUpperInvariant (); string message, reason = null; if (string.IsNullOrEmpty (ic.ResponseText)) { for (int i = ic.RespCodes.Count - 1; i >= 0; i--) { if (ic.RespCodes[i].IsError && !string.IsNullOrEmpty (ic.RespCodes[i].Message)) { reason = ic.RespCodes[i].Message; break; } } } else { reason = ic.ResponseText; } if (!string.IsNullOrEmpty (reason)) message = string.Format ("The IMAP server replied to the '{0}' command with a '{1}' response: {2}", command, result, reason); else message = string.Format ("The IMAP server replied to the '{0}' command with a '{1}' response.", command, result); return ic.Exception != null ? new ImapCommandException (ic.Response, reason, message, ic.Exception) : new ImapCommandException (ic.Response, reason, message); } /// /// Initializes a new instance of the class. /// /// /// Creates a new . /// /// The IMAP command response. /// The error message. /// The human-readable response text. /// The inner exception. public ImapCommandException (ImapCommandResponse response, string responseText, string message, Exception innerException) : base (message, innerException) { ResponseText = responseText; Response = response; } /// /// Initializes a new instance of the class. /// /// /// Creates a new . /// /// The IMAP command response. /// The human-readable response text. /// The error message. public ImapCommandException (ImapCommandResponse response, string responseText, string message) : base (message) { ResponseText = responseText; Response = response; } /// /// Initializes a new instance of the class. /// /// /// Creates a new . /// /// The IMAP command response. /// The human-readable response text. public ImapCommandException (ImapCommandResponse response, string responseText) { ResponseText = responseText; Response = response; } /// /// Gets the IMAP command response. /// /// /// Gets the IMAP command response. /// /// The IMAP command response. public ImapCommandResponse Response { get; private set; } /// /// Gets the human-readable IMAP command response text. /// /// /// Gets the human-readable IMAP command response text. /// /// The response text. public string ResponseText { get; private set; } #if SERIALIZABLE /// /// When overridden in a derived class, sets the /// with information about the exception. /// /// /// Serializes the state of the . /// /// The serialization info. /// The streaming context. /// /// is null. /// [SecurityCritical] public override void GetObjectData (SerializationInfo info, StreamingContext context) { base.GetObjectData (info, context); info.AddValue ("Response", Response, typeof (ImapCommandResponse)); info.AddValue ("ResponseText", ResponseText); } #endif } }