public static void SendEmail(string subject, string body, string from,
                                     string[] to, string[] cc, string[] bcc, int retryMax = 3)
        {
            // sanitize the subject (pretties, but mainly gets rid of cr & lf)
            subject  = subject.StripRedundantWhiteSpace();
            retryMax = Math.Max(0, retryMax);
            var mailMessage = new MailMessage(from, string.Join(",", to), subject, body);

            if ((cc != null) && (cc.Length > 0))
            {
                mailMessage.CC.Add(string.Join(",", cc));
            }
            if ((bcc != null) && (bcc.Length > 0))
            {
                mailMessage.Bcc.Add(string.Join(",", bcc));
            }
            mailMessage.IsBodyHtml = true;

#if !NoEmail
            while (retryMax >= 0)
            {
                try
                {
                    //var smtpClient = new SmtpClient("localhost");
                    //smtpClient.Send(mailMessage);
                    EmailUtility.GetConfiguredSmtpClient().Send(mailMessage);
                    retryMax = -1; // kill the loop
                }
                catch (Exception)
                {
                    if (retryMax-- <= 0)
                    {
                        throw;
                    }
                    Thread.Sleep(1000); // let things settle
                }
            }
#endif
        }
Esempio n. 2
0
        protected void Page_Load(object sender, EventArgs e)
        {
            Page.IncludeJs("http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js");
            Page.IncludeJs(
                "http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js");
            Page.IncludeJs("~/js/jq/jquery.simpleCombo.js");
            Page.IncludeJs("~/js/EmailForm.js");
            Page.IncludeCss("~/js/jq/jquery-ui.css");
            Page.IncludeCss("~/css/EmailForm.css");

            EmailFormErrorLabel.Visible = false;
            EmailFormGoodLabel.Visible  = false;

            // setup client-side input processing
            EmailFormCaptcha.UserInputClientID = EmailFormCaptchaCodeTextBox.ClientID;

            if (!IsPostBack)
            {
                AddItems("Select a subject or type your own", "");
            }

// ReSharper disable InvertIf
            if (IsPostBack)
// ReSharper restore InvertIf
            {
                var subject = Request.Form["EmailForm$EmailFormSubject"];
                AddItems(subject, subject);
                // validate the Captcha to check we're not dealing with a bot
                if (IsHuman)
                {
                    try
                    {
                        if (!Validation.IsValidEmailAddress(EmailFormFromEmailAddress.Text))
                        {
                            throw new VoteException("The email address you entered is not valid");
                        }
                        if (string.IsNullOrWhiteSpace(subject))
                        {
                            throw new VoteException(
                                      "Please enter or select a subject for the email");
                        }
                        if (string.IsNullOrWhiteSpace(EmailFormMessage.Text))
                        {
                            throw new VoteException("The email message body is missing");
                        }
                        var email = new MailMessage
                        {
                            IsBodyHtml = false,
                            From       = new MailAddress(EmailFormFromEmailAddress.Text)
                        };
                        email.To.Add(ToEmailAddress);
                        email.Subject = subject;
                        email.Body    = EmailFormMessage.Text;

 #if !NoEmail
                        //var smtpClient = new SmtpClient("localhost");
                        //smtpClient.Send(email);
                        EmailUtility.GetConfiguredSmtpClient().Send(email);
#endif
                        EmailFormGoodLabel.Text    = "Email has been sent";
                        EmailFormGoodLabel.Visible = true;
                    }
                    catch (Exception ex)
                    {
                        EmailFormErrorLabel.Text    = ex.Message;
                        EmailFormErrorLabel.Visible = true;
                    }
                }
                else
                {
                    EmailFormErrorLabel.Text    = "The typed characters did not match the picture";
                    EmailFormErrorLabel.Visible = true;
                }
                EmailFormCaptchaCodeTextBox.Text = null; // clear previous user input
            }
        }
        protected void SubmitForm(object sender, EventArgs e)
        {
            //var subject = Request.Form["ctl00$MasterMainContent$EmailForm$EmailFormSubject"];
            var subject = EmailFormSubject.Value;

            if (subject == "Other")
            {
                subject = EmailFormOtherSubject.Text;
            }
            // validate the Captcha to check we're not dealing with a bot
            if (IsHuman)
            {
                try
                {
                    if (!Validation.IsValidEmailAddress(EmailFormFromEmailAddress.Text))
                    {
                        throw new VoteException("The email address you entered is not valid");
                    }
                    if (string.IsNullOrWhiteSpace(subject))
                    {
                        throw new VoteException(
                                  "Please select a subject for the email");
                    }
                    if (!MessageOptional && string.IsNullOrWhiteSpace(EmailFormMessage.Text))
                    {
                        throw new VoteException("The email message body is missing");
                    }

                    OptionalItem.ValidateAll(_OptionalItems);

                    if (Callback != null)
                    {
                        // create dictionary for callback
                        var dict = new Dictionary <string, string>
                        {
                            // add the fixed items
                            { "EmailFormSubject", subject },
                            { "EmailFormFromEmailAddress", EmailFormFromEmailAddress.Text },
                            { "EmailFormMessage", EmailFormMessage.Text }
                        };

                        // add any optional items
                        OptionalItem.AddAllToDictionary(_OptionalItems, dict);

                        Callback(dict);
                    }

                    var email = new MailMessage
                    {
                        IsBodyHtml = false,
                        //From = new MailAddress(EmailFormFromEmailAddress.Text)
                        From = new MailAddress("*****@*****.**")
                    };
                    email.To.Add(ToEmailAddress);
                    if (!string.IsNullOrWhiteSpace(CcEmailAddress))
                    {
                        email.CC.Add(CcEmailAddress);
                    }
                    email.Subject = subject;
                    email.Body    = "Email: " + EmailFormFromEmailAddress.Text + "\n\n" +
                                    OptionalItem.FormatAll(_OptionalItems) + EmailFormMessage.Text;

#if !NoEmail
                    EmailUtility.GetConfiguredSmtpClient().Send(email);
#endif
                    EmailFormGoodLabel.Text    = "Email has been sent";
                    EmailFormGoodLabel.Visible = true;

                    EmailFormSubject.Value         = string.Empty;
                    EmailFormOtherSubject.Text     = string.Empty;
                    EmailFormFromEmailAddress.Text = string.Empty;
                    EmailFormMessage.Text          = string.Empty;
                    OptionalItem.ClearAll(_OptionalItems);
                }
                catch (Exception ex)
                {
                    EmailFormErrorLabel.Text    = ex.Message;
                    EmailFormErrorLabel.Visible = true;
                }
            }
            else
            {
                EmailFormErrorLabel.Text    = "The typed characters did not match the picture";
                EmailFormErrorLabel.Visible = true;
            }
            EmailFormCaptchaCodeTextBox.Text = null; // clear previous user input
        }