예제 #1
0
        public static void SendMsg(string SysFromEmail, string CmsHost, MailAddress From, string subject, string Message, List <MailAddress> to, int id, int?pid, List <LinkedResource> attachments = null)
        {
            if (ConfigurationManager.AppSettings["sendemail"] == "false")
            {
                return;
            }

            var senderrorsto = ConfigurationManager.AppSettings["senderrorsto"];
            var msg          = new MailMessage();

            if (From == null)
            {
                From = FirstAddress(senderrorsto);
            }
            var problemDomains = (ConfigurationManager.AppSettings["ProblemDomainsForEmail"] ?? "").Split(',');

            if (problemDomains.Any(dd => From.Host.ToLower() == dd || to.Any(ee => ee.Host.ToLower() == dd)))
            {
                if (!SysFromEmail.HasValue())
                {
                    SysFromEmail = "*****@*****.**";
                }
                var sysmail = new MailAddress(SysFromEmail, From.DisplayName);
                msg.From = sysmail;
                msg.ReplyToList.Add(From);
            }
            else
            {
                msg.From = From;
                if (SysFromEmail.HasValue())
                {
                    var sysmail = new MailAddress(SysFromEmail);
                    if (From.Host != sysmail.Host)
                    {
                        msg.Sender = sysmail;
                    }
                }
            }

            msg.Headers.Add("X-SMTPAPI",
                            $"{{\"unique_args\":{{\"host\":\"{CmsHost}\",\"mailid\":\"{id}\",\"pid\":\"{pid}\"}}}}");
            msg.Headers.Add("X-BVCMS", $"host:{CmsHost}, mailid:{id}, pid:{pid}");

            foreach (var ma in to)
            {
                if (ma.Host != "nowhere.name" || IsInRoleEmailTest)
                {
                    msg.AddAddr(ma);
                }
            }
            msg.Subject = subject;
            var addrs        = string.Join(", ", to.Select(tt => tt.ToString()));
            var BadEmailLink = "";

            if (msg.To.Count == 0 && to.Any(tt => tt.Host == "nowhere.name"))
            {
                return;
            }
            if (msg.To.Count == 0)
            {
                msg.AddAddr(msg.From);
                msg.AddAddr(FirstAddress(senderrorsto));
                msg.Subject += $"-- bad addr for {CmsHost}({pid})";
                BadEmailLink = $"<p><a href='{CmsHost}/Person2/{pid}'>bad addr for</a></p>\n";
            }

            var regex     = new Regex("</?([^>]*)>", RegexOptions.IgnoreCase | RegexOptions.Multiline);
            var text      = regex.Replace(Message, string.Empty);
            var htmlView1 = AlternateView.CreateAlternateViewFromString(text, Encoding.UTF8, MediaTypeNames.Text.Plain);

            htmlView1.TransferEncoding = TransferEncoding.Base64;
            msg.AlternateViews.Add(htmlView1);

            var html   = BadEmailLink + Message;
            var result = PreMailer.Net.PreMailer.MoveCssInline(html);

            html = result.Html;
            var htmlView = AlternateView.CreateAlternateViewFromString(html, Encoding.UTF8, MediaTypeNames.Text.Html);

            htmlView.TransferEncoding = TransferEncoding.Base64;
            if (attachments != null)
            {
                foreach (var a in attachments)
                {
                    htmlView.LinkedResources.Add(a);
                }
            }
            msg.AlternateViews.Add(htmlView);

            try
            {
                var smtp = Smtp();
                smtp.Send(msg);
            }
            finally
            {
                htmlView.Dispose();
            }
        }
