How to Use a Web API to Send Email Asynchronously

web api send email async

Most developers encounter asynchronous code for the first time when dealing with Ajax requests and Javascript. However, async has been around since the early days of programming. And within recent years, async has become almost a standard in web development.

If you’re looking to use a web API to send email asynchronously, or if you’d like to learn more about async, then this post will help.

Before we discuss how to use a web API to send email async, let’s start with what async programming is.

What is Asynchronous Programming?

Asynchronous programming is a means of parallel programming that allows a process to run separately from the main application thread and notifies the calling thread of its status — either success, failure or progress.

Async programming looks something like this:

  • Issue a statement
  • Finish other tasks while waiting for the statement to complete
  • Then run a callback function when complete (a callback is a way of telling the program what code to execute when the request is complete)

The opposite of async is synchronous programming.

Handling tasks synchronously means waiting. The process usually looks something like this:

  • Issue a statement
  • Wait for a response…
  • …and then move onto the next statement.

The Difference Between Synchronous and Asynchronous Programming Explained by Baking a Cake

Asynchronous Programming Explained by Baking a Cake

Let’s look at synchronous and asynchronous programming through the example of baking a delicious chocolate cake… because really, who doesn’t love cake?

When baking a cake, you will typically start the process by preheating the oven.

Since the oven won’t preheat instantly, you may start doing other things like:

– Greasing and flouring the cake pan

– Combining ingredients into a bowl that’s also mixing the ingredients together

– And making the icing as you wait for your cake to finish baking

You may not realize this, but as you bake the cake you are performing little asynchronous tasks.

One task is heating up the oven, the other task is flouring the cake pan as the oven heats, and so on.

In the programming world, when writing code asynchronously, you’re essentially doing the same exact thing as when you’re baking a cake in the scenario above. For example, while waiting for HTTP requests to complete, your program could be running another task like updating a database.

Now imagine the opposite scenario:

You start the process of baking your cake by waiting for the oven to preheat before you grease and flour your pan.

And then you combine the ingredients into a mixing bowl.

And then you mix the ingredients.

Then you wait for the cake to bake.

Then only after the cake finishes baking, you make the icing.

In this scenario, it’s going to take you twice as long to bake the cake, even though you’ve done the exact same amount of work. This is how synchronous programming works.

The key thing to keep in mind when thinking about synchronous vs asynchronous programming is that async is all about getting many things to happen at the same time, so the time that you actually spend waiting is kept to a minimum.

Example of When to Use Asynchronous Programming

Asynchronous task handling makes sense when you don’t want your users to wait. One common use case for asynchronous programming is when a user signs up for your service.

When a user signs up, you typically want to grant access to your tools right away.

This means that a number of things must happen asynchronously behind the scenes:

  • HTTP requests are made
  • Data is stored in a database
  • The user’s data is synced to your CRM, which then delivers a welcome email
    In this case, you’ll want to handle the tasks asynchronously, so the user doesn’t have to wait to get access to your service.

How to Use a Web API to Send Email Async

Most web applications send transactional emails such as welcome emails, password reset links, invoices, and in-app notifications.

As an app scales, synchronously sending many transactional emails at once may cause a bottleneck in the application — ultimately degrading the user experience. For example, the application could become laggy for a number of seconds, or even suffer from connection timeouts.

These are some of the reasons why we recommend sending email asynchronously from your application. You can accomplish this using our send Email API and one of our pre-built libraries.

At SocketLabs, we recently launched a number of libraries for C#, PHP, Node.Js, Python, Golang, and Java. Some of our libraries have async methods that will call our Injection API on a background thread.

Below, is a code sample for using our C# Library for asynchronously sending an email message. This library along with our API for sending email will allow you to send email async in C#

        var client = new SocketLabsClient(00000, "apiKey");
         
        var message = new BulkMessage();
        
        message.PlainTextBody = "This is the body of my message sent to ##Name##";
        message.HtmlBody = "This is the HtmlBody of my message sent to ##Name##";
        message.Subject = "Sending a test message";
        message.From.Email = "[email protected]";
        
        var recipient1 = message.To.Add("[email protected]");
        recipient1.MergeData.Add("Name", "Recipient1");
         
        var recipient2 = message.To.Add("[email protected]");
        recipient2.MergeData.Add("Name", "Recipient2");
        
        var response = await client.SendAsync(message);
        
        if (response.Result != SendResult.Success)
        {
          // Handle Error
        }

In addition, our C# library also supports methods for asynchronously sending a bulk email message, which then returns the response from our Injection API. You can see a code sample on Github.

That wraps up our high level overview of using a web API to send email asynchronously. To learn more about our libraries visit the SocketLabs Developer Hub.