July, 2012

  • Akash Blogging......

    EWS Managed API is lying to me!

    • 0 Comments

    I have been working with Exchange Web Services(EWS) for a long time and this is the first time I saw that the EWS was lying to me! The code was executing a FindItem call on a list of folders and all of a sudden it threw the following error: “Error occurred: The request failed. The remote server returned an error: (401) Unauthorized”.

    Why would that happen? If it was an authentication problem then why did my previous call work? The first step was to check the IIS Logs on the CAS server and below is what we find:

    7/11/2012  10.171.230.40    POST /EWS/Exchange.asmx    SoapAction=FindItem 443    ExchangeServicesClient/14.03.0067.001    200     63025
    7/11/2012 10.171.230.40 POST /EWS/Exchange.asmx SoapAction=FindItem 443 ExchangeServicesClient/14.03.0067.001 200 21809
    7/11/2012 10.171.230.40 POST /EWS/Exchange.asmx SoapAction=FindItem 443 ExchangeServicesClient/14.03.0067.001 302 196752
    7/11/2012 10.171.230.40 POST /EWS/Exchange.asmx SoapAction=FindItem 443 ExchangeServicesClient/14.03.0067.001 302 196643

    I chose to show only the relevant columns above. I you notice the highlighted values, the 302 is the HTTP Status code which means that the object has been temporarily moved. The last column is the time taken in milliseconds to execute the FindItem request. In the above case it was more than 3 minutes which is not good and means we have a LOTS of scope to optimize the code.

    But, why do I get the 302? I turned my attention to the Event Logs on the CAS box and I did not have to search too much. Below is what I saw:

    Log Name:      Application
    Source: ASP.NET 2.0.50727.0
    Date: 7/11/2012 12:58:05 PM
    Event ID: 1309
    Task Category: Web Event
    Level: Warning
    Keywords: Classic
    User: N/A
    Computer: myserver.contoso.com
    Description:
    Event code: 3001
    Event message: The request has been aborted.
    Event time: 7/11/2012 12:58:05 PM
    Event time (UTC): 7/11/2012 4:58:05 PM
    Event ID: 38a627eada914200923e244e9c83f7c8
    Event sequence: 6230694
    Event occurrence: 37
    Event detail code: 0

    Application information:
    Application domain: /LM/W3SVC/1/ROOT/EWS-1-129851299999841294
    Trust level: Full
    Application Virtual Path: /EWS
    Application Path: C:\Program Files\Microsoft\Exchange Server\ClientAccess\exchweb\EWS\
    Machine name: myserver

    Process information:
    Process ID: 1152
    Process name: w3wp.exe
    Account name: NT AUTHORITY\SYSTEM

    Exception information:
    Exception type: HttpException
    Exception message: Request timed out.

    Request information:
    Request URL: https://myserver.contoso.com:443/EWS/Exchange.asmx
    Request path: /EWS/Exchange.asmx
    User host address: 10.171.230.40
    User: DOMAIN\Username
    Is authenticated: True
    Authentication Type: Negotiate
    Thread account name: NT AUTHORITY\SYSTEM

    Thread information:
    Thread ID: 26
    Thread account name: NT AUTHORITY\SYSTEM
    Is impersonating: False
    Stack trace:

    Now the picture is clearer! Since the request was running for more that 110 seconds which is default timeout in ASP.NET 2.0, the request was being aborted, which resulted in a 302 in the IIS logs. Now why did EWS Managed API return 401, frankly I did not bother to find!

    To validate the cause we went ahead and increased the executionTimeout from 110 to 240 in the Web.Config for EWS and then we did not see the issue any more. This was done for TESTING only and is definitely not recommended.

    There are two possible solutions to this issue:
    1) Optimize your code and use filters and paging while doing FindItems (Recommended).
    2) Increase the executionTimeout setting in the Web.Config(Not Recommended – can be tried only If optimization does not work ).

    Enjoy!

  • Akash Blogging......

    Unable to send Bcc using System.Net.Mail when specifying a Pickup Directory(Exchange 2007/Exchange 2010) in code

    • 0 Comments

    Not sure developers using System.Net.Mail are not complaining about this issue? The issue is very easy to reproduce, all you need to do is create a new mail with someone on Bcc and drop it to the Pickup folder In Exchange. Most likely not to many people use Bcc or use Port instead of Pickup.

    MailMessage message = new MailMessage("User1@mycompany.com", "User2@mycompany.com", "Sent Using Pickup", "This Message was sent using Pickup");
    message.Bcc.Add(new MailAddress("User3@mycompany.com"));
    SmtpClient client = new SmtpClient();
    client.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
    client.PickupDirectoryLocation = @"C:\Program Files\Microsoft\Exchange Server\TransportRoles\Pickup";
    client.Send(message);

    If we send a mail using the above code we would expect that the mail gets delivered to the Bcc user i.e User3@mycompany.com, but that does not happen! Why would that happen? I then tried to use CDOSYS to create a mail and added Bcc to it and this time the mail was delivered to User3@mycompany.com.

    What could the difference be? I stopped the Transport service on my Exchange 2007(installed on Windows 2003) so that the mail do not get delivered by the transport and then examined the .eml to check what the difference could be. Below is what I find:

    System.Net.Mail uses X-Headers to add the Recipient information to the mail.Below is what the MIME look like on my Windows 2003 box:

    X-Sender: User1@mycompany.com
    X-Receiver: User2@mycompany.com
    X-Receiver: User3@mycompany.com <- This is the BCC
    MIME-Version: 1.0
    From: User1@mycompany.com
    To: User2@mycompany.com
    Date: 19 Jul 2012 13:53:05 -0400
    Subject: Sent Using Pickup
    Content-Type: text/plain; charset=us-ascii
    Content-Transfer-Encoding: quoted-printable

    This Message was sent using Pickup

    Below is what the MIME looks like when I used CDOSYS on my Windows 2003 box:
    thread-index: Ac1l181oBFqzycOSRFaCD7mjVftgqw==
    Thread-Topic: This is a test CDOSYS message (Sent via Pickup)
    From: <User1@mycompany.com>
    To: <User2@mycompany.com>
    Bcc: <User3@mycompany.com>
    Subject: This is a test CDOSYS message (Sent via Pickup)
    Date: Thu, 19 Jul 2012 13:56:20 -0400
    Message-ID: <D76CF8D778AE43A096B10F2281A9914F@mycompany.com>
    MIME-Version: 1.0
    ...

    If you notice, the above MIME does not use X-Headers, it uses the Bcc header and it works. Unfortunately, this behavior of CDOSYS could be different on different environments. When I tested on a Windows 7 box, the MIME looks very similar to what we get from System.Net.Mail(uses X-Header).
    Based on my discussion with the Exchange guys, it turns out that when the mail is put in the Pickup folder, it does not look at the X-Headers and uses the From, To, Bcc headers if present.
     
    The simplest solution was to drop the mail in the Replay folder instead of the Pickup folder.
    MailMessage message = new MailMessage("User1@mycompany.com", "User2@mycompany.com", "Sent Using Pickup", "This Message was sent using Pickup");
    message.Bcc.Add(new MailAddress("User3@mycompany.com"));
    SmtpClient client = new SmtpClient();
    client.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
    client.PickupDirectoryLocation = @"C:\Program Files\Microsoft\Exchange Server\TransportRoles\Replay";
    client.Send(message);
     
    Below is an article that talks about both the directories, although it does not mention that X-Headers are ignored in the pickup, it does explicitly say that Replay makes use of the X-Headers.

    Understanding the Pickup and Replay Directories
    http://technet.microsoft.com/en-us/library/bb124230.aspx

    I have tested this on Exchange 2007 installed on Windows 2003 and it works!
    Enjoy!


Page 1 of 1 (2 items)