/// <summary> /// Composes email using content-managed text and placeholders. /// </summary> /// <param name="report">Report to append to email body.</param> /// <param name="item">Item to send information on.</param> /// <param name="username">Receiver.</param> /// <returns>An <see cref="T:System.Net.Mail.MailMessage"/> email message ready to send.</returns> private static MailMessage ComposeEmail(string report, Item item, string username) { NotificationEmail mail = new NotificationEmail(); string to = string.Empty; string bcc = string.Empty; string publishedBy = string.Empty; if (!string.IsNullOrWhiteSpace(username)) { var author = User.FromName(username, false); if (author.Profile != null && !string.IsNullOrWhiteSpace(author.Profile.Email)) { to = author.Profile.Email; publishedBy = !string.IsNullOrWhiteSpace(author.Profile.FullName) ? author.Profile.FullName : author.Name; } } if (!string.IsNullOrWhiteSpace(mail.EmailTo)) { if (string.IsNullOrEmpty(to)) { var index = mail.EmailTo.IndexOf(','); if (index == -1) { to = mail.EmailTo.Trim(); } else { to = mail.EmailTo.Substring(0, index); bcc = mail.EmailTo.Substring(index + 1).Trim(); } } else { bcc = mail.EmailTo; } } if (SectionsEmailSettings.Enabled) { string emailList = GetEmailsForSection(item); if (!string.IsNullOrEmpty(emailList)) { if (!string.IsNullOrWhiteSpace(bcc)) { bcc = string.Format("{0}, {1}", bcc, emailList); } else { bcc = emailList; } } to = string.IsNullOrWhiteSpace(to) && bcc.Length > 0 ? bcc.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries)[0] : to; } if (bcc.Contains(",")) { var uniqueBccAddresses = bcc .Replace(to, "") .Split(new[] {", "}, StringSplitOptions.RemoveEmptyEntries) .Distinct(); bcc = string.Join(", ", uniqueBccAddresses); } if (string.IsNullOrWhiteSpace(to)) { return null; } string subject = mail.Subject .Replace("[item]", item.DisplayName) .Replace("[path]", item.Paths.FullPath) .Replace("[date]", DateTime.Now.ToShortDateString()) .Replace("[time]", DateTime.Now.ToShortTimeString()) .Replace("[version]", item.Version.ToString()) .Replace("[id]", item.ID.ToString()) .Replace("[publisher]", publishedBy); string body = mail.Body .Replace("[item]", item.DisplayName) .Replace("[path]", item.Paths.FullPath) .Replace("[date]", DateTime.Now.ToShortDateString()) .Replace("[time]", DateTime.Now.ToShortTimeString()) .Replace("[version]", item.Version.ToString()) .Replace("[id]", item.ID.ToString()) .Replace("[publisher]", publishedBy); MailMessage mailMessage = new MailMessage { From = new MailAddress(mail.EmailFrom), To = { to }, Subject = subject, Body = body + Environment.NewLine + report, IsBodyHtml = true, }; if (!string.IsNullOrEmpty(bcc)) { mailMessage.Bcc.Add(bcc); } return mailMessage; }
/// <summary> /// Composes email using content-managed text and placeholders. /// </summary> /// <param name="report">Report to append to email body.</param> /// <param name="item">Item to send information on.</param> /// <param name="sendTo">Receiver's email address.</param> /// <returns>An <see cref="T:System.Net.Mail.MailMessage"/> email message ready to send.</returns> private static MailMessage ComposeEmail(string report, Item item, string sendTo) { NotificationEmail mail = new NotificationEmail(); string to = string.Empty; string bcc = string.Empty; if (!string.IsNullOrEmpty(sendTo)) { to = sendTo; } if (!string.IsNullOrWhiteSpace(mail.EmailTo)) { if (string.IsNullOrEmpty(to)) { var index = mail.EmailTo.IndexOf(','); if (index == -1) { to = mail.EmailTo.Trim(); } else { to = mail.EmailTo.Substring(0, index); bcc = mail.EmailTo.Substring(index + 1).Trim(); } } else { bcc = mail.EmailTo; } } if (string.IsNullOrWhiteSpace(to)) { return null; } string body = mail.Body.Replace("[item]", item.DisplayName) .Replace("[path]", item.Paths.FullPath) .Replace("[date]", DateTime.Now.ToShortDateString()) .Replace("[time]", DateTime.Now.ToShortTimeString()) .Replace("[version]", item.Version.ToString()) .Replace("[id]", item.ID.ToString()); MailMessage mailMessage = new MailMessage { From = new MailAddress(mail.EmailFrom), To = { to }, Subject = mail.Subject.Replace("[item]", item.DisplayName), Body = body + Environment.NewLine + report, IsBodyHtml = true, }; if (!string.IsNullOrEmpty(bcc)) { mailMessage.Bcc.Add(bcc); } return mailMessage; }