void Application_Error(object sender, EventArgs e)
        {

            Exception exc = Server.GetLastError();

            if (exc.GetType() == typeof(HttpException))
            {
                if (exc.Message.Contains("NoCatch") || exc.Message.Contains("maxUrlLength"))
                    return;
                Server.Transfer("~/500.html");
            }
            string error = "<h2>Error in Website</h2>\n";
            error += "<p>Error: " + exc.Message + "</p>\n";
            error += "<p>Stack:<br/>"+exc.StackTrace;

            Email Email = new Email();
            Email.SendMail(EmailType.Error, true, error, Email.SITE_ADMIN, "Webmaster");

            Server.ClearError();
            Server.Transfer("~/500.html");
        }
        public async Task<ActionResult> ConfirmEmail(string userId, string code)
        {
            if (userId == null || code == null)
            {
                return View("Error");
            }
            var result = await UserManager.ConfirmEmailAsync(userId, code);

            Email Email = new Email();
            string defaultHtml = Email.HTML_WELCOME;

            string body = defaultHtml
                .Replace("*|EMAIL_PREVIEW|*", "This is a Welcome Email")
                .Replace("*|HEADER|*", Helpers.WEBSITE_NAME)
                .Replace("*|FIRSTPARAGRAPH|*", "Thank you for Registering and Confirming your Account. Share " + Helpers.WEBSITE_NAME + " with your friends!")
                .Replace("*|SECONDPARAGRAPH|*", Email.HTML_SHARE)
                .Replace("*|WEBSITE_URL|*", Helpers.WEBSITE_URL)
                .Replace("*|WEBSITE_NAME|*", Email.WEBSITE_NAME)
                .Replace("*|CURRENT_YEAR|*", DateTime.Now.Year.ToString());

            string email = (from e in UserManager.Users where e.Id == userId select e.Email).Single();


            KeyValuePair<bool, string> response = Email.SendMail(EmailType.Welcome, true, body, email);

            if (response.Key)
                TempData["Success"] = "Welcome Email was sent, thank you for registering";
            else TempData["Error"] = response.Value;

            return View(result.Succeeded ? "ConfirmEmail" : "Error");
        }
        public async Task<ActionResult> ForgotPassword(ForgotPasswordViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = await UserManager.FindByNameAsync(model.Email);
                if (user == null || !(await UserManager.IsEmailConfirmedAsync(user.Id)))
                {
                    // Don't reveal that the user does not exist or is not confirmed
                    return View("ForgotPasswordConfirmation");
                }

                // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
                // Send an email with this link
                string code = await UserManager.GeneratePasswordResetTokenAsync(user.Id);
                var callbackUrl = Url.Action("ResetPassword", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                // await UserManager.SendEmailAsync(user.Id, "Reset Password", "Please reset your password by clicking <a href=\"" + callbackUrl + "\">here</a>");

                Email Email = new Email();
                string defaultHtml = Email.HTML_FORGOTEN;
                string body = defaultHtml
                    .Replace("*|EMAIL_PREVIEW|*", "This is a Password Reset Email")
                    .Replace("*|HEADER|*", Helpers.WEBSITE_NAME)
                    .Replace("*|FIRSTPARAGRAPH|*", "It seems you forgot your password. Click the link below to reset it.")
                    .Replace("*|SECONDPARAGRAPH|*", "<a href =\"" + callbackUrl + "\">Reset Password</a><br/>Or copy the url below into your web browser<br/><span style='color:#ccc'>" + callbackUrl + "</span>")
                    .Replace("*|WEBSITE_URL|*", Helpers.WEBSITE_URL)
                    .Replace("*|WEBSITE_NAME|*", Email.WEBSITE_NAME)
                    .Replace("*|CURRENT_YEAR|*", DateTime.Now.Year.ToString());


                KeyValuePair<bool, string> response = Email.SendMail(EmailType.Forgotten, true, body, user.Email);

                if (response.Key)
                    TempData["Success"] = "Password reset email was sent, check your emails.";
                else TempData["Error"] = response.Value;

                return RedirectToAction("ForgotPasswordConfirmation", "Account");
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }
        public async Task<ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
                var result = await UserManager.CreateAsync(user, model.Password);
                if (result.Succeeded)
                {
                    await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);

                    // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
                    // Send an email with this link
                    string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                    var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                    //await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");
                    Email Email = new Email();
                    string defaultHtml = Email.HTML_CONFIRMATION;
                    string body = defaultHtml
                        .Replace("*|EMAIL_PREVIEW|*", "This is a Registration Email")
                        .Replace("*|HEADER|*", Helpers.WEBSITE_NAME)
                        .Replace("*|FIRSTPARAGRAPH|*", "Complete your registration by clicking the link below")
                        .Replace("*|SECONDPARAGRAPH|*", "<a href =\"" + callbackUrl + "\">Confirm Account</a><br/>Or copy the url into your web browser<br/><span style='color:#ccc'>" + callbackUrl + "</span>")
                        .Replace("*|WEBSITE_URL|*", Helpers.WEBSITE_URL)
                        .Replace("*|WEBSITE_NAME|*", Email.WEBSITE_NAME)
                        .Replace("*|CURRENT_YEAR|*", DateTime.Now.Year.ToString());


                    KeyValuePair<bool, string> response = Email.SendMail(EmailType.Confirmation, true, body, user.Email);

                    if (response.Key)
                        TempData["Success"] = "Confirmation Email was sent, please activate your account";
                    else TempData["Error"] = response.Value;

                    return RedirectToAction("Index", "Home");
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }