Mail Service

Understanding Mail Service in Genocs Library's Web API.

Mail Service in Genocs Library’s Web API is a service that allows you to send emails from your application.

There are two ways to send emails using the Mail Service:

Sendgrid

Sendgrid is a cloud-based email service that provides reliable transactional email delivery. It is a popular choice for sending emails from your application. To use Sendgrid with the Mail Service, you need to configure the Sendgrid API key in the appsettings.json file.

Configure Sendgrid API Key

  1. Sign up for a Sendgrid account.
  2. Create a new API key in the Sendgrid dashboard.
  3. Add the Sendgrid API key to the appsettings.json file.
{
  "Sendgrid": {
    "ApiKey": "YOUR_SENDGRID_API_KEY"
  }
}

Send Email using Sendgrid

To send an email using Sendgrid, you need to create an instance of the SendgridMailService class and call the SendEmailAsync method.

This code snippet is used to send an email using SendGrid. The code snippet is written in C# and uses the SendGrid NuGet package. The code snippet is used to send an email to multiple recipients. The code snippet is written in C# and uses the SendGrid NuGet package. The code snippet is used to send an email to multiple recipients.


    /// <summary>
    /// Send Email using Sendgrid
    /// </summary>
    /// <param name="subject">Email Subject</param>
    /// <param name="content">Email Content</param>
    private async Task InternalSend(string subject, string content)
    {
        try
        {
            var client = new SendGridClient(_settings.ApiKey);
            var from = new EmailAddress(_settings.Email);
            var plainTextContent = "This email is generated by the system. Please do not respond on this account";

            var recipients = _settings.Recipients.Split(",");
            if (recipients.Length == 1)
            {
                var to = new EmailAddress(_settings.Recipients);
                var msg = MailHelper.CreateSingleEmail(from,
                                                    to,
                                                    subject,
                                                    plainTextContent,
                                                    content);
                var response = await client.SendEmailAsync(msg);
                var res = response.IsSuccessStatusCode;
            }
            else
            {
                var recipientsAddress = new List<EmailAddress>();
                foreach (string recipient in recipients)
                {
                    if (!string.IsNullOrWhiteSpace(recipient))
                    {
                        recipientsAddress.Add(new EmailAddress(recipient.Trim()));
                    }
                }

                var to = new EmailAddress(_settings.Recipients);
                var msg = MailHelper.CreateSingleEmailToMultipleRecipients(from,
                                                    recipientsAddress,
                                                    subject,
                                                    plainTextContent,
                                                    content);

                var response = await client.SendEmailAsync(msg);
                var res = response.IsSuccessStatusCode;
            }
        }
        catch (System.Exception ex)
        {
            _logger.LogError("Error on Send Email", ex);
        }
    }