/// <summary> /// /// </summary> /// <param name="details"></param> /// <param name="title"></param> /// <param name="content1"></param> /// <param name="content2"></param> /// <param name="buttonText"></param> /// <param name="buttonUrl"></param> /// <returns></returns> public async Task <SendEmailResponse> SendGeneralEmailAsync(SendEmailDetails details, string title, string content1, string content2, string buttonText, string buttonUrl) { string templateText; // Read the general template from file // TODO: Replace with IoC Flat data provider using (var reader = new StreamReader(Assembly.GetEntryAssembly().GetManifestResourceStream("Remax.Web.Server.Email.Templates.GeneralTemplate.htm"), Encoding.UTF8)) { // Read file contents templateText = await reader.ReadToEndAsync(); } // Replace special values with those inside the template templateText = templateText.Replace("--Title--", title) .Replace("--Content1--", content1) .Replace("--Content2--", content2) .Replace("--ButtonText--", buttonText) .Replace("--ButtonUrl--", buttonUrl); // Set the details content to this template content details.Content = templateText; // Send email return(await EmailServicesDependecyInjection.EmailSender.SendEmailAsync(details)); }
/// <summary> /// /// </summary> /// <param name="details"></param> /// <returns></returns> public async Task <SendEmailResponse> SendEmailAsync(SendEmailDetails details) { // Get the SendGrid key var apiKey = Framework.Construction.Configuration["SendGridKey"]; // Create a new SendGrid client var client = new SendGridClient(apiKey); // From var from = new EmailAddress(details.FromEmail, details.FromName); // To var to = new EmailAddress(details.ToEmail, details.ToName); // Subject var subject = details.Subject; // Content var content = details.Content; // Create Email class ready to send var msg = MailHelper.CreateSingleEmail( from, to, subject, // Plain content details.IsHTML ? null : details.Content, // HTML content details.IsHTML ? details.Content : null); // Finally, send the email... var response = await client.SendEmailAsync(msg); // If we succeeded... if (response.StatusCode == System.Net.HttpStatusCode.Accepted) { // Return successful response return(new SendEmailResponse()); } // Otherwise, it failed... try { // Get the result in the body var bodyResult = await response.Body.ReadAsStringAsync(); // Deserialize the response var sendGridResponse = JsonConvert.DeserializeObject <SendGridResponse>(bodyResult); // Add any errors to the response var errorResponse = new SendEmailResponse { Errors = sendGridResponse?.Errors.Select(f => f.Message).ToList() }; // Make sure we have at least one error if (errorResponse.Errors == null || errorResponse.Errors.Count == 0) { // Add an unknown error // TODO: Localization errorResponse.Errors = new List <string>(new[] { "Unknown error from email sending service. Please contact Yhotel support." }); } // Return the response return(errorResponse); } catch (Exception ex) { // TODO: Localization // Break if we are debugging if (Debugger.IsAttached) { var error = ex; Debugger.Break(); } // If something unexpected happened, return message return(new SendEmailResponse { Errors = new List <string>(new[] { "Unknown error occurred" }) }); } }