public static SmtpClient Build(SmtpElement smtpElement) { SmtpClient smtpClient = new SmtpClient(smtpElement.Host); smtpClient.Credentials = new System.Net.NetworkCredential(smtpElement.UserName, smtpElement.Password); smtpClient.Port = smtpElement.Port; smtpClient.EnableSsl = smtpElement.EnableSsl; return(smtpClient); }
public MailProcessor(ProcessorElement config, Service service) : base(config, service) { foreach (string item in new string[] { "sender", "recipient", "subject", "body" }) { string value = null; if (config.Options[item] != null) { value = config.Options[item].Value; } if (string.IsNullOrEmpty(value)) { throw new Exception(GetType() + "[" + Name + "]: Undefined or empty " + item); } } if (config.Options["sender"] != null) { sender = config.Options["sender"].Value; } if (config.Options["recipient"] != null) { recipient = config.Options["recipient"].Value; } if (config.Options["subject"] != null) { subject = config.Options["subject"].Value; } if (config.Options["body"] != null) { body = config.Options["body"].Value; } SmtpElement smtpConfig = F2B.Config.Instance.Smtp; if (!string.IsNullOrEmpty(smtpConfig.Username.Value) && !string.IsNullOrEmpty(smtpConfig.Password.Value)) { if (!smtpConfig.Ssl.Value) { throw new InvalidDataException("Can't send SMTP AUTH email without SSL encryption"); } smtpAuth = new System.Net.NetworkCredential(smtpConfig.Username.Value, smtpConfig.Password.Value); } #if DEBUG nmsgs = 0; #endif }
/// <summary> /// This Method send the email using AWS SES /// </summary> /// <param name="emailRequest">EmailRequest Type containing Fields Required to send an email</param> /// <param name="smtpSettings">Configured SMTP Settings</param> /// <returns>Boolean based on email send success status</returns> private bool SendEmail(EmailRequest emailRequest, SmtpElement smtpSettings) { EmailService emailService = new EmailService(); SESClientRequest sesRequest = new SESClientRequest() { To = emailRequest.To, ReceiverToBcc = emailRequest.Bcc, ReceiverToCc = emailRequest.Cc, From = emailRequest.From, EmailBody = emailRequest.EmailBody, EmailSubject = emailRequest.Subject, Host = smtpSettings.Host, Port = smtpSettings.Port, SMTP_Password = smtpSettings.Password, SMTP_Username = smtpSettings.UserName, ReplyTo = emailRequest.ReplyTo, Attachments = emailRequest.Attachments }; SESClientResponse emailSendStatus = emailService.SendEmail <SESClientResponse, SESClientRequest>(sesRequest); return(emailSendStatus.EmailActionSuccess); }
public static MailMessage Build(SmtpElement smtpElement, string toRecipients, string ccRecipients, string bccRecipients, string subject, string body, bool isHighPriority, List <Attachment> attachments) { MailMessage mailMessage = new MailMessage(); if (!String.IsNullOrEmpty(smtpElement.Sender)) { mailMessage.From = new MailAddress(smtpElement.From, smtpElement.Sender); } else { mailMessage.From = new MailAddress(smtpElement.From); } if (String.IsNullOrEmpty(toRecipients) && String.IsNullOrEmpty(ccRecipients) && String.IsNullOrEmpty(bccRecipients)) { toRecipients = smtpElement.DefaultToRecipients; if (String.IsNullOrEmpty(subject)) { subject = smtpElement.DefaultSubject; } } if (!String.IsNullOrEmpty(toRecipients)) { foreach (MailAddress address in ParseRecipients(toRecipients)) { mailMessage.To.Add(address); } } if (!String.IsNullOrEmpty(ccRecipients)) { foreach (MailAddress address in ParseRecipients(ccRecipients)) { mailMessage.CC.Add(address); } } if (!String.IsNullOrEmpty(bccRecipients)) { foreach (MailAddress address in ParseRecipients(bccRecipients)) { mailMessage.Bcc.Add(address); } } mailMessage.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess | DeliveryNotificationOptions.OnFailure; mailMessage.Subject = subject.Replace('\r', ' ').Replace('\n', ' ').Replace(Environment.NewLine, String.Empty); mailMessage.SubjectEncoding = Encoding.UTF8; mailMessage.BodyEncoding = Encoding.UTF8; if (isHighPriority) { mailMessage.Priority = MailPriority.High; } else { mailMessage.Priority = MailPriority.Normal; } if (attachments != null) { foreach (Attachment attachment in attachments) { mailMessage.Attachments.Add(attachment); } } if (body.StartsWith(MailQueue.NheaMailingStarter)) { body = body.Replace(MailQueue.NheaMailingStarter, String.Empty); var parameters = JsonConvert.DeserializeObject <MailParameters>(body); if (!string.IsNullOrEmpty(parameters.PlainText) || smtpElement.AutoGeneratePlainText) { if (string.IsNullOrEmpty(parameters.PlainText)) { parameters.PlainText = Nhea.Text.HtmlHelper.GetPlainText(parameters.Body); } mailMessage.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(parameters.PlainText, null, MediaTypeNames.Text.Plain)); mailMessage.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(ParseBody(parameters.Body), null, MediaTypeNames.Text.Html)); } else { mailMessage.Body = ParseBody(parameters.Body); mailMessage.IsBodyHtml = true; } if (parameters.IsBulkEmail) { mailMessage.Headers.Add("Precedence", "bulk"); } if (!string.IsNullOrEmpty(parameters.ListUnsubscribe)) { string listUnsubscribeHeader = parameters.ListUnsubscribe.Replace(" ", string.Empty); if (!listUnsubscribeHeader.StartsWith("<")) { listUnsubscribeHeader = "<" + listUnsubscribeHeader; } if (!listUnsubscribeHeader.EndsWith(">")) { listUnsubscribeHeader += ">"; } mailMessage.Headers.Add("List-Unsubscribe", listUnsubscribeHeader); if (parameters.UnsubscribeOneClick) { mailMessage.Headers.Add("List-Unsubscribe-Post", "List-Unsubscribe=One-Click"); } } } else { mailMessage.Body = ParseBody(body); mailMessage.IsBodyHtml = true; } return(mailMessage); }
/// <summary> /// This method gets SMTP settings from the site configuration /// </summary> /// <returns>SmtpElement containing SMTP Settings</returns> private SmtpElement GetSMTPSettings() { SmtpElement smtpSettings = Config.Get <SystemConfig>().SmtpSettings; return(smtpSettings); }
public bool Send(string fromAddress, string toAddresses, string subject, string messageHtml, List <Attachment> attachments = null, string fromName = "", string bccAdresses = "", string replyTo = "") { var isSentToRecepient = false; MailMessage message = new MailMessage { Subject = subject, Body = messageHtml, IsBodyHtml = true, BodyEncoding = Encoding.UTF8 }; // Add sender's address try { message.From = string.IsNullOrEmpty(fromName) ? new MailAddress(fromAddress) : new MailAddress(fromAddress, fromName); } catch (Exception ex) { Log.Write(ex); message.From = new MailAddress(DefaultSenderEmail, DefaultSenderName); } // Add addresses of recipients try { message.To.Add(toAddresses); } catch (Exception ex) { Log.Write(ex); return(false); } // Add bcc addresses if (!string.IsNullOrEmpty(bccAdresses)) { try { message.Bcc.Add(bccAdresses); } catch (Exception ex) { Log.Write(ex); } } // Add reply to addresses try { if (!string.IsNullOrEmpty(replyTo)) { message.ReplyToList.Add(new MailAddress(replyTo)); } } catch (Exception ex) { Log.Write(ex); } // Add attachments if (attachments != null) { foreach (Attachment file in attachments) { message.Attachments.Add(file); } } SmtpClient smtpClient = new SmtpClient(); SmtpElement settings = Config.Get <SystemConfig>().SmtpSettings; var basicCredential = new NetworkCredential(settings.UserName, settings.Password, settings.Domain); smtpClient.Host = settings.Host; smtpClient.UseDefaultCredentials = false; smtpClient.Credentials = basicCredential; // Attempts to send the email try { smtpClient.Send(message); isSentToRecepient = true; } catch (Exception ex) { Log.Write(ex); } return(isSentToRecepient); }