C#, .Net and Azure

Email relay


After I migrated my website to blob storage via Azure CDN I accidently broke the MX record of my website and instead of fixing it like a normal person I decided to build my own email solution.

I abused a free SendGrid account in combination with an azure function as my mail service:

Yeah, but your scientists were so preoccupied with whether or not they could, they didn’t stop to think if they should.

Features include:


Mainly I wanted to see if I could get rid of my 2$/month email service but still be able to send/receive emails from my domain.

Since I hardly ever send emails from my domain I was willing to accept a few shortcomings.

I was already familiar with SendGrid inbound parse thanks to a previous project so I figured I could throw together a quick PoC whether SendGrid can be used as a free email service.

A few hours later the answer was yes.

I polished the project and published it on github so others can use it as well.

Note that more professional solutions exist as well (such as ImprovMX) however from what I have seen they all require you to enter your email password into their service (giving them theoretically full access to your private email.

The way my email function works, it only needs access to your domain email (via SendGrid) but not your private email.

And since you have to deploy the Azure function yourself (for pennies a month) I (personally) don’t get access to any of your mails.

How it works

Under the hood an azure function is used to receive SendGrid Inbound Parse events. They are fired by SendGrid for every incoming email.

The azure function then parses the email and forwards it to you own private email.

Since there is no concept of mail storage at the domain (SendGrid only forwards it) the mails can optionally be archived in azure blob storage as well (although the forwarded emails will already be stored in your private email account and that’s good enough for me).

Usage examples

After you set it all up you can use it to send/receive emails.

Receive an email

Example: sender@example.com sends an email to you@yourdomain.com.

Since you configured all emails to be relayed to yourprivatemail@mail.com you will receive an email from you@yourdomain.com with subject “Email relay for: sender@example.com: Test”

The subject will let you know who actually sent it while the sender is the alias from your domain that originally received the email (this seems clunky but is the only way to get the email to your private inbox).

Responding to emails

When you respond to such an email you essentially respond to you@yourdomain.com. The azure function will parse the email again, realize it is coming from your configured private email and then transform the subject and send it to the correct address in the name of the domain.

The subject format “Email relay for: <email>” will be parsed and result in an email from you@yourdomain.com to be sent.

Example: If you respond to the email above an email with subject “Test” will be sent from you@yourdomain.com to sender@example.com.

Sending an email

To send a new email in the name of the domain you need to send an email from your registered private email to the domain (the azure function will realize it’s you and relay the email).

To send an email “Test subject” from contact@yourdomain.com to recipient@example.com you must send an email with subject “Email relay for: recipient@example.com: Test subject” to contact@yourdomain.com.

The special subject and the fact that the sender is your registered email will let the azure function know that it should send an email from the domain to the target.

Known limitations

There are a few problems I am aware of (listed on github as well) so check if those are deal breakers for you:

Setup & more examples

The entire function is opensource on github including a setup guide and more examples.