public static IEmailSender NewInstance( ILogSink logger, EmailSenderOptions options ) { return(new DotNetEmailSender( logger: logger, options: options )); }
public DotNetEmailSender( ILogSink logger, EmailSenderOptions options ) { ValidateLogger(logger); ValidateOptions(options); _logger = logger; _options = options; }
private static void SendCore( EmailSenderOptions options, EmailMessage incoming ) { // compose message from incoming spec var message = GetMailMessage(incoming); // compose smtp client from options var smtp = GetSmtpClient(options); // Send the message (sync) smtp.Send(message); }
private static SmtpClient GetSmtpClient( EmailSenderOptions options ) { // Create a System.Net.Mail.SmtpClient object // and set the SMTP host and port number var smtp = new SmtpClient(options.Host, options.Port); // If your server requires authentication add the below code // ========================================================= // Enable Secure Socket Layer (SSL) for connection encryption smtp.EnableSsl = options.EnableSsl; // Do not send the DefaultCredentials with requests // Create a System.Net.NetworkCredential object and set // the username and password required by your SMTP account smtp.UseDefaultCredentials = false; smtp.Credentials = new NetworkCredential(options.UserName, options.Password); // ========================================================= return(smtp); }
private static void ValidateOptions(EmailSenderOptions options) { // throw when null if (options == null) { throw new ArgumentNullException("options"); } // throw when null/empty if (string.IsNullOrWhiteSpace(options.UserName)) { throw new ArgumentException("{UserName} cannot be null or empty.", "credentials"); } // throw when null/empty if (string.IsNullOrWhiteSpace(options.Password)) { throw new ArgumentException("{Password} cannot be null or empty.", "credentials"); } // throw when null/empty if (string.IsNullOrWhiteSpace(options.Host)) { throw new ArgumentException("{Host} cannot be null or empty.", "options"); } // throw when unpexected range if (options.MaxDegreeOfParallelism <= 0) { throw new ArgumentOutOfRangeException("options", "{MaxDegreeOfParallelism} must be positive integer."); } // throw when unpexected range if (options.Port <= 0) { throw new ArgumentOutOfRangeException("options", "{Port} must be positive integer."); } }