Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ExchangeServiceBinding FindItem does not find the (one) message

Status
Not open for further replies.

GoodDay

Programmer
Nov 17, 2002
15
RU
Hello I have some problem with reading message in some email (inbox) I see this message in inbox (Outlook client), but it is not found by method FindItem. It happens after some experiments with this message, but what I do not understand. Other messages was founded good. There is a simple code on C# (search all messages in inbox) via EWS proxy classes (Exchange 2007)

Code:
private static bool getReceiveMessages(string _email, out ItemType[] messages)
        {
           //for errorLog
            string msgPrefix = "?ethod GetReceiveMessages(" + _email + ")\r\n";

            FindItemType request = new FindItemType();
            request.ItemShape = new ItemResponseShapeType();
            request.ItemShape.BaseShape = DefaultShapeNamesType.AllProperties;
            DistinguishedFolderIdType folder = new  DistinguishedFolderIdType();
            folder.Id = DistinguishedFolderIdNameType.inbox;
            folder.Mailbox = new EmailAddressType();
            folder.Mailbox.EmailAddress = _email;
            request.ParentFolderIds = new BaseFolderIdType[] { folder };
            request.Traversal = ItemQueryTraversalType.Shallow;
            // 
            try
            {
                FindItemResponseType response =binding.FindItem(request);
                FindItemResponseMessageType responseMessage =
                    response.ResponseMessages.Items[0] as FindItemResponseMessageType;
                if (responseMessage.ResponseClass != ResponseClassType.Success)
                {
                    LogWriter.Write(msgPrefix + responseMessage.MessageText);
                    messages = null;
                    return false;
                }
                // get messages 
                messages = (responseMessage.RootFolder.Item as ArrayOfRealItemsType).Items;
            }
            catch (Exception ex)
            {
                //write in log
                //LogWriter.Write(msgPrefix + ex.Message);
                messages = null;
                return false;
            }
            return true;
        }
Why this message is not visible for FindItem and visible for me?
thank you and sorry for poor english!
 
the first problem is you are swallowing the exception. you have your logging code commented out.
second issue. you are only logging the message and not the stack trace.
change you catch block to this
Code:
catch (Exception ex)
{
   LogWriter.Write(ex.ToString()); //no need for prefix now.
   messages = null;
   return false;
}
I would also make two other changes. these changes will not effect the overall outcome. it just makes it easier to work with.
1. change the method signature to avoid the out parameter
2. return an empty array if no items are present or an exception is encountered. now you don't need to check for null.

that would change the code above to
Code:
private ItemType[] getReceiveMessages(string _email)
{
   FindItemType request = new FindItemType();
   request.ItemShape = new ItemResponseShapeType();
   request.ItemShape.BaseShape = DefaultShapeNamesType.AllProperties;
   DistinguishedFolderIdType folder = new  DistinguishedFolderIdType();
   folder.Id = DistinguishedFolderIdNameType.inbox;
   folder.Mailbox = new EmailAddressType();
   folder.Mailbox.EmailAddress = _email;
   request.ParentFolderIds = new BaseFolderIdType[] { folder };
   request.Traversal = ItemQueryTraversalType.Shallow;

   try
   {
      FindItemResponseType response = binding.FindItem(request);
      FindItemResponseMessageType responseMessage =
      response.ResponseMessages.Items[0] as FindItemResponseMessageType;
      if (responseMessage.ResponseClass != ResponseClassType.Success)
      {
         LogWriter.Write(responseMessage.MessageText);
         return ItemType[0];
      }
      return (responseMessage.RootFolder.Item as ArrayOfRealItemsType).Items;
   }
   catch (Exception ex)
   {
      LogWriter.Write(ex.ToString());
      return ItemType[0];
   }
}

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
thanks jmeckley!!
Sorry I forgot uncommented this code I did it
Code:
 catch (Exception ex)
            {
                //write in log
                LogWriter.Write(msgPrefix + ex.Message);
                messages = null;
                return false;
            }
Earlier I looked this situation in a debugger. No errors are present. These blocks are NOT carried out
Code:
if (responseMessage.ResponseClass != ResponseClassType.Success)
....
try
{
}
catch
{
...
}
Method playes successful and array of messages are filled (all input messages, except this message). Filled array not contains this magic message. If i reply this message - the reply copy will be found, but original (magic) message - not
I try to write simple example code as you wrote
Code:
            ExchangeServiceBinding binding = new ExchangeServiceBinding();
            binding.Url = @"[URL unfurl="true"]https://dcex/ews/exchange.asmx";[/URL]
            binding.Credentials = new NetworkCredential("prog", "pwd");
            FindItemType request = new FindItemType();
            request.ItemShape = new ItemResponseShapeType();
            request.ItemShape.BaseShape = DefaultShapeNamesType.AllProperties;
            DistinguishedFolderIdType folder = new DistinguishedFolderIdType();
            folder.Id = DistinguishedFolderIdNameType.inbox;
            folder.Mailbox = new EmailAddressType();
            folder.Mailbox.EmailAddress = @"prog@test.local";
            request.ParentFolderIds = new BaseFolderIdType[] { folder };
            request.Traversal = ItemQueryTraversalType.Shallow;
            ItemType[] messages = null;

            try
            {
                FindItemResponseType response = binding.FindItem(request);
                FindItemResponseMessageType responseMessage =
                response.ResponseMessages.Items[0] as FindItemResponseMessageType;
                if (responseMessage.ResponseClass != ResponseClassType.Success)
                {

                    Console.WriteLine("NO_SUCCESS");

                }
                messages=(responseMessage.RootFolder.Item as ArrayOfRealItemsType).Items;
            }
            catch (Exception ex)
            {

                Console.WriteLine(ex.ToString());
                
            }
Result not changed - this (ONE!!!!) message not founded (((
All other - OK
May be this magic message has some properties .......?
Please help
 
Sorry - attachment in my previous post not readable ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top