About Me

I am a Software Engineer presently working in Microsoft as vendor.The main aim of this blog is to Share my knowledge in Sharepoint,AZURE and Silverlight.

Friday 2 July 2010

How to Send an Email from Microsoft Azure (cloud).

Hi, Sorry for not updating my blog these days. from past eight months I am working very less in sharepoint 2010.These days I am working more on new emerging technology "Microsoft Azure" .As we aware Azure is cloud computing.These days I am getting requests from other teams as well as from other colleagues about how to send an email from Azure.

So here are the ways I did it practically in our applications.

1)sending mail using exchange service
2)sending mail using SMTP Server
a)using service bus
b)using a windows service
3)you can also use public available mail services like LIVEID,Google
4)SMTP Relay or mail relay service(which I dont prefer as I faced lots of port issues.)

lets see one by one

easy approach to send an email from azure is using exchange service :). I prefer this approach.If the application is having any dependency on SMTP service then next two approaches can be used.

sending mail from exchange service is 4 steps.

1)Download the dll . 32bit is fine


2)Reference this dll in your project.((but make sure copylocal property set to True)

3)set the values in the service configuration.(for easy configuration)
I am using web.config in this but better to use service configuration instead of web.configuration.

<---add key="EmailDomain" value="your Domain Name"/>
<---add key="EmailUserName" value ="provide your username "/>
<---add key="EmailPwd" value="provide password "/>
<---add key="EmailID" value="provide your email ID"/>
<---add key="ExchangeserverURI" value="provide your exchange service URI"/>

4)use the below code to send an email.
try
{

string domain = WebConfigurationManager.AppSettings["EmailDomain"];
string username=WebConfigurationManager.AppSettings["EmailUserName"];
string pwd=WebConfigurationManager.AppSettings["EmailPwd"];
string email=WebConfigurationManager.AppSettings["EmailID"];
string exchangeuri=WebConfigurationManager.AppSettings["ExchangeserverURI"];

ExchangeService service = new ExchangeService();
service.Credentials = new WebCredentials(username, pwd, domain);
service.Url = new Uri(exchangeuri);
service.AutodiscoverUrl(email);
EmailMessage message = new EmailMessage(service);
message.ToRecipients.Add(e.Message.To[0].ToString());
message.From = new EmailAddress(email);
message.Subject = e.Message.Subject;
message.Body = e.Message.Body;
// The following method sends the mail and also saves a copy in the 'Sent Items' folder
message.SendAndSaveCopy();
}


That's it now you can send an email from AZURE using exchange service. :) Give a try if you are facing problems add a comment.

2)some applications depends on SMTP server.if this SMTP server resides in our intranet or company's network it is not easy to access from cloud.as the smtp is protected using firewalls and NAT's to avoid spam's or attacks by external users.

there are two ways to achieve this.

1)using service bus.

I am not going to explain it detailedly but ill give you steps how to do. if possible ill also update the code if I get some time. :)

create a wcf service which uses SMTP server and host it in IIS or windows service which reads mail message and using service bus connect to this wcf service that's it now you are good to send an email.

2)not recommended but if application is having mails to send on timely basis like daily once or for every 6 hr's some thing like this they can use this approach.

store the mail details in SQLAzure and read from sql azure using windows service and send the mails using your SMTP server.


That's it as of now these are the possibilities we have in AZURE and I did all these practically in my Azure applications.