Example #1
0
        private async Task <EmailStatus> SendEmail(EmailTemplate template, EmailOutbox email)
        {
            try
            {
                var parameters = JsonConvert.DeserializeObject <object[]>(email.Parameters);
                var body       = string.Format(template.Template ?? "", parameters);
                var bodyHtml   = string.Format(template.TemplateHtml, parameters).Replace("{{", "{").Replace("}}", "}");
                var subject    = string.Format(template.Subject, parameters);
                var message    = MailHelper.CreateSingleEmail(MailHelper.StringToEmailAddress(template.FromAddress), MailHelper.StringToEmailAddress(email.Destination), subject, body, bodyHtml);

                var client   = new SendGridClient(_sendGrid_Api_Key);
                var response = await client.SendEmailAsync(message, CancellationToken);

                if (response.StatusCode == System.Net.HttpStatusCode.Accepted)
                {
                    return(EmailStatus.Processed);
                }

                email.Error = response.StatusCode.ToString();
            }
            catch (Exception ex)
            {
                email.Error = ex.Message;
                Log.Exception("[SendEmail]", ex);
            }
            return(EmailStatus.Failed);
        }
Example #2
0
        public ActionResult AdminResetPassword(string user)
        {
            if (ModelState.IsValid && Roles.IsUserInRole("Administrator"))
            {
                MembershipUser currentUser = System.Web.Security.Membership.GetUser(user);
                string         newpassword = currentUser.ResetPassword();
                //Send email to user with new password
                try
                {
                    EmailOutbox outEmail = db.EmailOutboxes.Where(s => s.purpose == "password reset").FirstOrDefault();

                    string fromAddress = outEmail.emailAddress;   //"*****@*****.**";
                    string fromName    = outEmail.emailName;
                    string password    = outEmail.emailPassword;  //"Parcmen!";
                    string emailBody   = "Your password for the Pocket Job Coach has been reset to the temporary password '" + newpassword + "'. Please login and change your password now at http://pjc.gear.host";
                    string server      = outEmail.smtpServerName; //"smtp.gmail.com";
                    int    port        = outEmail.portNumber;
                    int    timeout     = outEmail.smtpTimeout;
                    Email.send(fromAddress, fromName, currentUser.Email, "Pocket Job Coach Password Reset", emailBody, password, server, port, timeout);
                    Response.Redirect("~/Account/List");
                }
                catch (Exception e)
                {
                    db.Debugs.Add(new Debug()
                    {
                        debugMessage = e.ToString().Substring(0, 199)
                    });
                    db.SaveChanges();

                    Response.Redirect("~/Unauthorized");
                }
                ModelState.AddModelError("", "Password has been reset for " + currentUser.UserName);
            }
            else
            {
                Response.Redirect("~/Unauthorized");
            }
            return(View());
        }
Example #3
0
        public ActionResult AdminResetPassword(string user)
        {
            if (!(ModelState.IsValid && Roles.IsUserInRole("Administrator")))
            {
                Response.Redirect("~/Unauthorized");
                return(View());
            }

            MembershipUser currentUser = System.Web.Security.Membership.GetUser(user);

            if (currentUser.IsLockedOut)
            {
                currentUser.UnlockUser();
            }

            string newpassword = currentUser.ResetPassword();

            //Send email to user with new password
            try
            {
                EmailOutbox outEmail = helper.getEmailOutboxForPurpose("password reset");

                string emailBody = "Your password for the Pocket Job Coach has been reset to the temporary password '" + newpassword + "'. Please login and change your password now at http://pjc.gear.host";
                Email.send(outEmail, currentUser.Email, "Pocket Job Coach Password Reset", emailBody);

                Response.Redirect("~/Account/List");
            }
            catch (Exception e)
            {
                debug.createDebugMessageInDatabase(e.ToString());

                Response.Redirect("~/Unauthorized");
            }
            ModelState.AddModelError("", "Password has been reset for " + currentUser.UserName);

            return(View());
        }