예제 #1
0
 public void Send(Stream body, string recipients, ReplacementCollection replacements, IEnumerable <Attachment> attachments)
 {
     using (var msg = CreateMailMessage(body, recipients, replacements, attachments))
     {
         var client = new SmtpClient();
         client.Send(msg);
     }
 }
예제 #2
0
 public void Send(string body, string recipients, ReplacementCollection replacements)
 {
     using (var msg = CreateMailMessage(body, recipients, replacements, null))
     {
         var client = new SmtpClient();
         client.Send(msg);
     }
 }
예제 #3
0
        public MailMessage CreateMailMessage(Stream body, string recipients, ReplacementCollection replacements, IEnumerable <Attachment> attachments)
        {
            string bodyText = null;

            if (body != null)
            {
                using (var reader = new StreamReader(body))
                {
                    bodyText = reader.ReadToEnd();
                }
            }

            return(CreateMailMessage(bodyText, recipients, replacements, attachments));
        }
예제 #4
0
        private static string ApplyReplacements(string str, ReplacementCollection replacements, bool IsHtml)
        {
            if (!string.IsNullOrEmpty(str) && replacements != null)
            {
                foreach (var Item in replacements)
                {
                    string Name  = Item.Name.StartsWith("<%", StringComparison.Ordinal) ? Item.Name : "<%" + Item.Name + "%>";
                    string Value = Item.Value ?? "";

                    if (IsHtml)
                    {
                        if (Item.HtmlEncoding)
                        {
                            Value = Regex.Replace(System.Web.HttpUtility.HtmlEncode(Value), "\r\n|\r|\n", "<br />");
                        }

                        str = Regex.Replace(str, System.Web.HttpUtility.HtmlEncode(Name), Value, RegexOptions.IgnoreCase);
                    }

                    str = Regex.Replace(str, Name, Value, RegexOptions.IgnoreCase);
                }
            }
            return(str);
        }
예제 #5
0
 public MailMessage CreateMailMessage(string recipients, ReplacementCollection replacements, IEnumerable <Attachment> attachments)
 {
     return(CreateMailMessage((string)null, recipients, replacements, attachments));
 }
예제 #6
0
 public MailMessage CreateMailMessage(string recipients, ReplacementCollection replacements)
 {
     return(CreateMailMessage((string)null, recipients, replacements, null));
 }
예제 #7
0
 private static string ApplyReplacements(string str, ReplacementCollection replacements)
 {
     return(ApplyReplacements(str, replacements, false));
 }
예제 #8
0
 public MailMessage CreateMailMessage(Stream body, string recipients, ReplacementCollection replacements)
 {
     return(CreateMailMessage(body, recipients, replacements, null));
 }
예제 #9
0
        public MailMessage CreateMailMessage(string body, string recipients, ReplacementCollection replacements, IEnumerable <Attachment> attachments)
        {
            if (body == null && !string.IsNullOrEmpty(this.BodyFileName))
            {
                body = File.ReadAllText(GetBodyFileNameForCulture());
            }

            string from = this.From;

            if (string.IsNullOrEmpty(from))
            {
                from = GetDefaultFrom();
            }
            from = ApplyReplacements(from, replacements);

            if (recipients != null)
            {
                recipients = ApplyEmailReplacement(ApplyReplacements(recipients.Replace(';', ','), replacements), from, EmailReplacementType.From);
            }

            string cc = this.CC;

            if (cc != null)
            {
                cc = ApplyEmailReplacement(ApplyReplacements(cc.Replace(';', ','), replacements), from, EmailReplacementType.From);
            }

            string bcc = this.Bcc;

            if (bcc != null)
            {
                bcc = ApplyEmailReplacement(ApplyReplacements(bcc.Replace(';', ','), replacements), from, EmailReplacementType.From);
            }

            if (!string.IsNullOrEmpty(body))
            {
                //Body replacements
                body = ApplyReplacements(body, replacements, this.IsBodyHtml);
                body = ApplyEmailReplacement(body, from, EmailReplacementType.From, this.IsBodyHtml);
                body = ApplyEmailReplacement(body, recipients, EmailReplacementType.To, this.IsBodyHtml);
                body = ApplyEmailReplacement(body, cc, EmailReplacementType.CC, this.IsBodyHtml);
                body = ApplyEmailReplacement(body, bcc, EmailReplacementType.Bcc, this.IsBodyHtml);
            }

            var message = new MailMessage()
            {
                From = new MailAddress(from), IsBodyHtml = this.IsBodyHtml, Priority = this.Priority
            };

            try
            {
                foreach (var address in ParseRecipients(recipients))
                {
                    message.To.Add(address);
                }
                foreach (var address in ParseRecipients(cc))
                {
                    message.CC.Add(address);
                }
                foreach (var address in ParseRecipients(bcc))
                {
                    message.Bcc.Add(address);
                }

                string subject = this.Subject;
                if (string.IsNullOrEmpty(subject) && this.IsBodyHtml)
                {
                    subject = ExtractSubjectFromHtmlBody(body);
                }

                if (!string.IsNullOrEmpty(subject))
                {
                    //Subject replacements
                    subject = ApplyReplacements(subject, replacements);
                    subject = ApplyEmailReplacement(subject, from, EmailReplacementType.From);
                    subject = ApplyEmailReplacement(subject, recipients, EmailReplacementType.To);
                    subject = ApplyEmailReplacement(subject, cc, EmailReplacementType.CC);
                    subject = ApplyEmailReplacement(subject, bcc, EmailReplacementType.Bcc);

                    message.Subject = subject;
                }

                if (m_EmbeddedObjects != null && m_EmbeddedObjects.Count != 0)
                {
                    message.AlternateViews.Add(GetAlternateView(body));
                }
                else if (!string.IsNullOrEmpty(body))
                {
                    message.Body = body;
                }

                if (attachments != null)
                {
                    foreach (var attachment in attachments)
                    {
                        message.Attachments.Add(attachment);
                    }
                }
            }
            catch
            {
                message.Dispose();
                throw;
            }

            return(message);
        }