/// <summary> /// Sends an email one at a time, with variable data for each message body /// Uses String.Format to handle a template. /// Dim Template As string = "Confirmation email for {0}. " + _ /// "Date: {1}. Weeks: {2}\n" + _ /// "Name: {3}\n" + _ /// "{4}" /// String.Format(Template, "Spot1", Date.UtcNow, "Spot3", "Spot4") /// </summary> /// <param name="mailToAddressesAndData">email address and all the data for that email message</param> /// <param name="title"></param> /// <param name="contents"></param> /// <param name="legal"></param> /// <param name="callback"></param> /// <param name="callbackData"></param> /// <param name="addressseparator"></param> /// <returns></returns> public Dictionary <string, bool> SendTemplate(List <KeyValuePair <string, List <string> > > mailToAddressesAndData, string title, string contents, FooterLegal legal, SendCompletedCallbackDelegate callback, string callbackData, char addressseparator = ';') { var returnValue = new Dictionary <string, bool>(); var mailResult = new KeyValuePair <string, bool>(); var mailTo = new List <string>(); var titleFilled = string.Empty; foreach (var Item_loopVariable in mailToAddressesAndData) { mailTo.Clear(); mailTo.Add(Item_loopVariable.Key); contents = new TemplateBuilder(contents, Item_loopVariable.Value).ToString(); mailResult = Send(mailTo, title, contents, legal, callback, callbackData); returnValue.Add(mailResult.Key, mailResult.Value); } return(returnValue); }
/// <summary> /// Sends a mail based on off SMTP settings in .config /// </summary> /// <param name="mailToAddresses"></param> /// <param name="title"></param> /// <param name="contents"></param> /// <param name="legal"></param> /// <param name="callback"></param> /// <param name="callbackData"></param> /// <param name="addressseparator"></param> /// <returns>Email addresses and their send status (true or false)</returns> public KeyValuePair <String, Boolean> Send(System.Collections.Generic.List <string> mailToAddresses, string title, string contents, FooterLegal legal, SendCompletedCallbackDelegate callback, string callbackData, char addressseparator = ';') { var returnValue = new KeyValuePair <String, bool>(); var client = new SmtpClient(); var footer = string.Empty; var toAddresses = new List <string>(); try { // Never batch send for legal reasons. Have to put email in the footer of every email foreach (var emailAddress in mailToAddresses) { if (emailAddress.IsEmail(false)) { // Will get 'from, etc' from .config file var OutgoingMail = new MailMessage(); OutgoingMail.To.Add(new MailAddress(emailAddress)); OutgoingMail.Subject = title; OutgoingMail.Body = contents + (legal.PublishFooter ? legal.ToFooter() : string.Empty); OutgoingMail.IsBodyHtml = true; OutgoingMail.Priority = MailPriority.Normal; client.SendCompleted += SendCompletedCallback; client.Send(OutgoingMail); } } } catch { throw; } finally { client.Dispose(); } // Return success/failre return(returnValue); }