示例#1
0
        public EMLReturnClass Send(List <EMLMailTarget> targets, string m_MailSubject, string m_MailHtmlBody, string m_MailTextBody, bool substitute)
        {
            EMLReturnClass outcome = new EMLReturnClass();

            MailSubject  = m_MailSubject;
            MailHtmlBody = m_MailHtmlBody;
            MailTextBody = m_MailTextBody;

            IRestResponse response = null;
            RestClient    client   = new RestClient();

            client.BaseUrl       = new Uri("https://api.mailgun.net/v3");
            client.Authenticator = new HttpBasicAuthenticator("api", APIKEY);
            foreach (var x in targets)
            {
                if (EMLHelpers.IsValidEmail(x.EmailAddress))
                {
                    try {
                        if (substitute)
                        {
                            MailHtmlBody = EMLHelpers.SubstituteFields(m_MailHtmlBody, x);
                            MailTextBody = EMLHelpers.SubstituteFields(m_MailTextBody, x);
                            MailSubject  = EMLHelpers.SubstituteFields(m_MailSubject, x);
                        }

                        RestRequest request = new RestRequest();
                        request.AddParameter("domain", MAILDOMAIN, ParameterType.UrlSegment);
                        request.Resource = "{domain}/messages";
                        request.AddParameter("from", "" + FromEmailName + " <" + FromEmailUsername + "@" + MAILDOMAIN + ">");
                        if (EMLHelpers.IsValidEmail(ReplyToEmailAddress))
                        {
                            request.AddParameter("h:Reply-To", "" + ReplytoEmailName + " <" + ReplyToEmailAddress + ">");
                        }
                        request.AddParameter("to", "" + x.Name + " <" + x.EmailAddress + ">");
                        request.AddParameter("subject", MailSubject);
                        request.AddParameter("html", MailHtmlBody);
                        request.AddParameter("text", MailTextBody);
                        request.Method = Method.POST;
                        response       = client.Execute(request);
                    } catch (Exception ex) {
                        outcome.Errors.Add("Error with " + x.Name + " " + ex.ToString());
                    }
                }
                else
                {
                    outcome.Errors.Add("No Email address for " + x.Name);
                }
            }

            if (outcome.Errors.Count > 0 && outcome.Success)
            {
                outcome.Success = false;
            }
            return(outcome);
        }
示例#2
0
        public EMLReturnClass Send(List <EMLMailTarget> targets, string m_MailSubject, string m_MailHtmlBody, string m_MailTextBody, bool substitute)
        {
            MailSubject  = m_MailSubject;
            MailHtmlBody = m_MailHtmlBody;
            MailTextBody = m_MailTextBody;

            EMLReturnClass outcome = new EMLReturnClass();
            AlternateView  av1     = null;
            AlternateView  av2     = null;
            SmtpClient     client  = new SmtpClient();

            client.Port      = port;
            client.Host      = mailhost;
            client.EnableSsl = UseSSL;

            if (accountname.Length > 0)
            {
                client.Credentials = new NetworkCredential(accountname, accountpassword);
            }
            else
            {
                client.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
            }

            foreach (var x in targets)
            {
                if (EMLHelpers.IsValidEmail(x.EmailAddress))
                {
                    try {
                        if (substitute)
                        {
                            MailHtmlBody = EMLHelpers.SubstituteFields(m_MailHtmlBody, x);
                            MailTextBody = EMLHelpers.SubstituteFields(m_MailTextBody, x);
                            MailSubject  = EMLHelpers.SubstituteFields(m_MailSubject, x);
                        }

                        MailMessage mailMessage = new MailMessage(new MailAddress(FromEmailAddress, FromEmailName), new MailAddress(x.EmailAddress, x.Name));

                        mailMessage.Subject = MailSubject;

                        if (EMLHelpers.IsValidEmail(deliverymail))
                        {
                            mailMessage.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess | DeliveryNotificationOptions.OnFailure | DeliveryNotificationOptions.Delay;
                            mailMessage.Headers.Add("Disposition-Notification-To", deliverymail);
                        }
                        if (MailHtmlBody.Length > 0)
                        {
                            mailMessage.IsBodyHtml = true;
                            if (MailTextBody.Length > 0)
                            {
                                av2 = AlternateView.CreateAlternateViewFromString(MailTextBody, null, MediaTypeNames.Text.Plain);
                                mailMessage.AlternateViews.Add(av2);
                                av1 = AlternateView.CreateAlternateViewFromString(MailHtmlBody, Encoding.UTF8, MediaTypeNames.Text.Html);
                                mailMessage.AlternateViews.Add(av1);
                            }
                            else
                            {
                                av1 = AlternateView.CreateAlternateViewFromString(MailHtmlBody, Encoding.UTF8, MediaTypeNames.Text.Html);
                                mailMessage.AlternateViews.Add(av1);
                            }
                        }
                        else
                        {
                            mailMessage.IsBodyHtml = false;
                            av2 = AlternateView.CreateAlternateViewFromString(MailTextBody, null, MediaTypeNames.Text.Plain);
                            mailMessage.AlternateViews.Add(av2);
                        }

                        if (EMLHelpers.IsValidEmail(ReplyToEmailAddress))
                        {
                            mailMessage.Headers.Add("Return-Path", ReplyToEmailAddress);
                            mailMessage.ReplyToList.Add(new MailAddress(ReplyToEmailAddress, ReplytoEmailName));
                        }

                        client.Send(mailMessage);
                    } catch (Exception ex) {
                        outcome.Errors.Add("Sending Error with " + x.Name + " " + ex.ToString());
                    }
                }
                else
                {
                    outcome.Errors.Add("No Email address for " + x.Name);
                }
            }

            return(outcome);
        }
