How to send an Email from an Azure application

Standard

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("smtp.mail.yahoo.com", 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!";
try
{
MySMTPClient.Send(myEmail);
status.Text = "Email Sent!";
}
catch (Exception ex)
{
// Display Exception Details
status.Text = ex.ToString();
}
[/sourcecode]

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:

1

Related post:

http://blog.smarx.com/posts/emailtheinternet-com-sending-and-receiving-email-in-windows-azure

http://social.msdn.microsoft.com/Forums/en-US/windowsazure/thread/409f1c6b-ba7a-4efa-aecf-51900d7f8c64/

http://social.technet.microsoft.com/Forums/en-US/windowsazuredevelopment/thread/4b72743e-4c9b-493b-8f71-65ee966ce52e

 

Please Note: This post is now owned by the community. URL: http://social.technet.microsoft.com/wiki/contents/articles/how-to-send-an-email-from-an-azure-application.aspx

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

What do you think? Leave a comment below.