/// <summary>
            /// Determines if an email has valid syntax
            /// </summary>
            /// <param name="email">the email to test</param>
            /// <param name="TLDrequired">indicates whether or not the
            /// email must end with a known TLD to be considered valid</param>
            /// <returns>boolean indicating if the email has valid syntax</returns>
            /// <remarks>
            /// Validates an email address specifying whether or not
            /// the email is required to have a TLD that is valid.
            /// </remarks>
            public static bool Valid(string email, bool TLDrequired)
            {
                EmailSyntaxValidator v;
                bool valid;

                //call syntax validator
                v = new EmailSyntaxValidator(email, TLDrequired);

                //determine validity
                valid = v.IsValid;

                //cleanup
                v = null;

                //return indication of validity
                return(valid);
            }
Beispiel #2
0
        protected SubscriptionResult AddSubscriptionToList(string email, RecipientList selectedList)
        {
            EmailSyntaxValidator validator = new EmailSyntaxValidator(email, false);

            if (validator.IsValid)
            {
                _log.Debug("Attemt to add email subscription for {0}", email);


                EmailAddress emailAddress = selectedList.CreateEmailAddress(email);
                emailAddress.Source = EmailAddressSource.SelfRegistered;
                emailAddress.Added  = DateTime.Now;
                emailAddress.Save(); // Will add it to the list, or update it if it exists

                return(SubscriptionResult.Success);
            }
            else
            {
                _log.Warning("Failed to add email subscription for '{0}' (not valid)", email);
                return(SubscriptionResult.EmailNotValid);
            }
        }
        /// <summary>
        /// Enables processing of HTTP Web requests by a custom HttpHandler that implements the <see cref="T:System.Web.IHttpHandler"/> interface.
        /// </summary>
        /// <param name="context">An <see cref="T:System.Web.HttpContext"/> object that provides references to the intrinsic server objects (for example, Request, Response, Session, and Server) used to service HTTP requests.</param>
        public void ProcessRequest(HttpContext context)
        {
            error_p        = false;
            body           = "";
            redirect       = "";
            requiredFields = "";
            splitFields    = "";
            foreach (string key in context.Request.Form.AllKeys)
            {
                switch (key)
                {
                case "submit.x": break;     // ignore

                case "submit.y": break;     // ignore

                case "__from":
                    from = context.Request.Params[key];
                    if (!EmailSyntaxValidator.Valid(from, true))
                    {
                        errorList.Add("Error: from email '" + from + "' is invalid. Please go back and enter a valid email address.");
                        error_p = true;
                    }
                    break;

                case "__subject": subject = context.Request.Params[key]; break;

                case "__recipient":
                    to = ConfigurationManager.AppSettings[context.Request.Params[key]];
                    if ((to == null) || (to == ""))
                    {
                        errorList.Add("Error: recipient '" + context.Request.Params[key] + "' is not configured.");
                        error_p = true;
                    }
                    break;

                case "__redirectto": redirect = context.Request.Params[key]; break;

                case "__requiredfields": requiredFields = context.Request.Params[key].Trim().Replace(" ", ""); break;

                case "__splitFields": splitFields = "," + context.Request.Params[key].Replace(" ", "") + ","; break;

                // Recaptcha fields
                case "g-recaptcha-response": recaptchaResponseField = context.Request.Form[key]; break;

                default:
                    if (key.StartsWith("__linebreak"))
                    {
                        body += "\n";
                    }
                    else
                    {
                        if (!key.StartsWith("__"))
                        {     // only send "real" fields
                            if (splitFields.IndexOf(key) == -1)
                            {
                                body += key.Replace("_", " ") + ": " + context.Request.Params[key] + "\n";
                            }
                            else
                            {
                                // split the field into multiple lines
                                body += key.Replace("_", " ") + ": \n\t\t" + context.Server.UrlDecode(context.Request.Params[key].Replace(",", "\n\t\t")) + "\n";
                            }
                        }
                    }
                    break;
                }
            }

            if (requiredFields != "")
            {
                foreach (string field in requiredFields.Split(','))
                {
                    if ((context.Request.Params[field] == null) || (context.Request.Params[field].Trim() == ""))
                    {
                        errorList.Add("Required field missing: " + field);
                        error_p = true;
                    }
                }
            }

            // Verify captcha was submitted, and is valid
            ReCaptchaValidator captcha = ValidateCaptcha(recaptchaResponseField, context.Request.UserHostAddress);

            if (!captcha.Success)
            {
                if (captcha.ErrorCodes == null)
                {
                    errorList.Add("reCAPTCHA check not completed!");
                }
                else
                {
                    errorList.AddRange(captcha.ErrorCodes);
                }
                error_p = true;
            }

            if (!error_p)
            {
                try
                {
                    SendEmail();
                }
                catch (Exception exception)
                {
                    errorList.Add("Error: " + exception.Message);
                    error_p = true;
                }

                if (!error_p && redirect != null)
                {
                    context.Response.Redirect(redirect);
                }
            }

            if (error_p)
            {
                Content = string.Empty;
                errorList.ForEach(entry => {
                    Content += HtmlEncoder.HtmlEncode(entry) + "<br>\n";
                });

                context.Response.Write(Content);
            }
        }