BANGALORE, INDIA: A lot of companies rely on Microsoft Exchange to provide their messaging and collaboration services – such as email, scheduling, calendaring, contacts, tasks and other management tools. With the use of Outlook Web Access and MS Outlook, users can be extremely productive in what they do. However, it is sometimes necessary to be able to perform some other messaging or collaboration tasks that are not available directly within the interface of either of these two software. In such a case a custom software can be built that uses Exchange to process the requirement.
This article takes a look at a new method to do this. Traditionally to use mail from code you need to use things like SMTP, CDOSYS, IMAP commands and stuff. However, with more and more companies moving to Exchange which uses MAPI, you need to use a different method to do this. MAPI not only gives access to mail, but all the other features of Exchange as well. But one of the sad things was using MAPI was quite hard – till now. Microsoft has released a new managed API called Exchange Web Services (EWS) as part of Exchange 2007 SP1 and Exchange 2010. Although the API is the same for both, there are some extra features that are possible only when using Exchange 2010. We will look at some simple examples of using different features of Exchange with this.
Installing EWS Managed API
The first thing to do is to of course get the API into your application. For this, download the appropriate (32-bit/64-bit) version of EWS Managed API v 1.0 from the Microsoft Download Center. Install the pack while noting down the installation folder. Now fire up VS2008 or VS2010 and create a new project – say a Windows application. First off, add a new reference to the Microsoft. Exchange. WebServices.dll file found in the installation folder. Then in the code, add the following statement:
In this window we add a reference to the EWS API DLL in the project.
Using Microsoft.Exchange.WebServices.Data;
Sending mailNow drop in a textbox(for the email address) and a rich text box (for the emailbody) and a button to send emails. In the button's event handler, add the following code:ExchangeService service = new ExchangeService();// Use when machine is IN Domainservice.UseDefaultCredentials = true;service.AutodiscoverUrl("user@domain.com");// Use when machine is NOT in Domain service.Credentials = new WebCredentials("user@domain.com", "password");service.Url = newUri("https://ExchangeServer/EWS/Exchange.asmx");// Create an e-mail messageEmailMessage message = new EmailMessage(service);// Add properties to the e-mail message.message.Subject = "Sending from C# Code";message.Body = richTextBox1.Text;message.ToRecipients.Add(textBox1.Text);// Send the e-mail message and save a copy in the default Sent Items foldermessage.SendAndSaveCopy();
Sending an email from code and receiving it correctly.
The code creates a new instance of the EWS. Depending on whether the machine you will run the application on is in the same domain as the Exchange server or not, you will need to choose between two sets of code for credentials and the URL. The AutodiscoverUrl is preferred, but you can code in the URL as well as shown. The nice thing about using Exchange for sending mails is that it saves copies on the server – making even sent items completely accessible from anywhere – something that POP/SMTP combinations simply cannot do.
Get most out of your technology infrastructure investments with Dell
About CIOL | Media Kit | Site Map | Contact Us | Help | Write to us | Jobs@CyberMedia | Privacy Policy
Copyright © CyberMedia India Online Ltd. All rights reserved. Usage of content from web site is subject to Terms and Conditions.