RaspberryIO/Unosquare.Swan/Networking/SmtpSender.cs
2019-12-03 18:44:25 +01:00

55 lines
1.5 KiB
C#

using System;
#if !NETSTANDARD1_3
using System.Net.Mail;
#else
using Exceptions;
#endif
namespace Unosquare.Swan.Networking {
/// <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}".Debug(typeof(SmtpClient), this._sessionId);
}
}
public String ReplyText {
get; set;
}
public Boolean IsReplyOk => this.ReplyText.StartsWith("250 ");
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}".Debug(typeof(SmtpClient), this._sessionId);
if(response.IsPositive) {
return;
}
String responseContent = String.Empty;
if(response.Content.Count > 0) {
responseContent = String.Join(";", response.Content.ToArray());
}
throw new SmtpException((SmtpStatusCode)response.ReplyCode, responseContent);
} catch {
throw new SmtpException($"Could not parse server response: {this.ReplyText}");
}
}
}
}