using Swan.Logging; using System; using System.Linq; using System.Net.Mail; namespace Swan.Net.Smtp { /// /// Use this class to store the sender session data. /// internal class SmtpSender { private readonly String _sessionId; private String _requestText; public SmtpSender(String sessionId) => this._sessionId = sessionId; public String RequestText { get => this._requestText; set { this._requestText = value; $" TX {this._requestText}".Trace(typeof(SmtpClient), this._sessionId); } } public String ReplyText { get; set; } public Boolean IsReplyOk => this.ReplyText.StartsWith("250 ", StringComparison.OrdinalIgnoreCase); public void ValidateReply() { if(this.ReplyText == null) { throw new SmtpException("There was no response from the server"); } try { SmtpServerReply response = SmtpServerReply.Parse(this.ReplyText); $" RX {this.ReplyText} - {response.IsPositive}".Trace(typeof(SmtpClient), this._sessionId); if(response.IsPositive) { return; } String responseContent = response.Content.Any() ? String.Join(";", response.Content.ToArray()) : String.Empty; throw new SmtpException((SmtpStatusCode)response.ReplyCode, responseContent); } catch(Exception ex) { if(!(ex is SmtpException)) { throw new SmtpException($"Could not parse server response: {this.ReplyText}"); } } } } }