public async Task <ActionResult> SendBulkVerificationEmail() { var users = from u in db.Tokens where u.User.Verified == false select new SendBulkTokenEmailModel { TitleID = u.User.TitleID, Intials = u.User.Intials, Surname = u.User.Surname, UserName = u.User.UserName, TokenID = u.Id, Email = u.User.Email, IssueDate = u.IssueDate }; foreach (var user in users) { string clientName = user.TitleID + " " + user.Intials + " " + user.Surname; bool success = NotificationsHelper.SendEmailWithVerificationToken(user.Email, clientName, user.UserName, ControllerContext); if (!success) { Trace.WriteLine("Failed to send verification email to: " + user.Email + " during bulk email sending", "Verification Bulk Emails"); } } ViewBag.Title = "Bulk Verification Emails"; return(View(await users.ToListAsync())); }
public async Task <ActionResult> Register(RegisterViewModel model) { if (ModelState.IsValid) { #region validate email Institution institution = db.Institutions.Find(model.Institution); Resident resident = db.Residents.Find(1); Faculty faculty = db.Faculties.Find(1); Qualification qualification = db.Qualifications.Find(1); bool emailValid = false; string username = ""; string extension = ""; try { username = model.Email.Substring(0, model.Email.IndexOf('@')).ToLower(); extension = model.Email.Substring(model.Email.IndexOf('@') + 1).ToLower(); // validate the email extension the student entered if (institution.Extension1.Equals(extension) || institution.Extension2.Equals(extension) || institution.Extension3.Equals(extension) || institution.Extension4.Equals(extension) || institution.Extension5.Equals(extension)) { emailValid = true; } else { emailValid = false; ModelState.AddModelError("Email", "The student email address entered is not valid"); } } catch (Exception e) { emailValid = false; Trace.WriteLine(e.Message, "Invalid Email During Registration: " + model.Email); ModelState.AddModelError("Email", "The student email address entered is not valid"); } #endregion #region create user and send verification email if (emailValid) { // set text info to be able to capitalize the product name TextInfo ti = CultureInfo.CurrentCulture.TextInfo; var user = new ApplicationUser() { UserName = username, Email = model.Email, Institution = institution, Faculty = faculty, Qualification = qualification, Resident = resident, Verified = false, RegistrationDate = DateTime.Now, LastSeen = DateTime.Now }; var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { // get user info var unverifiedUser = UserManager.FindByName(user.UserName); var userFullName = user.TitleID + " " + user.Intials + " " + user.Surname; // send verification email bool success = NotificationsHelper.SendEmailWithVerificationToken(user.Email, userFullName, user.UserName, ControllerContext); if (!success) { ModelState.AddModelError("", "An error occured while sending the verifiaction email. We will try to send the verification again soon."); } else { Session.Add("currentInstitution", institution.abbreviation); Session.Add("verifyName", userFullName); return(RedirectToAction("Verification", "Account")); } } else { AddErrors(result); } } #endregion } // recreate form data model.Institutions = GetInstitutions(); // If we got this far, something failed, redisplay form return(View("~/Views/Home/Index.cshtml", model)); }