/// <summary> /// Returns all the recipient for the given <paramref name="type"/> /// </summary> /// <param name="type">The <see cref="Storage.Recipient.RecipientType"/> to return</param> /// <returns></returns> public List <RecipientPlaceHolder> GetEmailRecipients(Storage.Recipient.RecipientType type) { var recipients = new List <RecipientPlaceHolder>(); // ReSharper disable once LoopCanBeConvertedToQuery foreach (var recipientRow in this) { // First we filter for the correct recipient type if (recipientRow.RecipientType == type) { recipients.Add(new RecipientPlaceHolder(recipientRow.EmailAddress, recipientRow.DisplayName, recipientRow.AddresType)); } } return(recipients); }
/// <summary> /// Change the E-mail sender addresses to a human readable format /// </summary> /// <param name="message">The Storage.Message object</param> /// <param name="convertToHref">When true the E-mail addresses are converted to hyperlinks</param> /// <param name="type">This types says if we want to get the TO's or CC's</param> /// <param name="html">Set this to true when the E-mail body format is html</param> /// <returns></returns> private static string GetEmailRecipients(Storage.Message message, Storage.Recipient.RecipientType type, bool convertToHref, bool html) { var output = string.Empty; var recipients = new List <Recipient>(); if (message == null) { return(output); } foreach (var recipient in message.Recipients) { // First we filter for the correct recipient type if (recipient.Type == type) { recipients.Add(new Recipient { EmailAddress = recipient.Email, DisplayName = recipient.DisplayName }); } } if (recipients.Count == 0 && message.Headers != null) { switch (type) { case Storage.Recipient.RecipientType.To: if (message.Headers.To != null) { recipients.AddRange(message.Headers.To.Select(to => new Recipient { EmailAddress = to.Address, DisplayName = to.DisplayName })); } break; case Storage.Recipient.RecipientType.Cc: if (message.Headers.Cc != null) { recipients.AddRange(message.Headers.Cc.Select(cc => new Recipient { EmailAddress = cc.Address, DisplayName = cc.DisplayName })); } break; case Storage.Recipient.RecipientType.Bcc: if (message.Headers.Bcc != null) { recipients.AddRange(message.Headers.Bcc.Select(bcc => new Recipient { EmailAddress = bcc.Address, DisplayName = bcc.DisplayName })); } break; } } foreach (var recipient in recipients) { if (output != string.Empty) { output += "; "; } var tempEmailAddress = RemoveSingleQuotes(recipient.EmailAddress); var tempDisplayName = RemoveSingleQuotes(recipient.DisplayName); var emailAddress = tempEmailAddress; var displayName = tempDisplayName; // Sometimes the E-mail address and displayname get swapped so check if they are valid if (!IsEmailAddressValid(tempEmailAddress) && IsEmailAddressValid(tempDisplayName)) { // Swap them emailAddress = tempDisplayName; displayName = tempEmailAddress; } else if (IsEmailAddressValid(tempDisplayName)) { // If the displayname is an emailAddress them move it emailAddress = tempDisplayName; displayName = tempDisplayName; } if (html) { emailAddress = HttpUtility.HtmlEncode(emailAddress); displayName = HttpUtility.HtmlEncode(displayName); } if (convertToHref && html && !string.IsNullOrEmpty(emailAddress)) { output += "<a href=\"mailto:" + emailAddress + "\">" + (!string.IsNullOrEmpty(displayName) ? displayName : emailAddress) + "</a>"; } else { if (!string.IsNullOrEmpty(emailAddress)) { output = emailAddress; } if (!string.IsNullOrEmpty(displayName)) { output += (!string.IsNullOrEmpty(emailAddress) ? " <" : string.Empty) + displayName + (!string.IsNullOrEmpty(emailAddress) ? ">" : string.Empty); } } } return(output); }