예제 #2
0
파일: Emailer.cs 프로젝트: thewruck/bvcms
        public static void SendMsg(string sysFromEmail, string cmsHost, MailAddress fromAddress, string subject, string message, List <MailAddress> to, int id, int?pid, List <LinkedResource> attachments = null, List <MailAddress> cc = null)

        {
            if (ConfigurationManager.AppSettings["sendemail"] == "false")
            {
                return;
            }

            var senderrorsto = ConfigurationManager.AppSettings["senderrorsto"];
            var msg          = new MailMessage();

            if (fromAddress == null)
            {
                fromAddress = FirstAddress(senderrorsto);
            }
            var problemDomains = (ConfigurationManager.AppSettings["ProblemDomainsForEmail"] ?? "").Split(',');

            if (problemDomains.Any(dd => fromAddress.Host.ToLower() == dd || to.Any(ee => ee.Host.ToLower() == dd)))
            {
                if (!sysFromEmail.HasValue())
                {
                    sysFromEmail = "*****@*****.**";
                }
                var sysmail = new MailAddress(sysFromEmail, fromAddress.DisplayName);
                msg.From = sysmail;
                msg.ReplyToList.Add(fromAddress);
            }
            else
            {
                msg.From = fromAddress;
                if (sysFromEmail.HasValue())
                {
                    var sysmail = new MailAddress(sysFromEmail);
                    if (fromAddress.Host != sysmail.Host)
                    {
                        msg.Sender = sysmail;
                    }
                }
            }
            if (cc != null)
            {
                foreach (var a in cc)
                {
                    msg.ReplyToList.Add(a);
                }
                if (!msg.ReplyToList.Contains(msg.From) && msg.From.Address.NotEqual("*****@*****.**"))
                {
                    msg.ReplyToList.Add(msg.From);
                }
            }

            msg.Headers.Add("X-SMTPAPI",
                            $"{{\"unique_args\":{{\"host\":\"{cmsHost}\",\"mailid\":\"{id}\",\"pid\":\"{pid}\"}}}}");
            msg.Headers.Add("X-BVCMS", $"host:{cmsHost}, mailid:{id}, pid:{pid}");

            foreach (var ma in to)
            {
                if (ma.Host != "nowhere.name" || IsInRoleEmailTest)
                {
                    msg.AddAddr(ma);
                }
            }
            msg.Subject = subject;
            var addrs        = string.Join(", ", to.Select(tt => tt.ToString()));
            var badEmailLink = "";

            if (msg.To.Count == 0 && to.Any(tt => tt.Host == "nowhere.name"))
            {
                return;
            }
            if (msg.To.Count == 0)
            {
                msg.AddAddr(msg.From);
                msg.AddAddr(FirstAddress(senderrorsto));
                msg.Subject += $"-- bad addr for {cmsHost}({pid})";
                badEmailLink = $"<p><a href='{cmsHost}/Person2/{pid}'>bad addr for</a></p>\n";
            }

            var regex     = new Regex("</?([^>]*)>", RegexOptions.IgnoreCase | RegexOptions.Multiline);
            var text      = regex.Replace(message, string.Empty);
            var htmlView1 = AlternateView.CreateAlternateViewFromString(text, Encoding.UTF8, MediaTypeNames.Text.Plain);

            htmlView1.TransferEncoding = TransferEncoding.Base64;
            msg.AlternateViews.Add(htmlView1);


            var html = badEmailLink + message;

            if (cc != null && cc.Count > 0)
            {
                string cclist = (string.Join(", ", cc));

                var ccstring = $"<p align='center'><small><i>This email was CC\'d to the email addresses below and they are included in the Reply-To Field.</br>" + cclist + "</i></small></p>";
                html = html + ccstring;
            }


            var result = PreMailer.Net.PreMailer.MoveCssInline(html);

            html = result.Html;
            var htmlView = AlternateView.CreateAlternateViewFromString(html, Encoding.UTF8, MediaTypeNames.Text.Html);

            htmlView.TransferEncoding = TransferEncoding.Base64;
            if (attachments != null)
            {
                foreach (var a in attachments)
                {
                    htmlView.LinkedResources.Add(a);
                }
            }
            msg.AlternateViews.Add(htmlView);

            try
            {
                var smtp = Smtp();
                smtp.Send(msg);
            }
            finally
            {
                htmlView.Dispose();
            }
        }
