A notification system is a must in a customer-driven web application to alert users of certain events occurring in the application. Even though the most common approach is to use in-app notifications, the user has to frequently visit the application to stay informed. As a result, developers adopted emails, a readily accessible channel to the users, to create event-driven notifications such as account confirmations, payment confirmations, etc.
This article will evaluate four ways of sending an email using C# to ensure that you select the best event-driven email notification service for your C# application to keep the users informed on key application events.
Fundamentals of Email Sending
Before sending emails in a C# application, knowing the fundamentals will help streamline the process.
Templated Emails vs. Plain Text Emails
A templated email consists of an HTML body with CSS styling. It is commonly used to communicate with end-users.
Figure 01 - Representation of a template email
Plain Text Emails
A plain text email consists of a textual email body. It is used in applications to notify internal users of system errors.
Figure 02 - Representation of plain text email
Input Sanitization for Emails
When sending emails based on user inputs, it's vital to sanitize user inputs to remove unnecessary HTML attributes from the input fields of your forms, which helps to send emails securely.
1. Sending an email using SMTP
The standard approach to send an email using C# is SMTP (Simple Mail Transfer Protocol). It is a network protocol used to send emails over the internet. Additionally, it allows you to relayemails across multiple networks. Therefore, SMTP is widely used in many applications to send emails.
There are certain advantages of using SMTP to send emails in your C# application.
- It is pre-built into the .NET Framework via the
System.Net.MailNamespace. Therefore, it is effortless to get started.
- You only require SMTP credentials to get started.
- The built-in SMTP library assigns the required headers for sending the email.
However, sending emails with SMTP creates some drawbacks.
- SMTP communicates back and forth between the client and the SMTP server. Therefore, it adds latency overheads that delay the process.
- It opens and closes an SMTP connection per email. Hence, sending bulk emails is not recommended with SMTP as it can cause performance issues.
If the added latency is not an issue, and you need a quick setup process, refer to this section to send a templated email via the Gmail SMTP Server.
First, you will need to create a Google account to get started. Then, you will need to visit your account settings and enable less secure devices to communicate with your Google account. It is crucial because it will create authentication errors if you do not do this.
Figure 03 - Enabling less secure app access in Google
Afterward, you can use the snippet below to send templated emails via the Gmail SMTP server.
The snippet above successfully sends an email via the Google SMTP server.
2. Sending an email using a Transactional Email API - SendGrid
If your application has to notify users of important events such as payment confirmations or request submissions, sending emails using SMTP will not work for you because SMTP opens and closes a connection per email and creates performance overheads. Therefore, it is highly inefficient.
The best approach is to use a transactional email API.
- Since sending emails over an API eliminates the need for a local SMTP server, it's faster and reduces latencies.
- The Email APIs have authorized domains. Therefore, SMTP clients do not flag emails sent from APIs as spam.
- Most email APIs offer analytics to monitor click-through and bounce rates of the sent emails.
- Email APIs can rapidly scale up and down based on your demand, unlike SMTP authentication. Therefore, even if you serve 10,000 emails daily, the API will remain active to deliver all 10,000 emails.
However, email APIs do have their disadvantages.
- The email APIs have a high learning curve.
- Some email APIs are language-dependent.
For developers seeking to use email APIs for C# applications, SendGrid is an excellent and easy-to-use email API with a free plan that allows you to send 100 emails per day. Additionally, it offers domain verification that ensures your emails are not flagged as spam, and it provides analytics for all emails sent using SendGrid.
Follow this walkthrough to send an email using SendGrid.
Setting up SendGrid
Figure 04 - Creating the sender identity on SendGrid
Afterward, an API key must be created. This API key will be used by the C# application to communicate with the SendGrid API to send emails using the verified sender identity.
Figure 05 - Creating the API key for SendGrid
Provide Full Access permission scope for the API Key for testing.
Figure 06 - Created API Key
Sending the Email
Next, install SendGrid to your C# application using
NuGet with the command displayed below.
Afterward, add the code shown below to a C# file to send the email successfully.
The code above sends an email using the SendGrid Email API. Ensure that the sender email is the same email that you verified earlier.
To confirm if the email was delivered, visit the SendGrid dashboard and navigate to "your activity." This dashboard also provides analytics on the email, allowing you to see if the user read or discarded the email.
3. Sending an email using a Transactional Email API - Amazon SES
Amazon SES (Simple Email Service) is a cost-effective and scalable email service offered by AWS to send transactional emails quickly.
Compared to SendGrid, Amazon SES is more advantageous.
- It allows applications to send 62,000 complimentary messages per month, while SendGrid only allows 100 messages per day.
- SES is built to scale effectively with no noticeable latency. Therefore, it is capable of sending over 1000 messages at once!
- All it takes is one click to set up DKIM verification in SES. Therefore, your emails can be sent from a verified sender, which prevents emails from being flagged as spam unnecessarily.
There is one main disadvantage of using SES for sending transactional emails.
- SES does not offer analytics out of the box. To do so, you would have to set up AWS Pinpoint. It adds complexity for users that require email analytics.
If you don't require analytics for your emails and want to send bulk emails per month, Amazon SES is perfect for you!
The walkthrough below shows how to send transactional emails using the AWS SES API.
Before getting started:
- Make sure to create an AWS Account (on the free tier).
- Obtain the access keys.
- Configure a shared credentials file so the C# application can communicate with AWS via the SDK.
Afterward, visit the Amazon SES dashboard in your AWS Account. Initially, you will have access only to the sandbox environment. When sending emails with sandbox, verifying both the sender and receiver emails via SES is essential.
Later on, a request can be made to AWS for production access.
Figure 07 - Verified Identities on SES
Figure 08 - Creating an email identity on SES
After setting up the sender and recipient identity, you can install the AWS SES Package in the C# application by executing the command below.
To send templated emails, make sure to create and upload the templates to SES beforehand.
Finally, the code below can be used to send transactional emails to users via the Amazon SES SDK.
Ensure to replace the sender and recipient addresses with your verified identities and set your SES region's region.
4. Sending an email using a Multi-Channel Notification Service - Courier
The first three ways discussed are suitable for applications relying only on email. But, what if a new notification channel such as Text Messages get set up in the future? Then, developers would have to integrate new libraries and set up everything from scratch, which is time-consuming.
This is where multi-channel notification services such as Couriercome into play. Courier streamlines your notification flow by allowing you to configure multiple notification channels and communicate with all the channels with one API.
Using Courier is highly advantageous. Some of these advantages are:
- It allows users to get notified across multiple channels with one API.
- From a developer's perspective, the codebase decreases as all notification channels are managed under one API.
- Courier allows you to add new notification channels to a template from the dashboard, which makes it easy for anyone to use Courier.
- Courier provides analytics for notifications across all notification channels.
- Courier integrates with third-party transactional APIs such as Amazon SES and SendGrid to help manage notifications from a centralized location.
The biggest drawback of using Courier in a C# application is that there is no developer SDK available at the moment. So for the moment, the only way to interact with Courier is by sending HTTP Requests through their REST API.
However, if not having an SDK is not a problem for you, you can create a free account at Courier and send 10,000 emails monthly.
Refer to the walkthrough below to email Gmail in your C# application with Courier.
To start, create a free account on Courier (if you haven't already).
Upon successful creation, navigate to "Integrations" in the Courier Dashboard and click on "Gmail" under Email Providers to set up the email notification channel.
Next, click "Sign in with Google" and authorize Courier to send emails through the authorized Google account.
Figure 09 - Setting up the email notification channel
If you set it up correctly, you will see the output below.
Figure 10 - Setting up Google with Courier
Afterward, we can create a notification in Courier. To do so, navigate to the dashboard and click "Designer" and click "Create Notification". You should see the output below.
Figure 11 - Creating a notification on Courier
Then, add a channel to the notification by selecting "Email." It will choose the Gmail account that was authorized earlier. Afterward, click on the channel to view the template.
Figure 12 - Previewing the template with the authorized sender
You can customize the template according to your requirements, but I will add a "Hello World" message in the body for the demonstration.
Figure 11 - Creating the email template
After creating the template, publish the changes.
Finally, you can use the Courier REST API (obtain Courier Auth Token from the template) to send the email via Gmail using the code below.
Update the code with your authentication token, Template (notification) ID, and recipient email to successfully send the email with Courier via the authorized Gmail account.
Visit the "Data" section in the Courier Dashboard to obtain timely analytics on the emails.
This article explored four different ways to send an email using C#. There is no clear winner here as the service you opt for solely depends on your use case. However, the multi-channel notification service - Courier stands out from the rest with its capability to deliver event-driven notifications across multiple channels seamlessly and efficiently.
I hope this article helps you select the best approach to send emails in your C# applications.
Thank you for reading.