Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Hosted-services / Azure

Emailing via Azure in .NET Core 2.0

1.44/5 (2 votes)
1 Sep 2017CPOL 3.8K  
How to send emails in .NET Core using Azure SendGrid service. Continue reading...

Problem

How to send emails in .NET Core using Azure SendGrid service.

Solution

Create a class library and add NuGet package: Sendgrid.

Add a class to encapsulate settings and server response:

C#
public class AzureEmailSettings
    {
        public AzureEmailSettings(string apiKey)
        {
            if (string.IsNullOrEmpty(apiKey))
                throw new ArgumentException("Api Key must be set");

            this.ApiKey = apiKey;
        }

        public string ApiKey { get; }
    }

    public class ResponseMessage
    {
        public ResponseMessage(string statusCode)
        {
            this.StatusCode = statusCode;
        }

        public string StatusCode { get; }
    }

Add a class to encapsulate an email message (see also the previous post on builder pattern):

C#
public sealed class EmailMessage
    {
        private readonly List<string> to;
        private readonly List<string> cc;
        private readonly List<string> bcc;
        private readonly List<string> attachments;

        public EmailMessage(string subject, string from, string body, List<string> to,
            List<string> cc, List<string> bcc, List<string> attachments)
        {
            if (string.IsNullOrEmpty(subject))
                throw new ArgumentException("Subject must be set");

            if (string.IsNullOrEmpty(from))
                throw new ArgumentException("From must be set");

            if (string.IsNullOrEmpty(body))
                throw new ArgumentException("Body must be set");

            if (to.Count == 0)
                throw new ArgumentException("At least one To must be set");

            this.Subject = subject;
            this.From = from;
            this.Body = body;
            this.to = to;
            this.cc = cc;
            this.bcc = bcc;
            this.attachments = attachments;
        }

        public string Subject { get; }
        public string From { get; }
        public string Body { get; }
        public IReadOnlyList<string> To => to;
        public IReadOnlyList<string> CC => cc;
        public IReadOnlyList<string> BCC => bcc;
        public IReadOnlyList<string> Attachments => attachments;
    }

Add a class to wrap functionality of sending email messages:

C#
public class AzureEmailSender : IAzureEmailSender
    {
        private readonly AzureEmailSettings settings;

        public AzureEmailSender(AzureEmailSettings settings)
        {
            this.settings = settings;
        }

        public async Task<ResponseMessage> SendAsync(EmailMessage message)
        {
            // Message
            var msg = new SendGridMessage();
            msg.Subject = message.Subject;
            msg.From = new EmailAddress(message.From);
            msg.PlainTextContent = message.Body;
            msg.AddTos(message.To.Select(s => new EmailAddress(s)).ToList());

            if (message.CC.Count > 0)
                msg.AddCcs(message.CC.Select(s => new EmailAddress(s)).ToList());

            if (message.BCC.Count > 0)
                msg.AddBccs(message.BCC.Select(s => new EmailAddress(s)).ToList());

            if (message.Attachments.Count > 0)
                msg.AddAttachments(message.Attachments.Select(s => new Attachment
                {
                    Filename = s,
                    Content = Convert.ToBase64String(System.IO.File.ReadAllBytes(s))
                }).ToList());

            // Send
            var client = new SendGridClient(this.settings.ApiKey);
            var response = await client.SendEmailAsync(msg);

            // Return
            return new ResponseMessage(response.StatusCode.ToString());
        }
    }

To use the above class:

C#
// Prepare Email Message
            var message = EmailMessageBuilder
                            .Init()
                            .AddSubject(subject)
                            .AddFrom(from)
                            .AddBody(body)
                            .AddTo(to)
                            .Build();

            // Send Email Message
            IAzureEmailSender sender = new AzureEmailSender(new AzureEmailSettings(apiKey));
            var response = sender.SendAsync(message).Result;
            Console.WriteLine(response.StatusCode);

Note: The sample code includes the builder for email message.

Discussion

The sample code will require you to setup Azure account and SendGrid account. Instructions for these could be found here.

You’ll also need API Key, which can be created here using your Azure SendGrid username and password. More details can be found at this link.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)