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:
Related post:
http://blog.smarx.com/posts/emailtheinternet-com-sending-and-receiving-email-in-windows-azure
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
You shouldn’t need to set enableNativeCodeExecution to true since that is the default value. I believe that it wasn’t thde default in early versions, but the default was changed since you can’t use Windows Azure Diagnostics without it.
http://msdn.microsoft.com/en-us/library/gg557553.aspx#WebRole
Thank you! i removed the piece of code from the blog post as it’s not a required step now. Thank you for pointing that out.