示例#3
0
        public EMLReturnClass Send(List <EMLMailTarget> targets, string m_MailSubject, string m_MailHtmlBody, string m_MailTextBody, bool substitute)
        {
            EMLReturnClass outcome  = new EMLReturnClass();
            EMLReturnClass outcome2 = new EMLReturnClass();

            MailSubject  = m_MailSubject;
            MailHtmlBody = m_MailHtmlBody;
            MailTextBody = m_MailTextBody;

            service = new ExchangeService(exVer, TimeZoneInfo.Local);

            try {
                ServicePointManager.ServerCertificateValidationCallback = delegate(Object oobj, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) { return(true); };
                service.Url = new Uri(MailserverUrl);
            } catch (Exception ex) {
                outcome.SetFailureMessage("Problem contacting exchange mail host", ex.ToString());
            }
            if (outcome.Success)
            {
                try {
                    if (MailserverDomain.Length > 0)
                    {
                        cred = new WebCredentials(MailserverUsername, MailserverPassword, MailserverDomain);
                    }
                    else
                    {
                        cred = new WebCredentials(MailserverUsername, MailserverPassword);
                    }
                    service.Credentials = cred;
                } catch (Exception ex) {
                    outcome.SetFailureMessage("Problem contacting exchange mail host", ex.ToString());
                }
            }
            if (outcome.Success)
            {
                foreach (var x in targets)
                {
                    if (EMLHelpers.IsValidEmail(x.EmailAddress))
                    {
                        try {
                            if (substitute)
                            {
                                MailHtmlBody = EMLHelpers.SubstituteFields(m_MailHtmlBody, x);
                                //MailTextBody = EMLHelpers.SubstituteFields(m_MailTextBody, x);
                                MailSubject = EMLHelpers.SubstituteFields(m_MailSubject, x);
                            }
                            message         = new EmailMessage(service);
                            message.Body    = new MessageBody(BodyType.HTML, MailHtmlBody);
                            message.Subject = MailSubject;
                            message.ToRecipients.Add(new EmailAddress(x.Name, x.EmailAddress));
                            message.From = new EmailAddress(FromEmailName, FromEmailAddress);

                            if (EMLHelpers.IsValidEmail(ReplyToEmailAddress))
                            {
                                //message.ReplyTo.Add(new MailAddress(ReplyToEmailAddress, ReplytoEmailName));
                                message.ReplyTo.Add(new EmailAddress(ReplytoEmailName, ReplyToEmailAddress));
                            }

                            message.SendAndSaveCopy();
                        } catch (Exception ex) {
                            outcome.Errors.Add("Sending Error with " + x.Name + " " + ex.ToString());
                        }
                    }
                    else
                    {
                        outcome.Errors.Add("No Email address for " + x.Name);
                    }
                }
            }

            if (outcome.Errors.Count > 0 && outcome.Success)
            {
                outcome.Success = false;
            }
            return(outcome);
        }