2019-12-08 21:23:54 +01:00
|
|
|
|
using Swan.Logging;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net.Mail;
|
|
|
|
|
|
|
|
|
|
namespace Swan.Net.Smtp {
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Use this class to store the sender session data.
|
|
|
|
|
/// </summary>
|
|
|
|
|
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}");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-12-04 18:57:18 +01:00
|
|
|
|
}
|