Header Image Receiving email attachments with Azure blob storage

CloudMailin allows you to receive emails via an HTTP POST, essentially sending email to a webhook.

As we discussed in our post about email attachments on AWS, the default maximum size for web servers can often be a little low. In fact, the default in the .net Framework is 4MB. This would mean that when receiving email via HTTP POST in the .net Framework the maximum email size we could receive is 4MB, including email attachments. Whilst we could modify our config and set the maxRequestLength parameter CloudMailin gives a better option.

When receiving email on Azure we want to make sure that your servers don't have to worry about attachments. That's why CloudMailin can parse the attachements from email and send the email attachment to Azure Blob Storage automatically. When we do this we allow the web application to receive a small HTTP POST and pass the URL of the email attachment stored in Azure Blob Storage.

Setup is fairly straight forward, we need to:

Creating a storage container in Azure

First we need to head to the Azure portal and create a storage container:

creating a storage container in azure screenshot

We need to head to the storage account section and then select containers from the menu. In the new container dialog we'll need to name the container and leave the container as private. We'll sort out CloudMailin permissions in the next step.

Generate SAS URL allowing write access

In order to give CloudMailin access to the storage account and container we'll be using a Shared Access Signature (or SAS URL).

creating a SAS url screenshot

When creating the URL we'll need to give permissions to the Blob Objects and only allow Write permissions. This is all that CloudMailin will need to upload files. We don't want permission to be able to read the contents of the azure storage.

Configure CloudMailin to send the email attachments to Azure

Finally we can now add the full URL to CloudMailin and the container name. Head to the CloudMailin email address and add an 'attachment store'

configuring CloudMailin Azure screenshot

Now whenever CloudMailin receives and email with an attachment the attachment will be sent directly to Azure Blob Storage for you to fetch.

More details aboout the exact format are in the CloudMailin HTTP POST Format Documentation. In the noramlized multipart format we would see something like the following:

string url = Request.Form["attachments"][0]["url"];

The POSTed content includes the URL to the file, we can now fetch the file and download it:

The following are examples from the azure quickstart for listing and downloading blobs:

Console.WriteLine("Listing blobs...");

// List all blobs in the container
await foreach (BlobItem blobItem in containerClient.GetBlobsAsync())
{
    Console.WriteLine("\t" + blobItem.Name);
}
// Download the blob to a local file
// Append the string "DOWNLOAD" before the .txt extension
// so you can compare the files in the data directory
string downloadFilePath = localFilePath.Replace(".txt", "DOWNLOAD.txt");

Console.WriteLine("\nDownloading blob to\n\t{0}\n", downloadFilePath);

// Download the blob's contents and save it to a file
BlobDownloadInfo download = await blobClient.DownloadAsync();

using (FileStream downloadFileStream = File.OpenWrite(downloadFilePath))
{
    await download.Content.CopyToAsync(downloadFileStream);
    downloadFileStream.Close();
}
2020-07-10
CloudMailin Team