/// <summary> /// Sends an email for the given details /// </summary> /// <param name="emailDetail">Email details</param> public void Send(EmailDetail emailDetail) { var mail = new MailMessage { Subject = emailDetail.Subject, Body = RelativeToAbsoluteUrls(emailDetail.Body), IsBodyHtml = emailDetail.IsBodyHtml }; AddSender(mail, emailDetail); AddRecipients(mail, emailDetail); AddBlindCopyRecipients(mail, emailDetail); AddAttachments(mail, emailDetail); try { using (var client = new SmtpClient()) { client.Send(mail); } } catch (SmtpException ex) { _loggingService.Log(string.Format("Error sending mail with code: {0}, {1}", ex.StatusCode, ex.Message), LogLevel.Error); } }
public ActionResult Register(RegistrationFormViewModel vm) { if (!ModelState.IsValid) { return CurrentUmbracoPage(); } var member = Members.CreateRegistrationModel(); member.Name = vm.Name; member.Email = vm.Email; member.Password = vm.Password; member.UsernameIsEmail = true; MembershipCreateStatus status; Members.RegisterMember(member, out status); if (!status.Equals(MembershipCreateStatus.Success)) { return CurrentUmbracoPage(); } var email = new EmailDetail { To = new List<string> { member.Email }, Subject = "Registraion confirmation", IsBodyHtml = false }; _mailer.Send(email); return RedirectToCurrentUmbracoPage(); }
private static void AddAttachments(MailMessage mail, EmailDetail emailDetail) { if (emailDetail.Attachments == null || !emailDetail.Attachments.Any()) { return; } foreach (var file in emailDetail.Attachments.Where(x => !string.IsNullOrEmpty(x))) { mail.Attachments.Add(new Attachment(string.Concat(AppDomain.CurrentDomain.BaseDirectory, file))); } }
private static void AddBlindCopyRecipients(MailMessage mail, EmailDetail emailDetail) { if (emailDetail.Bcc == null || !emailDetail.Bcc.Any()) { return; } foreach (var to in emailDetail.Bcc.Distinct()) { mail.Bcc.Add(new MailAddress(to)); } }
private static void AddRecipients(MailMessage mail, EmailDetail emailDetail) { if (emailDetail.To == null || !emailDetail.To.Any()) { mail.To.Add(new MailAddress(_emailAddress)); return; } foreach (var to in emailDetail.To.Distinct()) { mail.To.Add(new MailAddress(to)); } }
public ActionResult AjaxSendEmail(ContactFormViewModel vm) { if (!ModelState.IsValid) { return PartialView("_ContactForm", vm); } var email = new EmailDetail { From = vm.Email, DisplayName = vm.FullName, Subject = vm.Subject, Body = vm.Message, IsBodyHtml = false }; _mailer.Send(email); return Content(string.Format("<div class=\"alert alert-success\" role=\"alert\">{0}</div>", vm.ThankYouText), "text/html"); }
private static void AddSender(MailMessage mail, EmailDetail emailDetail) { mail.From = string.IsNullOrEmpty(emailDetail.From) ? new MailAddress(_emailAddress, _displayName) : new MailAddress(emailDetail.From, emailDetail.DisplayName); }