using SendGrid.Helpers.Mail; using SendGrid; using WebApp.Services.Interfaces; using Microsoft.Extensions.Options; using WebApp.Common.Configurations; using Microsoft.Extensions.Logging; using WebApp.Common.Exceptions.ServerSide; namespace WebApp.Services.Implementations; public class EmailSenderService : IEmailSenderService { private readonly EmailSenderSettings _senderSettings; private readonly ILogger _logger; public EmailSenderService(IOptions options, ILogger logger) { _senderSettings = options.Value; _logger = logger; } public async Task SendEmailConfirmationAsync(string email, string confirmationUri) { SendGridClient client = new(_senderSettings.Key); EmailAddress from = new(_senderSettings.Sender, _senderSettings.Name); string subject = "Registration confirmation"; EmailAddress to = new EmailAddress(email, "Example User"); string htmlContent = $"\r\n\r\n \r\n \r\n Email Confirmation\r\n \r\n \r\n\r\n\r\n\r\n
\r\n A preheader is the short summary text that follows the subject line when an email is viewed in the inbox.\r\n
\r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n
\r\n \r\n \r\n \r\n \r\n
\r\n

Confirm Your Email Address

\r\n
\r\n
\r\n \r\n\r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n
\r\n

Tap the button below to confirm your email address. If you didn't create an account with Paste, you can safely delete this email.

\r\n
\r\n \r\n \r\n \r\n \r\n
\r\n \r\n \r\n \r\n \r\n
\r\n Click here \r\n
\r\n
\r\n
\r\n \r\n
\r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n
\r\n

You received this email because we received a request for [type_of_action] for your account. If you didn't request [type_of_action] you can safely delete this email.

\r\n
\r\n

To stop receiving these emails, you can unsubscribe at any time.

\r\n

Paste 1234 S. Broadway St. City, State 12345

\r\n
\r\n
\r\n \r\n\r\n\r\n"; SendGridMessage msg = MailHelper.CreateSingleEmail(from, to, subject, null, htmlContent); Response response = await client.SendEmailAsync(msg); if (!response.IsSuccessStatusCode) { _logger.LogError("Email sender response is not success status code {statusCode}", response.StatusCode); throw new EmailSenderException("Invalid email operation"); } } public async Task SendChangePasswordAsync(string email, string changePasswordUri) { SendGridClient client = new(_senderSettings.Key); EmailAddress from = new(_senderSettings.Sender, _senderSettings.Name); string subject = "Reset password"; EmailAddress to = new EmailAddress(email, "Example User"); string htmlContent = $"\r\n\r\n \r\n \r\n Change password\r\n \r\n \r\n\r\n\r\n\r\n
\r\n A preheader is the short summary text that follows the subject line when an email is viewed in the inbox.\r\n
\r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n
\r\n \r\n \r\n \r\n \r\n
\r\n

Confirm Your Email Address

\r\n
\r\n
\r\n \r\n\r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n
\r\n

Tap the button below to confirm your email address. If you didn't create an account with Paste, you can safely delete this email.

\r\n
\r\n \r\n \r\n \r\n \r\n
\r\n \r\n \r\n \r\n \r\n
\r\n Click here \r\n
\r\n
\r\n
\r\n \r\n
\r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n
\r\n

You received this email because we received a request for [type_of_action] for your account. If you didn't request [type_of_action] you can safely delete this email.

\r\n
\r\n

To stop receiving these emails, you can unsubscribe at any time.

\r\n

Paste 1234 S. Broadway St. City, State 12345

\r\n
\r\n
\r\n \r\n\r\n\r\n"; SendGridMessage msg = MailHelper.CreateSingleEmail(from, to, subject, null, htmlContent); Response response = await client.SendEmailAsync(msg); if (!response.IsSuccessStatusCode) { _logger.LogError("Email sender response is not success status code {statusCode}", response.StatusCode); throw new EmailSenderException("Invalid email operation"); } } }