/// <summary>Send email via SMTP service</summary> /// <param name="objEmail">Email object containing email parameters</param> /// <returns>Success</returns> public bool SendMail(Email objEmail) { MailMessage mailMsg = new MailMessage(); SmtpClient smtp; if (objEmail.UseBackupSMTP || objEmail.To.ToLower().Contains("@aol.")) { //set up backup username, password and SMTP host smtp = new SmtpClient(ConfigurationSettings.AppSettings["BackupSMTP"].ToString(), 587); smtp.UseDefaultCredentials = false; smtp.EnableSsl = true; smtp.DeliveryMethod = SmtpDeliveryMethod.Network; smtp.Credentials = new NetworkCredential(ConfigurationSettings.AppSettings["BackupSMTPUsername"].ToString(), ConfigurationSettings.AppSettings["BackupSMTPPassword"].ToString()); mailMsg.From = new MailAddress(ConfigurationSettings.AppSettings["BackupSMTPAddress"]); } else { //use default web.config mail settings smtp = new SmtpClient(); mailMsg.From = new MailAddress(objEmail.From); //set delivery method if required if (objEmail.FireAndForget) smtp.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis; } mailMsg.To.Add(objEmail.To); mailMsg.Subject = objEmail.Subject; mailMsg.Body = objEmail.Body; mailMsg.IsBodyHtml = true; if (objEmail.HighPriority) mailMsg.Priority = MailPriority.High; try { smtp.Send(mailMsg); return true; } catch (Exception ex) { return false; } }
/// <summary> /// Sends the emails. /// </summary> /// <param name="url">The URL.</param> /// <param name="userName">Name of the user.</param> /// <param name="password">The password.</param> void IMailUnitOfWork.SendEmails(string url, string userName, string password) { List<Mail> listOfMails = EmailsToBeSent(); Email email = null; EmailUnitOfWork emailUnitOfWork = new EmailUnitOfWork(); foreach (Mail mail in listOfMails) { mail.URL = "http://*****:*****@"<var user = ""(ScheduMail.API.Contracts.User)Data""/> <var promotion = ""(from p in user.Data.Elements('promotion') select new { Product = (string)p.Element('product'), Discount = (string)p.Element('discount'), Expires = (string)p.Element('expires') }).FirstOrDefault()""/> Dear !{user.FirstName}, We are excited to tell you about our latest offerings. Due to your long standing account with us we would like to give you a sneak peak at our latest product before commercial release. The !{promotion.Product} is the best thing since slice bread and for a limited time only we would like to extend you a discount of !{promotion.Discount}. Act soon because the offer is only good until !{promotion.Expires}. Happy Buying! Click <a href='http://somecompany.com/unsubscribe?user=!{user.EmailAddress}'>here</a> to unsubscribe from out mailings. "; var parser = ServiceLocator.Resolve<ITemplateParser>(); foreach (var user in users) { email = new Email(); email.Body = parser.Render(user, "emailTemplate", template); email.To = user.EmailAddress; email.Subject = ""; email.From = ""; email.FireAndForget = true; emailUnitOfWork.SendMail(email); Console.Write(email); Console.WriteLine(); } } }