コード例 #1
0
        public async Task <IActionResult> OnPostAsync()
        {
            // Check if the reCaptcha is valid.
            if (!await _reCaptchaChecker.IsValid(Input.ReCaptchaToken))
            {
                // Add an error to the model.
                ModelState.AddModelError(string.Empty, "The reCaptcha verification failed.");
                // Return the page.
                return(Page());
            }
            // Check if the provided model is not valid.
            if (!ModelState.IsValid)
            {
                // Add an error to the model.
                ModelState.AddModelError(string.Empty, "An error was encountered. Please check again the input fields.");
                // Return the page.
                return(Page());
            }
            // Define a new view model for the e-mail.
            var emailViewModel = new EmailContactViewModel
            {
                Name           = Input.Name,
                Email          = Input.Email,
                Message        = Input.Message,
                ApplicationUrl = _linkGenerator.GetUriByPage(HttpContext, "/Index", handler: null, values: null)
            };
            // Send an e-mail to the predefined administrator address.
            await _emailSender.SendContactEmailAsync(emailViewModel);

            // Display a message to the user.
            TempData["StatusMessage"] = "Success: The message was sent successfully.";
            // Redirect to page.
            return(RedirectToPage());
        }
コード例 #2
0
        /// <summary>
        /// Sends a contact e-mail to the administrator users specified in the configuration file.
        /// </summary>
        /// <param name="viewModel">Represents the view model of the e-mail.</param>
        public async Task SendContactEmailAsync(EmailContactViewModel viewModel)
        {
            // Define the variables for the e-mail.
            var apiKey      = _configuration.GetSection("Authentication:SendGrid:AppKey").Value;
            var client      = new SendGridClient(apiKey);
            var from        = new EmailAddress(_configuration.GetSection("EmailSender:Email").Value, _configuration.GetSection("EmailSender:Name").Value);
            var tos         = _configuration.GetSection("Administrators").GetChildren().Select(item => new EmailAddress(item.GetSection("Email").Value, item.GetSection("Email").Value));
            var subject     = "Message from NetControl4BioMed";
            var htmlContent = await _renderer.RenderPartialToStringAsync("_EmailContactPartial", viewModel);

            var msg = MailHelper.CreateSingleEmailToMultipleRecipients(from, tos.ToList(), subject, string.Empty, htmlContent);
            // Send the e-mail containing the message.
            await client.SendEmailAsync(msg);
        }