Share via


How to send an Email from an Azure application

In this wiki, 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. For instance: LIVE SMTP server, Yahoo Mail plus SMTP server, etc. Also use 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.

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

http://parasdoshi1989.files.wordpress.com/2011/07/trust.jpg?w=498&h=307

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

SmtpClient MySMTPClient;
MailMessage myEmail;
  
MySMTPClient = new  SmtpClient("<SMTP server address>", 25);
MySMTPClient.Credentials = new  NetworkCredential("<MailID>", "<Password>");
myEmail = new  MailMessage(new  MailAddress("<sender>"), new  MailAddress("<receiver>"));
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();}

In the above code, please fill in the appropriate values for text inside "< >". The description of the value is between "< >".

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

That’s it. By 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:

http://parasdoshi1989.files.wordpress.com/2011/07/17.jpg

EmailTheInternet.com: Sending and Receiving Email in Windows Azure

System.Net.Mail.SmtpPermission not granted for smtp client from within a webrole

MS Tech Talk: "Can I install SMTP server on Windows Azure?"