locked
Exchange Web Services 2010 RRS feed

  • Question

  • Hello dear friends,

    I have installed exchange server 2010 now i want to write a exchange script for mail box, once inbox received mail script should fire.

    I implemented in exchange server 5.5 & also implemented in 2003 with event sinks but in Exchange Server 2010 and later Microsoft recommends using either Transport Agents, or Exchange Web services notifications.

    now i wanted to know how to achieve my requirement :

    1. Once i get mail in inbox script should run automatically

    2. If I write code in Exchange web service then how code execute automatically once mail comes in

    3 what is the requirement to install sdk exchange ws

    4 can we do it by using power shell script if yes then how to link this with inbox

    pls guide me

    Regards

    Jagdish Kotian

    Monday, September 26, 2011 1:51 PM

Answers

  • Hi,

    execute the AutoDiscover this way:

     service.AutoDiscover("myemailaddress@company.com");

    Be sure to assign the credentials to the service, either via

     service.UseDefaultCredentials = true;

    or

     service.Credentials = new NetworkCredential(username, password, domain);

    To answer your second question: You are using the wrong approach here.

        var items = service.FindItems(WellKnownFolderName.Inbox, new
    ItemView(512));

    And have a look at http://www.infinitec.de/post/2011/09/08/FindItems-Helper-method-for-the-Exchange-Managed-API.aspx.

    Kind regards,
    Henning

    If you get your question answered, please come back and mark the reply as an answer.
    If you are helped by an answer to someone else's question, please mark it as helpful.

    • Marked as answer by Jagdish Kotian Wednesday, October 12, 2011 12:09 PM
    Wednesday, October 12, 2011 11:38 AM
  • Hi,

    you do not call the event handlers yourself - the StreamingConnection will call them when their time has come.

    And you need to write a Windows service which runs constantly in the background.

    http://msdn.microsoft.com/en-us/library/zt39148a(v=vs.80).aspx
    http://www.infinitec.de/post/2009/06/13/Developing-Windows-Services-with-NET.aspx

    Kind regards,
    Henning

    If you get your question answered, please come back and mark the reply as an answer.
    If you are helped by an answer to someone else's question, please mark it as helpful.

    Thursday, October 13, 2011 4:26 PM
  • HI Jagdish,

    yes, you can do that. Just set the UseDefaultCredential to true on your service instance and change the user the service runs under to a domain user with appropriate permissions.

    Kind regards,
    Henning

    If you get your question answered, please come back and mark the reply as an answer.
    If you are helped by an answer to someone else's question, please mark it as helpful.

    • Marked as answer by Jagdish Kotian Thursday, October 20, 2011 6:28 AM
    Wednesday, October 19, 2011 11:43 AM
  • success :)

     

    FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox, new ItemView(512));

     

    FindFoldersResults findResultsFindFolder = service.FindFolders(WellKnownFolderName.Inbox ,new FolderView(int.MaxValue));

     

    Folder folderToMove = null;

    foreach (Folder folder in findResultsFindFolder)

    {

           //find folder
           if (folder.DisplayName == "xyz")

     

                folderToMove = folder;

           }

    }

    //move item to xyz folder

    findResults.Items[0].Move(folderToMove.Id);

     

     

    Henn, thank you for all your best nd best answer which helped me lot & i learned lot.

     

    With Best Regards

    Jagdish

           {
    Jagdish
    • Marked as answer by Jagdish Kotian Tuesday, November 12, 2013 9:40 AM
    Thursday, October 20, 2011 9:24 AM

All replies

  • Hi Jagdish,

    since you only want to target one specific mailbox, I recommend you write a Windows service. This service can use StreamingSubscriptions (See http://msdn.microsoft.com/en-us/library/hh312849(EXCHG.140).aspx for an article I wrote on MSDN about this) to monitor the inbox.

    You can then use the Exchange WebServices (or EWS Managed API, in case you are writing the service using .NET) to access the new messages in that mailbox.

    If you don't need to process the mails immediately, things get simpler. Since you already know PowerShell, you can use the EWS Managed API in a PowerShell script and call that every x minutes. Glen has a lot of articles on his blog how to use the EWS Managed API from Powershell.

    EWS Managed API - Download: http://www.microsoft.com/download/en/details.aspx?id=13480

    EWS Managed API - SDK: http://msdn.microsoft.com/en-us/library/dd633710(v=exchg.80).aspx

    Glen's Blog: http://www.gsexdev.blogspot.com/

    Kind regards,
    Henning Krause

    Monday, September 26, 2011 5:07 PM
  • i had gone through the code now my question is :

    1. this code asking for URL to the Exchange Web Services - which url should i enter i am confused

    2. where should i deploy this code & how application execute when inbox gets mail

    3. also i want to read subject line of each incoming mails from code - how to achieve this

    Tuesday, September 27, 2011 11:00 AM
  • Hi Jagdish,

    1. you can either use AutoDiscover to determine the URL automatically. However, your Exchange administrator needs to configure this on your site. Use the AutoDiscoverUrl method on the ExchangeService instance to do this (http://msdn.microsoft.com/en-us/library/dd635285(v=exchg.80).aspx). Another option is to set the URL property by hand. You need to specify your Exchange Server here. It will be something like https://your-exchange-server/ews/exchange.asmx.

    2. Your solution can be deployed on any computer. It doesn't really matter. All it needs is network access (HTTP) to the Exchange box.

    3. When you have the ID of an item, you can use the Item.Bind() method to access an item. (http://msdn.microsoft.com/en-us/library/microsoft.exchange.webservices.data.item.bind(v=EXCHG.80).aspx).

    Kind regards,
    Henning Krause

    Tuesday, September 27, 2011 7:30 PM
  • Hi Henning,

    Thanks for the above help, now I have little bit hands on over exchangews2010 

    now few more question :

    1.    When I run code service.AutodiscoverUrl(“emailid@emailid.com”) getting error The Autodiscover service couldn't be located. when I run Autodiscover (http://localhost/Autodiscover) shows ErrorCode : 600 and Message : Invalid Reques

    2.    When I pass ID of an item as below shows error ID is malformed.

          here I wanted to read emails subject line of an item

          Item item = Item.Bind(service, new ItemId("Inbox"));

     

    Thanks in advance

    Regards

    Jagdish

     
    Wednesday, October 12, 2011 10:54 AM
  • Hi,

    execute the AutoDiscover this way:

     service.AutoDiscover("myemailaddress@company.com");

    Be sure to assign the credentials to the service, either via

     service.UseDefaultCredentials = true;

    or

     service.Credentials = new NetworkCredential(username, password, domain);

    To answer your second question: You are using the wrong approach here.

        var items = service.FindItems(WellKnownFolderName.Inbox, new
    ItemView(512));

    And have a look at http://www.infinitec.de/post/2011/09/08/FindItems-Helper-method-for-the-Exchange-Managed-API.aspx.

    Kind regards,
    Henning

    If you get your question answered, please come back and mark the reply as an answer.
    If you are helped by an answer to someone else's question, please mark it as helpful.

    • Marked as answer by Jagdish Kotian Wednesday, October 12, 2011 12:09 PM
    Wednesday, October 12, 2011 11:38 AM
  • Hi Henning,

    Many thanks :),  now i  am able to read Items.

    But service does not contain defination for  AutoDiscover - service.AutoDiscover("myemailaddress@company.com");

    - i can see only service.AutodiscoverUrl(.....

    some other questions :

    i want to achieve following :

    1. once mail box received mails in inbox then i want to execute above code, like event sinks in Exchange Server 2003.

    Steps of execution

    Step1. Inbox received mail

    Step2. once mail received Inbox then Execute above code automatically i.e read subject line and does the some processing & mail move to subfolder.

    Code is ready but how to achieve auto execution of code once mail received in Inbox.

     

    Thanks & Regards

    Jagdish


    Jagdish
    Wednesday, October 12, 2011 12:41 PM
  • Yes, it's AutoDiscoverUrl.. my fault on that part...

    Regarding your other problem.. you may want to look into streaming subscriptions.
    http://msdn.microsoft.com/en-us/library/hh312849(EXCHG.140).aspx

    Another option would be polling. Just run your program every x minutes to check for new mails...

    Kind regards,
    Henning

    If you get your question answered, please come back and mark the reply as an answer.
    If you are helped by an answer to someone else's question, please mark it as helpful.

    Wednesday, October 12, 2011 1:04 PM
  • hi henning,

    my code is :

    static void Main(string[] args)

    {

    service.Credentials = new NetworkCredential("jagdish_admin", "password", "domain" );

    service.Url =

    new Uri("http://localhost/EWS/Exchange.asmx");

    SetStreamingNotifications(service);

    }

    static

    void SetStreamingNotifications(ExchangeService service)

    {

     

     StreamingSubscriptionConnection connection = new StreamingSubscriptionConnection (service, 30);

     StreamingSubscription streamingsubscription = service.SubscribeToStreamingNotifications(new FolderId[] { WellKnownFolderName.Inbox }, EventType.NewMail);

    connection.AddSubscription(streamingsubscription);

     // Delegate event handlers.

     

     connection.OnNotificationEvent +=new StreamingSubscriptionConnection.NotificationEventDelegate (OnNotificationEvent);

    connection.OnSubscriptionError +=new StreamingSubscriptionConnection.SubscriptionErrorDelegate (OnSubscriptionError);

    connection.OnDisconnect +=new StreamingSubscriptionConnection.SubscriptionErrorDelegate (OnDisconnect);

    connection.Open();

     }

     static private void OnDisconnect(object sender, SubscriptionErrorEventArgs args)

    {

    //some code

     }

     static void OnNotificationEvent(object sender, NotificationEventArgs args) {

      

    //some code

    }

    static void OnSubscriptionError(object sender, SubscriptionErrorEventArgs args) {

    //some code

    }

    my question 1 : is how to call funtion OnDisconnect(),OnNotificationEvent() & OnSubscriptionError().

     

    my question 2:  i have developed console application. whenever I run this programme executes and stops.  

    Still i am not cleared (sorry about this) if i deploy this exe file on exchange server this will not execute automatically until unless i execute it manually.

    what i want is  application should listen to a mailbox and take actions when a new email comes in.

     

    many thanks

    regards

    jagdish

     

    Thursday, October 13, 2011 10:37 AM
  • Hi,

    you do not call the event handlers yourself - the StreamingConnection will call them when their time has come.

    And you need to write a Windows service which runs constantly in the background.

    http://msdn.microsoft.com/en-us/library/zt39148a(v=vs.80).aspx
    http://www.infinitec.de/post/2009/06/13/Developing-Windows-Services-with-NET.aspx

    Kind regards,
    Henning

    If you get your question answered, please come back and mark the reply as an answer.
    If you are helped by an answer to someone else's question, please mark it as helpful.

    Thursday, October 13, 2011 4:26 PM
  • I have created a windows service in C# VS2008 that uses a reference to an external class library for Microsoft.Exchange.WebServices.dll. I have added the reference to it in VS2008 & it is working fine in local. After deployment of service when i click on start it shows error - Error 1053: The service did not respond to the start or control request in a timely fashion.

    Service doesnt start after adding reference dll file

    Regards

    Jagdish


    Jagdish
    Monday, October 17, 2011 11:51 AM
  • hi Henning

    above issue has been resolved

    actually i was instantiating objects of that dll (Microsoft.Exchange.WebServices.dll) on constructor..now i moved the code to
    onstart()...now working fine.

    all working fine as per requirement - i wanted thanks Henning Krause for the kind help.

    i have one more query :)

    1. below we have to pass id & password.  my query is, for security purpose i dont want to pass password.   Is it possible by passing only mailbox name can we achieve all my requiremnts.

    any alternative to hide my password.  

    service.Credentials = new NetworkCredential("jagdish", "password", "domain");

    regards
    Jagdish

    Jagdish
    Wednesday, October 19, 2011 9:56 AM
  • HI Jagdish,

    yes, you can do that. Just set the UseDefaultCredential to true on your service instance and change the user the service runs under to a domain user with appropriate permissions.

    Kind regards,
    Henning

    If you get your question answered, please come back and mark the reply as an answer.
    If you are helped by an answer to someone else's question, please mark it as helpful.

    • Marked as answer by Jagdish Kotian Thursday, October 20, 2011 6:28 AM
    Wednesday, October 19, 2011 11:43 AM
  • Magnificent all working fine - many thanks

    one more question :

    1. i have created one folder "xyz" in inbox, how to find this folder "xyz" & i want to move mail to this folder.

        i am able to find all inbuilt folder (like draft etc) & also i am able move mail to these folder.

    regards

    Jagdish

     


    Jagdish
    Thursday, October 20, 2011 7:12 AM
  • You'll need to use the ExchangeService.FindFolders method to find a non-standard folder.

    Have a look at http://www.infinitec.de/post/2011/10/05/Setting-the-Homepage-of-an-Exchange-folder-using-the-EWS-Managed-API.aspx.

    The code fragment there contains the method SetFolderHomePage. It contains the logic to find a folder.

    Kind regards,
    Henning

    If you get your question answered, please come back and mark the reply as an answer.
    If you are helped by an answer to someone else's question, please mark it as helpful.

    Thursday, October 20, 2011 7:47 AM
  • success :)

     

    FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox, new ItemView(512));

     

    FindFoldersResults findResultsFindFolder = service.FindFolders(WellKnownFolderName.Inbox ,new FolderView(int.MaxValue));

     

    Folder folderToMove = null;

    foreach (Folder folder in findResultsFindFolder)

    {

           //find folder
           if (folder.DisplayName == "xyz")

     

                folderToMove = folder;

           }

    }

    //move item to xyz folder

    findResults.Items[0].Move(folderToMove.Id);

     

     

    Henn, thank you for all your best nd best answer which helped me lot & i learned lot.

     

    With Best Regards

    Jagdish

           {
    Jagdish
    • Marked as answer by Jagdish Kotian Tuesday, November 12, 2013 9:40 AM
    Thursday, October 20, 2011 9:24 AM
  • hi All

    back after long time

    Before we have implemented windows service to access EWS which was working fine.

    Now EWS service have made secure https:// but when i access showing following error please help 

    Microsoft.Exchange.WebServices.Data.ServiceRequestException: The request failed. The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel. ---> System.Net.WebException: The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel. ---> System.Security.Authentication.AuthenticationException: The remote certificate is invalid according to the validation procedure.

    my code is 

    service = new Microsoft.Exchange.WebServices.Data.ExchangeService();
                    service.UseDefaultCredentials = true;

    service.Url = new Uri(ewsURL);

    service.FindItems(WellKnownFolderName.Inbox, new ItemView(512));

    Awaiting for reply

    Regards

    Jagdish


    Jagdish


    Tuesday, November 12, 2013 9:36 AM