public async Task SendAsync(IdentityMessage message) { var content = new SendGrid.Helpers.Mail.Content(); content.Type = "text/html"; content.Value = message.Body; var msg = new SendGrid.Helpers.Mail.Mail( new SendGrid.Helpers.Mail.Email("*****@*****.**", CONSTANTS.SYSTEM_USER_NAME), message.Subject, new SendGrid.Helpers.Mail.Email(message.Destination), content ); var trackingSettings = new SendGrid.Helpers.Mail.TrackingSettings(); trackingSettings.ClickTracking = new SendGrid.Helpers.Mail.ClickTracking(); trackingSettings.OpenTracking = new SendGrid.Helpers.Mail.OpenTracking(); trackingSettings.ClickTracking.Enable = false; trackingSettings.OpenTracking.Enable = false; msg.TrackingSettings = trackingSettings; dynamic sendGridClient = new SendGridAPIClient(Settings.EmailServiceKey); var response = await sendGridClient.client.mail.send.post(requestBody : msg.Get()); }
public async Task <ActionResult> Register(RegisterModel model) { if (ModelState.IsValid) { using (IdentityModels entities = new IdentityModels()) { var userStore = new UserStore <User>(entities); var manager = new UserManager <User>(userStore); manager.UserTokenProvider = new EmailTokenProvider <User>(); var user = new User() { UserName = model.EmailAddress, Email = model.EmailAddress }; IdentityResult result = manager.Create(user, model.Password); if (result.Succeeded) { User u = manager.FindByName(model.EmailAddress); // Creates customer record in Braintree string merchantId = ConfigurationManager.AppSettings["Braintree.MerchantID"]; string publicKey = ConfigurationManager.AppSettings["Braintree.PublicKey"]; string privateKey = ConfigurationManager.AppSettings["Braintree.PrivateKey"]; string environment = ConfigurationManager.AppSettings["Braintree.Environment"]; Braintree.BraintreeGateway braintree = new Braintree.BraintreeGateway(environment, merchantId, publicKey, privateKey); Braintree.CustomerRequest customer = new Braintree.CustomerRequest(); customer.CustomerId = u.Id; customer.Email = u.Email; var r = await braintree.Customer.CreateAsync(customer); string confirmationToken = manager.GenerateEmailConfirmationToken(u.Id); string sendGridApiKey = ConfigurationManager.AppSettings["SendGrid.ApiKey"]; SendGrid.SendGridClient client = new SendGrid.SendGridClient(sendGridApiKey); SendGrid.Helpers.Mail.SendGridMessage message = new SendGrid.Helpers.Mail.SendGridMessage(); message.Subject = string.Format("Please confirm your account"); message.From = new SendGrid.Helpers.Mail.EmailAddress("*****@*****.**", "Will Mabrey"); message.AddTo(new SendGrid.Helpers.Mail.EmailAddress(model.EmailAddress)); SendGrid.Helpers.Mail.Content contents = new SendGrid.Helpers.Mail.Content("text/html", string.Format("<a href=\"{0}\">Confirm Account</a>", Request.Url.GetLeftPart(UriPartial.Authority) + "/MyAccount/Confirm/" + confirmationToken + "?email=" + model.EmailAddress)); message.AddContent(contents.Type, contents.Value); SendGrid.Response response = await client.SendEmailAsync(message); return(RedirectToAction("ConfirmSent")); } else { ModelState.AddModelError("EmailAddress", "Unable to register with this email address."); } } } return(View(model)); }
public ActionResult Success(CheckOut checkout, int id) { //initialize variables for email string userName = ""; string userEmail = ""; string gameName = ""; string gameDate = ""; string players = ""; string numTix = ""; //pull from database using (EscapeRoomDBEntities entities = new EscapeRoomDBEntities()) { //get players and add info from form List <int?> list = entities.sp_basketPlayers(id).ToList(); int i = 0; foreach (int?item in list) { var player = entities.Players.Single(x => x.Id == item); player.FirstName = checkout.Players[i].FirstName; player.LastName = checkout.Players[i].LastName; player.Email = checkout.Players[i].Email; players += player.FirstName + " " + player.LastName + ", "; entities.SaveChanges(); i++; } //get basket and info for email info Basket basket = entities.Baskets.Single(x => x.ID == id); User user = entities.Users.Single(x => x.Id == basket.UserID); userName = user.FirstName; userEmail = user.Email; numTix = (i).ToString(); gameName = basket.Session.Title.ToString(); gameDate = basket.Session.Start.ToString(); players = players.Substring(0, players.Length - 2); } string apiKey = ConfigurationManager.AppSettings["SendGrid.Key"]; SendGrid.SendGridAPIClient client = new SendGrid.SendGridAPIClient(apiKey); SendGrid.Helpers.Mail.Email from = new SendGrid.Helpers.Mail.Email("*****@*****.**"); string subject = "Your Super Fake Chicago Unlocked Tickets"; SendGrid.Helpers.Mail.Email to = new SendGrid.Helpers.Mail.Email(userEmail); string emailContent = string.Format("<html><body><p>{0}!</p><p>Thank you for purchasing {1} fake tickets to {2} on {3}. If this was not totally fake, we would also reach out to these players: {4}. This website is a work in progress for the purpose of experimenting with .Net technologies. Please let me know any feedback you have!</p> <a href='mailto:[email protected]? subject = ChicagoUnlocked'>Email Some Feedback</a></body></html>", userName, numTix, gameName, gameDate, players); SendGrid.Helpers.Mail.Content content = new SendGrid.Helpers.Mail.Content("Text/html", emailContent); SendGrid.Helpers.Mail.Mail mail = new SendGrid.Helpers.Mail.Mail(from, subject, to, content); client.client.mail.send.post(requestBody: mail.Get()); return(RedirectToAction("About", "Home")); }
public async Task <ActionResult> ForgotPassword(ForgotPasswordViewModel model) { if (ModelState.IsValid) { using (IdentityModels entities = new IdentityModels()) { var userStore = new UserStore <User>(entities); var manager = new UserManager <User>(userStore); manager.UserTokenProvider = new EmailTokenProvider <User>(); var user = manager.FindByName(model.Email); // If user has to activate his email to confirm his account, the use code listing below //if (user == null || !(await UserManager.IsEmailConfirmedAsync(user.Id))) //{ // return Ok(); //} string code = await manager.GeneratePasswordResetTokenAsync(user.Id); string sendGridKey = System.Configuration.ConfigurationManager.AppSettings["SendGrid.ApiKey"]; SendGrid.SendGridClient client = new SendGrid.SendGridClient(sendGridKey); SendGrid.Helpers.Mail.SendGridMessage message = new SendGrid.Helpers.Mail.SendGridMessage(); message.Subject = string.Format("Reset Password"); message.From = new SendGrid.Helpers.Mail.EmailAddress("*****@*****.**", "Will Mabrey"); message.AddTo(new SendGrid.Helpers.Mail.EmailAddress(model.Email)); SendGrid.Helpers.Mail.Content contents = new SendGrid.Helpers.Mail.Content("text/html", string.Format("<a href=\"{0}\">Reset Password</a>", Request.Url.GetLeftPart(UriPartial.Authority) + "/MyAccount/ResetPassword/" + code + "?EmailAddress=" + model.Email)); message.AddContent(contents.Type, contents.Value); SendGrid.Response response = await client.SendEmailAsync(message); //await client.SendEmailAsync(user.Id, "Reset Password", $"Please reset your password by using this {code}"); return(RedirectToAction("ResetSent")); } } return(View()); // If we got this far, something failed, redisplay form }
public ActionResult Register(RegistrationModel model) { if (ModelState.IsValid) { if (WebMatrix.WebData.WebSecurity.UserExists(model.Email)) { ModelState.AddModelError("Email", "Email address is already registered"); } else { string token = WebMatrix.WebData.WebSecurity.CreateUserAndAccount(model.Email, model.Password, new { DateCreated = DateTime.UtcNow, FirstName = model.FirstName, LastName = model.LastName, Phone = model.Phone, Email = model.Email }, true); string apiKey = ConfigurationManager.AppSettings["SendGrid.Key"]; SendGrid.SendGridAPIClient client = new SendGrid.SendGridAPIClient(apiKey); SendGrid.Helpers.Mail.Email from = new SendGrid.Helpers.Mail.Email("*****@*****.**"); string subject = "Complete your EscapeRoom Registration"; SendGrid.Helpers.Mail.Email to = new SendGrid.Helpers.Mail.Email(model.Email); string emailContent = string.Format("<html><body><a href=\"{0}\">Complete your registration</a></body></html>", Request.Url.GetLeftPart(UriPartial.Authority) + "/Account/REgisterConfirm/" + HttpUtility.UrlEncode(token) + "?email=" + HttpUtility.UrlEncode(model.Email)); SendGrid.Helpers.Mail.Content content = new SendGrid.Helpers.Mail.Content("Text/html", emailContent); SendGrid.Helpers.Mail.Mail mail = new SendGrid.Helpers.Mail.Mail(from, subject, to, content); client.client.mail.send.post(requestBody: mail.Get()); return(RedirectToAction("RegisterComplete")); } return(View(model)); } return(View(model)); }
public async Task <ActionResult> Index(CheckoutModel model) { if (ModelState.IsValid) { using (AppStoreEntities entities = new AppStoreEntities()) { Order o = null; if (User.Identity.IsAuthenticated) { AspNetUser currentUser = entities.AspNetUsers.Single(x => x.UserName == User.Identity.Name); o = currentUser.Orders.FirstOrDefault(x => x.TimeCompleted == null); if (o == null) { o = new Order(); o.OrderNumber = Guid.NewGuid(); currentUser.Orders.Add(o); entities.SaveChanges(); } } else { if (Request.Cookies.AllKeys.Contains("orderNumber")) { Guid orderNumber = Guid.Parse(Request.Cookies["orderNumber"].Value); o = entities.Orders.FirstOrDefault(x => x.TimeCompleted == null && x.OrderNumber == orderNumber); } if (o == null) { o = new Order(); o.OrderNumber = Guid.NewGuid(); entities.Orders.Add(o); Response.Cookies.Add(new HttpCookie("orderNumber", o.OrderNumber.ToString())); entities.SaveChanges(); } } if (o.OrdersProducts.Sum(x => x.Quantity) == 0) { return(RedirectToAction("Index", "Cart")); } o.BuyerEmail = User.Identity.Name; Address newShippingAddress = new Address(); newShippingAddress.Address1 = model.ShippingAddress1; newShippingAddress.Address2 = model.ShippingAddress2; newShippingAddress.City = model.ShippingCity; newShippingAddress.State = model.ShippingState; newShippingAddress.Zip = model.ZipCode; newShippingAddress.Country = model.ShippingCountry; o.Address1 = newShippingAddress; WhereTo = ("\n Your Order will be shipped to the following address: \n" + model.ShippingAddress1 + "\n " + model.ShippingAddress2 + "\n " + model.ShippingCity + "\n " + model.ShippingState + "\n " + model.ZipCode); entities.sp_CompleteOrder(o.ID); string merchantId = ConfigurationManager.AppSettings["Braintree.MerchantID"]; string publicKey = ConfigurationManager.AppSettings["Braintree.PublicKey"]; string privateKey = ConfigurationManager.AppSettings["Braintree.PrivateKey"]; string environment = ConfigurationManager.AppSettings["Braintree.Environment"]; Braintree.BraintreeGateway braintree = new Braintree.BraintreeGateway(environment, merchantId, publicKey, privateKey); Braintree.TransactionRequest newTransaction = new Braintree.TransactionRequest(); newTransaction.Amount = o.OrdersProducts.Sum(x => x.Quantity * x.Product.Price) ?? 0.01m; Braintree.TransactionCreditCardRequest creditCard = new Braintree.TransactionCreditCardRequest(); creditCard.CardholderName = model.CreditCardName; creditCard.CVV = model.CreditCardVerificationValue; creditCard.ExpirationMonth = model.CreditCardExpiration.Value.Month.ToString().PadLeft(2, '0'); creditCard.ExpirationYear = model.CreditCardExpiration.Value.Year.ToString(); creditCard.Number = model.CreditCardNumber; newTransaction.CreditCard = creditCard; // If the user is logged in, associate this transaction with their account if (User.Identity.IsAuthenticated) { Braintree.CustomerSearchRequest search = new Braintree.CustomerSearchRequest(); search.Email.Is(User.Identity.Name); var customers = braintree.Customer.Search(search); newTransaction.CustomerId = customers.FirstItem.Id; } Braintree.Result <Braintree.Transaction> result = await braintree.Transaction.SaleAsync(newTransaction); if (!result.IsSuccess()) { ModelState.AddModelError("CreditCard", "Could not authorize payment"); return(View(model)); } string sendGridApiKey = ConfigurationManager.AppSettings["SendGrid.ApiKey"]; SendGrid.SendGridClient client = new SendGrid.SendGridClient(sendGridApiKey); SendGrid.Helpers.Mail.SendGridMessage message = new SendGrid.Helpers.Mail.SendGridMessage(); //TODO: Go into SendGrid and set up a template and insert the if below //message.SetTemplateId("524c7845-3ed9-4d53-81c8-b467443f8c5c"); message.Subject = string.Format("Receipt for order {0}", o.ID); message.From = new SendGrid.Helpers.Mail.EmailAddress("*****@*****.**", "Will Mabrey"); message.AddTo(new SendGrid.Helpers.Mail.EmailAddress(o.BuyerEmail)); string prodcuctsReceipt = "You've Ordered: "; WhatWasOrdered = prodcuctsReceipt; foreach (var item in o.OrdersProducts) { string addition = string.Format("\n " + "{0} copies of {1}", item.Quantity, item.Product.Name); prodcuctsReceipt += addition; } SendGrid.Helpers.Mail.Content contents = new SendGrid.Helpers.Mail.Content("text/plain", string.Format("Thank you for ordering through Ye Olde App Store \n {0} {1}", prodcuctsReceipt, WhereTo)); message.AddSubstitution("%ordernum%", o.ID.ToString()); message.AddContent(contents.Type, contents.Value); SendGrid.Response response = await client.SendEmailAsync(message); o.TimeCompleted = DateTime.UtcNow; entities.SaveChanges(); } return(RedirectToAction("profile", "Home")); } return(View(model)); }