public virtual void SendEmail(EmailServerSettings settings, string subject, string body, IEnumerable<MailAddress> toAddressList, MailAddress fromAddress, params EmailAttachmentData[] attachments) { using (var smtpClient = GetSmtpClient(settings)) { var message = new MailMessage(); message.From = fromAddress; message.Body = body; message.Subject = subject; message.IsBodyHtml = true; foreach (var toAddress in toAddressList) { message.To.Add(toAddress); } if (attachments != null) { foreach (var item in attachments) { var stream = StreamHelper.CreateMemoryStream(item.AttachmentData); stream.Position = 0; var attachment = new Attachment(stream, item.FileName); message.Attachments.Add(attachment); } } smtpClient.Send(message); } }
public void ProcessItem(EmailJobData jobItem) { var emailConnectionSettings = _connectionSettingsManager.Load<EmailConnectionSettings>(jobItem.SettingsSource, jobItem.SettingsKey); var emailServerSettings = new EmailServerSettings { Host = emailConnectionSettings.Host, Port = emailConnectionSettings.Port, UserName = emailConnectionSettings.UserName, Password = emailConnectionSettings.Password }; string overrideEmailAddress = ConfigurationManager.AppSettings["OverrideAllEmailAddress"]; var toAddressList = DataServiceMailAddress.ToMailAddressList(jobItem.ToAddressList); if (!string.IsNullOrWhiteSpace(overrideEmailAddress)) { toAddressList = new List<MailAddress>() { new MailAddress(overrideEmailAddress) }; } string subject = FilterSubject(jobItem.Subject); try { EmailAttachmentData[] attachments = null; if(jobItem.Attachments != null) { attachments = jobItem.Attachments.ToArray(); } _emailSender.SendEmail(emailServerSettings, subject, jobItem.Body, toAddressList, jobItem.FromAddress.ToMailAddress(), attachments); } catch (RazorEngine.Templating.TemplateCompilationException ex) { var sb = new StringBuilder(); sb.AppendLine("Error(s) compiling template for email: "); foreach (var item in ex.Errors) { sb.AppendLine("-" + item.Line.ToString() + ": " + item.ErrorText); } throw new Exception(sb.ToString(), ex); } }
private SmtpClient GetSmtpClient(EmailServerSettings emailServerSettings) { if(emailServerSettings == null) { emailServerSettings = this.EmailServerSettings; } if(emailServerSettings == null) { return new SmtpClient(); } else { int port = emailServerSettings.Port.GetValueOrDefault(25); var smtpClient = new SmtpClient(emailServerSettings.Host, port); if (!string.IsNullOrEmpty(emailServerSettings.UserName)) { var credential = new NetworkCredential(emailServerSettings.UserName, emailServerSettings.Password); credential.GetCredential(smtpClient.Host, port, "Basic"); smtpClient.UseDefaultCredentials = false; smtpClient.Credentials = credential; } return smtpClient; } }
private void SendEmail(EmailServerSettings emailServerSettings, string subject, string body, IEnumerable<string> toAddressList, string fromAddress, EmailAttachmentData[] attachments) { var toList = toAddressList.Select(i => new MailAddress(i)); var from = new MailAddress(fromAddress); this.SendEmail(emailServerSettings, subject, body, toList, from, attachments); }
public EmailSender(EmailServerSettings emailServerSettings) { this.EmailServerSettings = emailServerSettings; }
private void _btnSendIt_Click(object sender, EventArgs e) { dynamic model; try { model = JObject.Parse(_txtJson.Text); } catch (Exception err) { MessageBox.Show(err.ToString(), "Error"); return; } EmailServerSettings settings = null; if(_rdoUseSpecifiedSettings.Checked) { if(string.IsNullOrEmpty(_txtSmtpServerName.Text)) { MessageBox.Show("Please enter a server name", "Error"); return; } settings = new EmailServerSettings { Host = _txtSmtpServerName.Text, Port = Convert.ToInt32(_numPort.Value), UserName = _txtUserName.Text, Password = _txtPassword.Text }; } string from = _txtFrom.Text; if(string.IsNullOrEmpty(_txtFrom.Text)) { from = "*****@*****.**"; } if(string.IsNullOrEmpty(_txtFrom.Text)) { MessageBox.Show("Please enter at least one To email", "Error"); return; } List<string> toList = new List<string>(_txtFrom.Text.Split(new char[] {';'}, StringSplitOptions.RemoveEmptyEntries)); RazorEmailEngine emailEngine = new RazorEmailEngine(settings); try { emailEngine.SendEmail(_txtSubject.Text, model, _txtRazorView.Text, toList, from); MessageBox.Show("Message sent successfully!"); } catch(Exception err) { MessageBox.Show(err.ToString(), "Error"); return; } }