How to send an Email from an Azure application


In this BlogSpot, we will discuss how to send an email from an Azure application. As of now, Azure does not support SMTP relay and for few logical reasons. read: why should we not send emails directly through Azure . So we would have to use a third party SMTP server.  I used the Yahoo SMTP server (available only with the Yahoo Mail Plus) to try implementing a solution that sends an email in an Azure application. I used the port 25 to implement the functionality. if you wish to use a port except 25 then you should make sure that the role is not running in the partial trust mode. Now, in post 1.2 versions of the Azure SDK the ‘enableNativeCodeExecution’ attribute is set to true (by default). [Thanks to Neil Mackenzie for pointing that out; earlier i had shown code for setting the ‘enableNativeCodeExecution’ value to true, but now since, it is always on by default, we do not need that piece of code! Thanks again!]

Role Configuration (Note that the .Net Trust level is at Full trust by Default):

Now, a sample code to send an email from an Azure WebRole is:

[sourcecode language=”csharp” toolbar=”true” light=”true”]
SmtpClient MySMTPClient;
MailMessage myEmail;

MySMTPClient = new SmtpClient("", 25);
MySMTPClient.Credentials = new NetworkCredential("", "");
myEmail = new MailMessage(new MailAddress(""), new MailAddress(""));
myEmail.Body = "Just some random text to test the email Functionality for my Azure Application";
myEmail.Subject = "Email from an Azure app!";
status.Text = "Email Sent!";
catch (Exception ex)
// Display Exception Details
status.Text = ex.ToString();

Note: Do not forget to add “System.Net.Mail” and “System.Net”

That’s it. Using the above code – you will be able to send the email from an Azure application!

I uploaded my app to Azure. Here is the snapshot of the working solution:


Related post:


Please Note: This post is now owned by the community. URL:

0 thoughts on “How to send an Email from an Azure application

What do you think? Leave a comment below.