private SmtpOptions GetSmptOptions(ISiteSettings siteSettings) { SmtpOptions smtpOptions = new SmtpOptions(); smtpOptions.Password = siteSettings.SmtpPassword; smtpOptions.Port = siteSettings.SmtpPort; smtpOptions.PreferredEncoding = siteSettings.SmtpPreferredEncoding; smtpOptions.RequiresAuthentication = siteSettings.SmtpRequiresAuth; smtpOptions.Server = siteSettings.SmtpServer; smtpOptions.User = siteSettings.SmtpUser; smtpOptions.UseSsl = siteSettings.SmtpUseSsl; return smtpOptions; }
private SmtpOptions GetSmptOptions(ISiteContext siteSettings) { if(!siteSettings.SmtpIsConfigured()) { return globalSmtpSettings; } SmtpOptions smtpOptions = new SmtpOptions(); smtpOptions.Password = siteSettings.SmtpPassword; smtpOptions.Port = siteSettings.SmtpPort; smtpOptions.PreferredEncoding = siteSettings.SmtpPreferredEncoding; smtpOptions.RequiresAuthentication = siteSettings.SmtpRequiresAuth; smtpOptions.Server = siteSettings.SmtpServer; smtpOptions.User = siteSettings.SmtpUser; smtpOptions.UseSsl = siteSettings.SmtpUseSsl; smtpOptions.DefaultEmailFromAddress = siteSettings.DefaultEmailFromAddress; smtpOptions.DefaultEmailFromAlias = siteSettings.DefaultEmailFromAlias; return smtpOptions; }
public async Task SendEmailAsync( SmtpOptions smtpOptions, string to, string from, string subject, string plainTextMessage, string htmlMessage) { if (string.IsNullOrEmpty(plainTextMessage) && string.IsNullOrEmpty(htmlMessage)) { throw new ArgumentException("no message provided"); } var m = new MimeMessage(); m.From.Add(new MailboxAddress("", from)); m.To.Add(new MailboxAddress("", to)); m.Subject = subject; //m.Importance = MessageImportance.Normal; //Header h = new Header(HeaderId.Precedence, "Bulk"); //m.Headers.Add() BodyBuilder bodyBuilder = new BodyBuilder(); if (plainTextMessage.Length > 0) { bodyBuilder.TextBody = plainTextMessage; } if (htmlMessage.Length > 0) { bodyBuilder.HtmlBody = htmlMessage; } m.Body = bodyBuilder.ToMessageBody(); using (var client = new SmtpClient()) { //client.ServerCertificateValidationCallback = delegate ( // Object obj, X509Certificate certificate, X509Chain chain, // SslPolicyErrors errors) //{ // return (true); //}; await client.ConnectAsync(smtpOptions.Server, smtpOptions.Port, smtpOptions.UseSsl); //await client.ConnectAsync(smtpOptions.Server, smtpOptions.Port, SecureSocketOptions.StartTls); // Note: since we don't have an OAuth2 token, disable // the XOAUTH2 authentication mechanism. client.AuthenticationMechanisms.Remove("XOAUTH2"); // Note: only needed if the SMTP server requires authentication if (smtpOptions.RequiresAuthentication) { await client.AuthenticateAsync(smtpOptions.User, smtpOptions.Password); } client.Send(m); client.Disconnect(true); } }
public async Task SendEmailAsync( SmtpOptions smtpOptions, string to, string from, string subject, string plainTextMessage, string htmlMessage, string replyTo = null, Importance importance = Importance.Normal ) { if (string.IsNullOrWhiteSpace(to)) { throw new ArgumentException("no to address provided"); } if (string.IsNullOrWhiteSpace(from)) { throw new ArgumentException("no from address provided"); } if (string.IsNullOrWhiteSpace(subject)) { throw new ArgumentException("no subject provided"); } var hasPlainText = !string.IsNullOrWhiteSpace(plainTextMessage); var hasHtml = !string.IsNullOrWhiteSpace(htmlMessage); if (!hasPlainText && !hasHtml) { throw new ArgumentException("no message provided"); } var m = new MimeMessage(); m.From.Add(new MailboxAddress("", from)); if (!string.IsNullOrWhiteSpace(replyTo)) { m.ReplyTo.Add(new MailboxAddress("", replyTo)); } m.To.Add(new MailboxAddress("", to)); m.Subject = subject; m.Importance = GetMessageImportance(importance); //Header h = new Header(HeaderId.Precedence, "Bulk"); //m.Headers.Add() BodyBuilder bodyBuilder = new BodyBuilder(); if (hasPlainText) { bodyBuilder.TextBody = plainTextMessage; } if (hasHtml) { bodyBuilder.HtmlBody = htmlMessage; } m.Body = bodyBuilder.ToMessageBody(); using (var client = new SmtpClient()) { await client.ConnectAsync( smtpOptions.Server, smtpOptions.Port, smtpOptions.UseSsl) .ConfigureAwait(false); // Note: since we don't have an OAuth2 token, disable // the XOAUTH2 authentication mechanism. client.AuthenticationMechanisms.Remove("XOAUTH2"); // Note: only needed if the SMTP server requires authentication if (smtpOptions.RequiresAuthentication) { await client.AuthenticateAsync(smtpOptions.User, smtpOptions.Password) .ConfigureAwait(false); } await client.SendAsync(m).ConfigureAwait(false); await client.DisconnectAsync(true).ConfigureAwait(false); } }
public async Task SendEmailAsync( SmtpOptions smtpOptions, string to, string from, string subject, string plainTextMessage, string htmlMessage) { if(string.IsNullOrEmpty(plainTextMessage) && string.IsNullOrEmpty(htmlMessage)) { throw new ArgumentException("no message provided"); } var m = new MimeMessage(); m.From.Add(new MailboxAddress("", from)); m.To.Add(new MailboxAddress("", to)); m.Subject = subject; //m.Importance = MessageImportance.Normal; //Header h = new Header(HeaderId.Precedence, "Bulk"); //m.Headers.Add() BodyBuilder bodyBuilder = new BodyBuilder(); if(plainTextMessage.Length > 0) { bodyBuilder.TextBody = plainTextMessage; } if (htmlMessage.Length > 0) { bodyBuilder.HtmlBody = htmlMessage; } m.Body = bodyBuilder.ToMessageBody(); using (var client = new SmtpClient()) { //client.ServerCertificateValidationCallback = delegate ( // Object obj, X509Certificate certificate, X509Chain chain, // SslPolicyErrors errors) //{ // return (true); //}; await client.ConnectAsync(smtpOptions.Server, smtpOptions.Port, smtpOptions.UseSsl); //await client.ConnectAsync(smtpOptions.Server, smtpOptions.Port, SecureSocketOptions.StartTls); // Note: since we don't have an OAuth2 token, disable // the XOAUTH2 authentication mechanism. client.AuthenticationMechanisms.Remove("XOAUTH2"); // Note: only needed if the SMTP server requires authentication if(smtpOptions.RequiresAuthentication) { await client.AuthenticateAsync(smtpOptions.User, smtpOptions.Password); } client.Send(m); client.Disconnect(true); } }
public async Task SendMultipleEmailAsync( SmtpOptions smtpOptions, string toCsv, string from, string subject, string plainTextMessage, string htmlMessage) { if (string.IsNullOrEmpty(toCsv)) { throw new ArgumentException("no to addresses provided"); } if (string.IsNullOrEmpty(from)) { throw new ArgumentException("no from address provided"); } if (string.IsNullOrEmpty(subject)) { throw new ArgumentException("no subject provided"); } if (string.IsNullOrEmpty(plainTextMessage) && string.IsNullOrEmpty(htmlMessage)) { throw new ArgumentException("no message provided"); } var m = new MimeMessage(); m.From.Add(new MailboxAddress("", from)); string[] adrs = toCsv.Split(','); foreach (string item in adrs) { if (!string.IsNullOrEmpty(item)) { m.To.Add(new MailboxAddress("", item));; } } m.Subject = subject; m.Importance = MessageImportance.High; BodyBuilder bodyBuilder = new BodyBuilder(); if (plainTextMessage.Length > 0) { bodyBuilder.TextBody = plainTextMessage; } if (htmlMessage.Length > 0) { bodyBuilder.HtmlBody = htmlMessage; } m.Body = bodyBuilder.ToMessageBody(); using (var client = new SmtpClient()) { //client.ServerCertificateValidationCallback = delegate ( // Object obj, X509Certificate certificate, X509Chain chain, // SslPolicyErrors errors) //{ // return (true); //}; await client.ConnectAsync( smtpOptions.Server, smtpOptions.Port, smtpOptions.UseSsl).ConfigureAwait(false); //await client.ConnectAsync(smtpOptions.Server, smtpOptions.Port, SecureSocketOptions.StartTls); // Note: since we don't have an OAuth2 token, disable // the XOAUTH2 authentication mechanism. client.AuthenticationMechanisms.Remove("XOAUTH2"); // Note: only needed if the SMTP server requires authentication if (smtpOptions.RequiresAuthentication) { await client.AuthenticateAsync( smtpOptions.User, smtpOptions.Password).ConfigureAwait(false); } await client.SendAsync(m).ConfigureAwait(false); await client.DisconnectAsync(true).ConfigureAwait(false); } }
public async Task SendMultipleEmailAsync( SmtpOptions smtpOptions, string toCsv, string from, string subject, string plainTextMessage, string htmlMessage) { if (string.IsNullOrEmpty(toCsv)) { throw new ArgumentException("no to addresses provided"); } if (string.IsNullOrEmpty(from)) { throw new ArgumentException("no from address provided"); } if (string.IsNullOrEmpty(subject)) { throw new ArgumentException("no subject provided"); } if (string.IsNullOrEmpty(plainTextMessage) && string.IsNullOrEmpty(htmlMessage)) { throw new ArgumentException("no message provided"); } var m = new MimeMessage(); m.From.Add(new MailboxAddress("", from)); string[] adrs = toCsv.Split(','); foreach (string item in adrs) { if (!string.IsNullOrEmpty(item)) { m.To.Add(new MailboxAddress("", item)); ; } } m.Subject = subject; m.Importance = MessageImportance.High; BodyBuilder bodyBuilder = new BodyBuilder(); if (plainTextMessage.Length > 0) { bodyBuilder.TextBody = plainTextMessage; } if (htmlMessage.Length > 0) { bodyBuilder.HtmlBody = htmlMessage; } m.Body = bodyBuilder.ToMessageBody(); using (var client = new SmtpClient()) { //client.ServerCertificateValidationCallback = delegate ( // Object obj, X509Certificate certificate, X509Chain chain, // SslPolicyErrors errors) //{ // return (true); //}; await client.ConnectAsync( smtpOptions.Server, smtpOptions.Port, smtpOptions.UseSsl).ConfigureAwait(false); //await client.ConnectAsync(smtpOptions.Server, smtpOptions.Port, SecureSocketOptions.StartTls); // Note: since we don't have an OAuth2 token, disable // the XOAUTH2 authentication mechanism. client.AuthenticationMechanisms.Remove("XOAUTH2"); // Note: only needed if the SMTP server requires authentication if (smtpOptions.RequiresAuthentication) { await client.AuthenticateAsync( smtpOptions.User, smtpOptions.Password).ConfigureAwait(false); } await client.SendAsync(m).ConfigureAwait(false); await client.DisconnectAsync(true).ConfigureAwait(false); } }