Geeks With Blogs
Thorvald Bøe
The "something-as-a-service" model is already a defacto standard in IT. If you need something to get done, there is almost certainly a service that will do that for you. As the model matures, you can sit back and rest assured that the job is done well and cheap.

So when I faced the need for sending email from my web service, why did I spend half the afternoon trying to set up an SMTP server in Azure? I should have used SendGrid from the beginning!

Setting up your own SMTP server for sending email may work just fine. And I have done so in the past, with success. However, this time I ran into some problems because I tried using an existing VM in order to save time and money. The problem was, when I had set up the SMTP features, it didnt work. I think it had something to do with domain name resolution. Anyway, while investigating this, I came across SendGrid.

SendGrid is a service that allows you to send e-mail - lot's of them! In fact, they have a free program that allows you to send 25000 email per month for free, if you provision your account from the Azure marketplace. The service comes with a Nuget package you can use from visual studio with c# among other supported languages. They also have a lot of fancy functions for tracking, analysis, encryption etc. I just need to send an email every once in a while, so for me, this was a no-brainer. 25k is more than enough.

In order to get started, I followed this tutorial:

I had almost no problems using this tutorial. Almost. But since the article was written, they removed the "deliver" method, which forces you to use the DeliverAsync method instead. This was what caused me some problems. 

If you are like me, you like to take some sample code, throw it inside a console app, and just make it work as quickly as possible. So I did, using the code in the tutorial:

static void Main(string[] args)
            SendGridMessage myMessage = new SendGridMessage();

            myMessage.From = new MailAddress("");
            myMessage.AddTo(""); //make sure you use your own email here

            myMessage.Subject = "Sendgrid message " + DateTime.Now.ToString();
            myMessage.Text = "This is a test message using SendGrid";

            string userName = "yourusernamehere";
            string pwd = "yourpasswordhere";
            var credentials = new NetworkCredential(userName, pwd);
            Web transportWeb = new Web(credentials);

There are no errors in the code above. But it did not work. Can you see why?
There is one vital line of code missing. I guess I missed it because of my lack of async experience: Add Console.Readline() at the end, and it works fine.

If I knew something about async programming, I could probably tell you why that is necessary. I guess the simple explanation is that the console application quits before the async "DeliverAsync" method has time to do anything.

I spent several hours trying to figure this out yesterday with no luck. The thought of the missing Console.Readline() struck me when I woke up this morning. So I guess for a guy that knows almost nothing about c# async programming, I was a little bit proud when it worked :)

After educating myself a bit on async, I found you can do this, and drop the Console.Readline() call:


This prevents the program from returning until delivery is completed. Obvious when you know your async 101, but at the time of writing the post, obviously, I didn't :)
Posted on Wednesday, September 9, 2015 9:34 AM email , async | Back to top

Comments on this post: Send email using SendGrid

# re: Send email using SendGrid
Requesting Gravatar...
Exact same issue. I had to switch to SendGrid V4 instead, so I could use the Deliver() method.
Left by dp on Nov 10, 2015 8:21 PM

Your comment:
 (will show your gravatar)

Copyright © Thorvald Bøe | Powered by: