none
Office 365 Web Service - The specified attachment Id is invalid error RRS feed

  • Question

  • Hello all,

    I am facing a problem with attachments. We are using Office 365 mail service. We are getting email one by one from Office 365 Web service and we are parsing each of them to from,to,cc,bcc,subject,body and attachments etc. and creating CRM incidents. Problem is when we are getting some attachments with fileAttachment.Load() function (without parameter) from the mails it gives us error like "The specified attachment Id is invalid.".

    And we don't know why. The point is it's not gives this error every file attachments it happens only some of .eml files.

    I can't add links or image currently.

    Tuesday, August 7, 2018 1:46 PM

Answers

  • Hi,

    I just tested your code, and I think that's your error is "message.Subject.Contains("CRM:0001007432")".

    I changed your code, you can try it.

     
    TimeSpan ts = new TimeSpan(0, 0, -Convert.ToInt32(20), 0);
                DateTime date = DateTime.Now.Add(ts);
                SearchFilter.IsGreaterThanOrEqualTo filter1 = new SearchFilter.IsGreaterThanOrEqualTo(ItemSchema.DateTimeReceived,
                    date);
    
                List<SearchFilter> lstfilter = new List<SearchFilter>();
    
    
                //SearchFilter.ContainsSubstring filter2 = new SearchFilter.ContainsSubstring(ItemSchema.Subject, "test dikkate almayın 2", ContainmentMode.Substring, ComparisonMode.IgnoreCase);
                //lstfilter.Add(filter2);
    
    
                lstfilter.Add(filter1);
    
                SearchFilter.SearchFilterCollection filter = new SearchFilter.SearchFilterCollection(Microsoft.Exchange.WebServices.Data.LogicalOperator.And, lstfilter);
    
                if (exchange != null)
                {
                    FindItemsResults<Item> findResults = exchange.FindItems(WellKnownFolderName.Inbox, filter, new ItemView(200));
    
                    for (int iz = 0; iz < findResults.Items.Count; iz++)
                    {
                        EmailMessage item = findResults.Items[iz] as EmailMessage;
                        if (item.ConversationTopic.Contains("CRM:0001007432"))
                        {
    
                            try
                            {
                                EmailMessage message = EmailMessage.Bind(exchange, item.Id, new PropertySet(BasePropertySet.IdOnly, ItemSchema.Attachments));
    
    
                                //if (message.From.Address.Equals("hkaratas@evyapport.com"))
                                //{
                               // if (message.Subject.Contains("CRM:0001007432"))
                                //{
                                    Console.WriteLine("We Found mail");
                                    // .eml file
                                    if (message.HasAttachments && message.Attachments[0] is FileAttachment)
                                    {
                                        FileAttachment fileAttachment = message.Attachments[0] as FileAttachment;
                                        fileAttachment.Load();
                                        Console.WriteLine("FileName: " + fileAttachment.Name);
                                    }
                               // }
    
                                //}
                            }
                            catch (Exception e)
                            {
                                Console.WriteLine(e.Message);
                            }
                        }
                    }
                }

    Hope can give you a new idea. Looking forward to your updates and feedback.

    Thanks,

    Simon


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread.

    Wednesday, August 8, 2018 12:02 PM
    Moderator

All replies

  • Hi Harun,

    Could you please post some related codes for the issue? However, you can reference my test code, I jus tested .eml file attachments without problems.

     TimeSpan ts = new TimeSpan(0, -3, 0, 0);
                DateTime date = DateTime.Now.Add(ts);
                SearchFilter.IsGreaterThanOrEqualTo filter = new SearchFilter.IsGreaterThanOrEqualTo(ItemSchema.DateTimeReceived, date);
                FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox, filter, new ItemView(50));
    
                foreach (Item item in findResults)
                {
                    EmailMessage message = EmailMessage.Bind(service, item.Id, new PropertySet(BasePropertySet.IdOnly, ItemSchema.Attachments));
    
                    // .eml file
                    if (message.HasAttachments && message.Attachments[0] is FileAttachment)
                    {
                        FileAttachment fileAttachment = message.Attachments[0] as FileAttachment;
                        fileAttachment.Load();
                        Console.WriteLine("FileName: " + fileAttachment.Name);
                    }
    
                }

    Hope it works for you.

    Thanks,

    Simon


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread.

    Wednesday, August 8, 2018 9:29 AM
    Moderator
  • Hello Simon,

    When i use your test code it gives me this error : "You must load or assign this property before you can read its value."

    Here demonstration of my original code

    TimeSpan ts = new TimeSpan(0, 0, -Convert.ToInt32(20), 0);
                DateTime date = DateTime.Now.Add(ts);
                SearchFilter.IsGreaterThanOrEqualTo filter1 = new SearchFilter.IsGreaterThanOrEqualTo(ItemSchema.DateTimeReceived,
                    date);
    
                List<SearchFilter> lstfilter = new List<SearchFilter>();
    
    
                //SearchFilter.ContainsSubstring filter2 = new SearchFilter.ContainsSubstring(ItemSchema.Subject, "test dikkate almayın 2", ContainmentMode.Substring, ComparisonMode.IgnoreCase);
                //lstfilter.Add(filter2);
    
    
                lstfilter.Add(filter1);
    
                SearchFilter.SearchFilterCollection filter = new SearchFilter.SearchFilterCollection(Microsoft.Exchange.WebServices.Data.LogicalOperator.And, lstfilter);
    
                if (exchange != null)
                {
                    FindItemsResults<Item> findResults = exchange.FindItems(WellKnownFolderName.Inbox, filter, new ItemView(200));
    
                    for (int iz = 0; iz < findResults.Items.Count; iz++)
                    {
                        Item item = findResults.Items[iz];
    
                        try
                        {
                            EmailMessage message = EmailMessage.Bind(exchange, item.Id);
    
                           
                            //if (message.From.Address.Equals("hkaratas@evyapport.com"))
                            //{
                                if (message.Subject.Contains("CRM:0001007432"))
                                {
                                    Console.WriteLine("We Found mail");
                                // .eml file
                                    if (message.HasAttachments && message.Attachments[0] is FileAttachment)
                                    {
                                        FileAttachment fileAttachment = message.Attachments[0] as FileAttachment;
                                        fileAttachment.Load();
                                        Console.WriteLine("FileName: " + fileAttachment.Name);
                                    }
                            }
    
                            //}
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e.Message);
                        }
                    }
                }


    In my original code almost same as yours. Only difference is 

    new PropertySet(BasePropertySet.IdOnly, ItemSchema.Attachments)

    this part. when i added this part on my code it gives me "You must load or assign this property before you can read its value." error.

    Thanks,

    Harun

    Wednesday, August 8, 2018 10:59 AM
  • Hi,

    I just tested your code, and I think that's your error is "message.Subject.Contains("CRM:0001007432")".

    I changed your code, you can try it.

     
    TimeSpan ts = new TimeSpan(0, 0, -Convert.ToInt32(20), 0);
                DateTime date = DateTime.Now.Add(ts);
                SearchFilter.IsGreaterThanOrEqualTo filter1 = new SearchFilter.IsGreaterThanOrEqualTo(ItemSchema.DateTimeReceived,
                    date);
    
                List<SearchFilter> lstfilter = new List<SearchFilter>();
    
    
                //SearchFilter.ContainsSubstring filter2 = new SearchFilter.ContainsSubstring(ItemSchema.Subject, "test dikkate almayın 2", ContainmentMode.Substring, ComparisonMode.IgnoreCase);
                //lstfilter.Add(filter2);
    
    
                lstfilter.Add(filter1);
    
                SearchFilter.SearchFilterCollection filter = new SearchFilter.SearchFilterCollection(Microsoft.Exchange.WebServices.Data.LogicalOperator.And, lstfilter);
    
                if (exchange != null)
                {
                    FindItemsResults<Item> findResults = exchange.FindItems(WellKnownFolderName.Inbox, filter, new ItemView(200));
    
                    for (int iz = 0; iz < findResults.Items.Count; iz++)
                    {
                        EmailMessage item = findResults.Items[iz] as EmailMessage;
                        if (item.ConversationTopic.Contains("CRM:0001007432"))
                        {
    
                            try
                            {
                                EmailMessage message = EmailMessage.Bind(exchange, item.Id, new PropertySet(BasePropertySet.IdOnly, ItemSchema.Attachments));
    
    
                                //if (message.From.Address.Equals("hkaratas@evyapport.com"))
                                //{
                               // if (message.Subject.Contains("CRM:0001007432"))
                                //{
                                    Console.WriteLine("We Found mail");
                                    // .eml file
                                    if (message.HasAttachments && message.Attachments[0] is FileAttachment)
                                    {
                                        FileAttachment fileAttachment = message.Attachments[0] as FileAttachment;
                                        fileAttachment.Load();
                                        Console.WriteLine("FileName: " + fileAttachment.Name);
                                    }
                               // }
    
                                //}
                            }
                            catch (Exception e)
                            {
                                Console.WriteLine(e.Message);
                            }
                        }
                    }
                }

    Hope can give you a new idea. Looking forward to your updates and feedback.

    Thanks,

    Simon


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread.

    Wednesday, August 8, 2018 12:02 PM
    Moderator
  • Hi Harun,

    Could you please update for this? I wanna to confirm that this issue still persists for my new code?

    I look forward to receiving your feedback. 

    Thanks,

    Simon


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread.

    Thursday, August 9, 2018 6:40 AM
    Moderator