Example #1
0
 private static string ApplyEmailReplacement(string str, string addresses, EmailReplacementType type)
 {
     return(ApplyEmailReplacement(str, addresses, type, false));
 }
Example #2
0
        private static string ApplyEmailReplacement(string str, string addresses, EmailReplacementType type, bool isHtml)
        {
            if (!string.IsNullOrEmpty(str))
            {
                switch (type)
                {
                case EmailReplacementType.From:
                    MailAddress from = null;
                    if (addresses != null)
                    {
                        from = new MailAddress(addresses);
                    }

                    str = Regex.Replace(str, "<%From%>", from == null ? "" : from.Address, RegexOptions.IgnoreCase);
                    str = Regex.Replace(str, "<%FromDisplayName%>", from == null ? "" : string.IsNullOrEmpty(from.DisplayName) ? from.Address : from.DisplayName, RegexOptions.IgnoreCase);
                    str = Regex.Replace(str, "<%FromFullName%>", addresses ?? "", RegexOptions.IgnoreCase);
                    str = Regex.Replace(str, "<%FromFullAddress%>", addresses ?? "", RegexOptions.IgnoreCase);

                    if (isHtml)
                    {
                        str = Regex.Replace(str, "&lt;%From%&gt;", from == null ? "" : from.Address, RegexOptions.IgnoreCase);
                        str = Regex.Replace(str, "&lt;%FromDisplayName%&gt;", from == null ? "" : string.IsNullOrEmpty(from.DisplayName) ? from.Address : from.DisplayName, RegexOptions.IgnoreCase);
                        str = Regex.Replace(str, "&lt;%FromFullName%&gt;", addresses ?? "", RegexOptions.IgnoreCase);
                        str = Regex.Replace(str, "&lt;%FromFullAddress%&gt;", addresses ?? "", RegexOptions.IgnoreCase);
                    }
                    break;

                case EmailReplacementType.To:
                    var to = ParseRecipients(addresses);

                    str = Regex.Replace(str, "<%Recipients%>", string.Join(",", (from i in to select i.Address).ToArray()), RegexOptions.IgnoreCase);
                    str = Regex.Replace(str, "<%RecipientsDisplayName%>", string.Join(",", (from i in to select string.IsNullOrEmpty(i.DisplayName) ? i.Address : i.DisplayName).ToArray()), RegexOptions.IgnoreCase);
                    str = Regex.Replace(str, "<%RecipientsFullName%>", addresses ?? "", RegexOptions.IgnoreCase);
                    str = Regex.Replace(str, "<%RecipientsAddress%>", addresses ?? "", RegexOptions.IgnoreCase);
                    str = Regex.Replace(str, "<%To%>", string.Join(",", (from i in to select i.Address).ToArray()), RegexOptions.IgnoreCase);
                    str = Regex.Replace(str, "<%ToDisplayName%>", string.Join(",", (from i in to select string.IsNullOrEmpty(i.DisplayName) ? i.Address : i.DisplayName).ToArray()), RegexOptions.IgnoreCase);
                    str = Regex.Replace(str, "<%ToFullName%>", addresses ?? "", RegexOptions.IgnoreCase);
                    str = Regex.Replace(str, "<%ToFullAddress%>", addresses ?? "", RegexOptions.IgnoreCase);

                    if (isHtml)
                    {
                        str = Regex.Replace(str, "&lt;%Recipients%&gt;", string.Join(",", (from i in to select i.Address).ToArray()), RegexOptions.IgnoreCase);
                        str = Regex.Replace(str, "&lt;%RecipientsDisplayName%&gt;", string.Join(",", (from i in to select string.IsNullOrEmpty(i.DisplayName) ? i.Address : i.DisplayName).ToArray()), RegexOptions.IgnoreCase);
                        str = Regex.Replace(str, "&lt;%RecipientsFullName%&gt;", addresses ?? "", RegexOptions.IgnoreCase);
                        str = Regex.Replace(str, "&lt;%RecipientsFullAddress%&gt;", addresses ?? "", RegexOptions.IgnoreCase);
                        str = Regex.Replace(str, "&lt;%To%&gt;", string.Join(",", (from i in to select i.Address).ToArray()), RegexOptions.IgnoreCase);
                        str = Regex.Replace(str, "&lt;%ToDisplayName%&gt;", string.Join(",", (from i in to select string.IsNullOrEmpty(i.DisplayName) ? i.Address : i.DisplayName).ToArray()), RegexOptions.IgnoreCase);
                        str = Regex.Replace(str, "&lt;%ToFullName%&gt;", addresses ?? "", RegexOptions.IgnoreCase);
                        str = Regex.Replace(str, "&lt;%ToFullAddress%&gt;", addresses ?? "", RegexOptions.IgnoreCase);
                    }
                    break;

                case EmailReplacementType.CC:
                    var cc = ParseRecipients(addresses);

                    str = Regex.Replace(str, "<%CC%>", string.Join(",", (from i in cc select i.Address).ToArray()), RegexOptions.IgnoreCase);
                    str = Regex.Replace(str, "<%CCDisplayName%>", string.Join(",", (from i in cc select string.IsNullOrEmpty(i.DisplayName) ? i.Address : i.DisplayName).ToArray()), RegexOptions.IgnoreCase);
                    str = Regex.Replace(str, "<%CCFullName%>", addresses ?? "", RegexOptions.IgnoreCase);
                    str = Regex.Replace(str, "<%CCFullAddress%>", addresses ?? "", RegexOptions.IgnoreCase);

                    if (isHtml)
                    {
                        str = Regex.Replace(str, "&lt;%CC%&gt;", string.Join(",", (from i in cc select i.Address).ToArray()), RegexOptions.IgnoreCase);
                        str = Regex.Replace(str, "&lt;%CCDisplayName%&gt;", string.Join(",", (from i in cc select string.IsNullOrEmpty(i.DisplayName) ? i.Address : i.DisplayName).ToArray()), RegexOptions.IgnoreCase);
                        str = Regex.Replace(str, "&lt;%CCFullName%&gt;", addresses ?? "", RegexOptions.IgnoreCase);
                        str = Regex.Replace(str, "&lt;%CCFullAddress%&gt;", addresses ?? "", RegexOptions.IgnoreCase);
                    }
                    break;

                case EmailReplacementType.Bcc:
                    var bcc = ParseRecipients(addresses);

                    str = Regex.Replace(str, "<%Bcc%>", string.Join(",", (from i in bcc select i.Address).ToArray()), RegexOptions.IgnoreCase);
                    str = Regex.Replace(str, "<%BccDisplayName%>", string.Join(",", (from i in bcc select string.IsNullOrEmpty(i.DisplayName) ? i.Address : i.DisplayName).ToArray()), RegexOptions.IgnoreCase);
                    str = Regex.Replace(str, "<%BccFullName%>", addresses ?? "", RegexOptions.IgnoreCase);
                    str = Regex.Replace(str, "<%BccFullAddress%>", addresses ?? "", RegexOptions.IgnoreCase);

                    if (isHtml)
                    {
                        str = Regex.Replace(str, "&lt;%Bcc%&gt;", string.Join(",", (from i in bcc select i.Address).ToArray()), RegexOptions.IgnoreCase);
                        str = Regex.Replace(str, "&lt;%BccDisplayName%&gt;", string.Join(",", (from i in bcc select string.IsNullOrEmpty(i.DisplayName) ? i.Address : i.DisplayName).ToArray()), RegexOptions.IgnoreCase);
                        str = Regex.Replace(str, "&lt;%BccFullName%&gt;", addresses ?? "", RegexOptions.IgnoreCase);
                        str = Regex.Replace(str, "&lt;%BccFullAddress%&gt;", addresses ?? "", RegexOptions.IgnoreCase);
                    }
                    break;
                }
            }

            return(str);
        }