Example #1
0
        private async Task SendConfirmationEmail(string toEmail, string templateId, Personalization <PasswordResetTemplateData> personalized)
        {
            //Start SendGridClient
            var client = new SendGridClient(_sendgridConfigs.Value.API_Key);

            //Configure email
            var email = new SendGridEmailTemplate <PasswordResetTemplateData>(templateId);

            //Add personalization to email!
            email.AddPersonalization(personalized);

            //Email from parameters
            email.from = new From
            {
                email = _emailAuthorityConfigs.Value.Email,
                name  = _emailAuthorityConfigs.Value.Name
            };

            var json = JsonConvert.SerializeObject(email, Formatting.None);

            var response = await client.SendTransactionalEmail(json);

            if (!response.IsSuccessStatusCode)
            {
                if (response.Content != null)
                {
                    var errorMsg = await response.Content.ReadAsStringAsync();

                    Log.Error("Failed to send transactional email with password reset for {ToEmail}, with {TemplateId} and {@Email}. Failed with {ResponseMSG}", toEmail, templateId, email, errorMsg);
                    Console.WriteLine(errorMsg);
                }
                Log.Error("Failed to send transactional email with password reset for {ToEmail}, with {TemplateId} and {@Template}. Failed with {ResponseMSG}", toEmail, templateId, email, response.ReasonPhrase);
            }
        }