예제 #3
0
        public string SendSmtpMsg(MailAddress fromAddress, string subject, string message, List <MailAddress> to, int?id, int?pid, List <LinkedResource> attachments = null, List <MailAddress> cc = null)
        {
            var senderrorsto = ConfigurationManager.AppSettings["senderrorsto"];
            var msg          = new MailMessage();

            if (fromAddress == null)
            {
                fromAddress = Util.FirstAddress(senderrorsto);
            }

            var fromDomain = DefaultFromDomain;

            msg.From = new MailAddress(fromDomain, fromAddress.DisplayName);
            msg.ReplyToList.Add(fromAddress);
            if (cc != null)
            {
                foreach (var a in cc)
                {
                    msg.ReplyToList.Add(a);
                }
                if (!msg.ReplyToList.Contains(msg.From) && msg.From.Address.NotEqual(fromDomain))
                {
                    msg.ReplyToList.Add(msg.From);
                }
            }

            msg.Headers.Add(XSmtpApi, XSmtpApiHeader(id, pid, fromDomain));
            msg.Headers.Add(XBvcms, XBvcmsHeader(id, pid));

            foreach (var ma in to)
            {
                if (ma.Host != "nowhere.name" || Util.IsInRoleEmailTest)
                {
                    msg.AddAddr(ma);
                }
            }

            msg.Subject = subject;
            var badEmailLink = "";

            if (msg.To.Count == 0 && to.Any(tt => tt.Host == "nowhere.name"))
            {
                return(null);
            }
            if (msg.To.Count == 0)
            {
                msg.AddAddr(msg.From);
                msg.AddAddr(Util.FirstAddress(senderrorsto));
                msg.Subject += $"-- bad addr for {CmsHost}({pid})";
                badEmailLink = $"<p><a href='{CmsHost}/Person2/{pid}'>bad addr for</a></p>\n";
            }

            var regex    = new Regex("</?([^>]*)>", RegexOptions.IgnoreCase | RegexOptions.Multiline);
            var text     = regex.Replace(message, string.Empty);
            var textview = AlternateView.CreateAlternateViewFromString(text, Encoding.UTF8, MediaTypeNames.Text.Plain);

            textview.TransferEncoding = TransferEncoding.Base64;
            msg.AlternateViews.Add(textview);

            var html = badEmailLink + message + CcMessage(cc);

            using (var htmlView = AlternateView.CreateAlternateViewFromString(html, Encoding.UTF8, MediaTypeNames.Text.Html))
            {
                htmlView.TransferEncoding = TransferEncoding.Base64;
                if (attachments != null)
                {
                    foreach (var a in attachments)
                    {
                        htmlView.LinkedResources.Add(a);
                    }
                }
                msg.AlternateViews.Add(htmlView);

                var smtp = Smtp();
                smtp.Send(msg);
            }
            return(fromDomain);
        }
예제 #4
0
        public static void sendmsg(string SysFromEmail, string CmsHost, MailAddress From, string subject, string Message, List <MailAddress> to, int id, int?pid, List <LinkedResource> attachments = null)
        {
            var senderrorsto = ConfigurationManager.AppSettings["senderrorsto"];
            var msg          = new MailMessage();

            if (From == null)
            {
                From = Util.FirstAddress(senderrorsto);
            }

            msg.From = From;
            if (SysFromEmail.HasValue())
            {
                var sysmail = new MailAddress(SysFromEmail);
                if (From.Host != sysmail.Host)
                {
                    msg.Sender = sysmail;
                }
            }
            msg.Headers.Add("X-SMTPAPI",
                            "{{\"unique_args\":{{\"host\":\"{0}\",\"mailid\":\"{1}\",\"pid\":\"{2}\"}}}}"
                            .Fmt(CmsHost, id, pid));
            msg.Headers.Add("X-BVCMS", "host:{0}, mailid:{1}, pid:{2}".Fmt(CmsHost, id, pid));

            foreach (var ma in to)
            {
                if (ma.Host != "nowhere.name" || IsInRoleEmailTest)
                {
                    msg.AddAddr(ma);
                }
            }
            msg.Subject = subject;
            var addrs        = string.Join(", ", to.Select(tt => tt.ToString()));
            var BadEmailLink = "";

            if (msg.To.Count == 0)
            {
                msg.AddAddr(msg.From);
                msg.AddAddr(Util.FirstAddress(senderrorsto));
                msg.Subject += "-- bad addr for {0}({1})".Fmt(CmsHost, pid);
                BadEmailLink = "<p><a href='{0}Person/Index/{1}'>bad addr for</a></p>\n".Fmt(CmsHost, pid);
            }

            var regex     = new Regex("</?([^>]*)>", RegexOptions.IgnoreCase | RegexOptions.Multiline);
            var text      = regex.Replace(Message, string.Empty);
            var htmlView1 = AlternateView.CreateAlternateViewFromString(text, Encoding.UTF8, MediaTypeNames.Text.Plain);

            htmlView1.TransferEncoding = TransferEncoding.Base64;
            msg.AlternateViews.Add(htmlView1);

            var html     = BadEmailLink + Message;
            var htmlView = AlternateView.CreateAlternateViewFromString(html, Encoding.UTF8, MediaTypeNames.Text.Html);

            htmlView.TransferEncoding = TransferEncoding.Base64;
            if (attachments != null)
            {
                foreach (var a in attachments)
                {
                    htmlView.LinkedResources.Add(a);
                }
            }
            msg.AlternateViews.Add(htmlView);

            try
            {
                var smtp = Util.Smtp();
                smtp.Send(msg);
            }
            catch (Exception ex)
            {
                throw;
            }
            finally
            {
                htmlView.Dispose();
            }
        }