There already have been several blog posts (e.g. 1, 2, 3) about why and how to send emails from an Azure-hosted application.
I just wanted to summarize the essence and show some code on how to send email from Azure code via Exchange Online web services if you have an Exchange Online email subscription.
Turns out I was able to register a nice domain for my Exchange Online trial: windowsazure.emea.microsoftonline.com
So, here is the essential code snippet to send an email via EWS (Exchange Web Services) by leveraging the EWS Managed API 1.1 (get the download here):
var service = new ExchangeService(ExchangeVersion.Exchange2007_SP1); service.Url = new Uri(
"https://red002.mail.emea.microsoftonline.com/ews/exchange.asmx"); service.Credentials = new WebCredentials(userName, password); var message = new EmailMessage(service); message.ToRecipients.Add("[email protected]"); message.From = new EmailAddress(
"[email protected]"); message.Subject = "Hello EMail - from Windows Azure"; message.Body = new MessageBody(BodyType.HTML, "Email from da cloud :)"); message.SendAndSaveCopy();
In the code above I am sending the email via the EWS host for Europe – you may need different URLs for your location:
Asia Pacific (APAC): https://red003.mail.apac.microsoftonline.com
Europe, the Middle East, and Africa (EMEA): https://red002.mail.emea.microsoftonline.com
North America: https://red001.mail.microsoftonline.com
Hope this helps.
What should the userName be in the above example? The person's exchange online email address? Also when testing this code on by dev machine I keep picking up my local credentials regardless of what I set on service.Credentials? This means I am able to send emails from my machine but my partners machine won't authenticate?
Thanks,
-Patrick
Posted by: PSimpson | 02/10/2011 at 10:51 AM