none
attachments.clear() method is not working on server RRS feed

  • Question

  • Hello, i am trying to send a forward mail to a recipient however i there is a case where i need to remove the existing attachments first and add my own (edit attachments). I tried to remove attachments from the original message first with no success, then i tried to clear the attachments from the reply object as you can see from the code below.Unfortunately, the server(Exchange 2010), includes the original attachments and adds the new ones, that i want to send.

    What could be going wrong?

    PropertySet ps = new PropertySet(BasePropertySet.IdOnly, ItemSchema.Attachments);

    EmailMessage tmpitem = EmailMessage.Bind(_ExchangeService, new ItemId(emailId), ps);

     ResponseMessage responseMessage = tmpitem.CreateForward();

     responseMessage.ToRecipients.Add("abc@abc.com");

      EmailMessage reply = responseMessage.Save();

            //add the new attachments
              reply.Attachments.Clear();
              reply.Update(ConflictResolutionMode.AlwaysOverwrite);          

    foreach (FData fd in FDataList)
              {
                if (fd.binary != null)
                  reply.Attachments.AddFileAttachment(fd.name, fd.binary);
              }
            }

            reply.SendAndSaveCopy(WellKnownFolderName.SentItems);

    Monday, July 16, 2012 2:54 PM

Answers

  • After you have saved the message you should call Load which will load the Attachments Collection on the Reply then when you call clear it will actually have something to clear eg

                EmailMessage reply = responseMessage.Save();
                reply.Load();
                reply.Attachments.Clear();
                reply.Update(ConflictResolutionMode.AlwaysOverwrite);
                reply.SendAndSaveCopy();

    Should work to clear the attachments collection.

    Cheers
    Glen

    • Marked as answer by NikFou Tuesday, July 17, 2012 8:03 AM
    Tuesday, July 17, 2012 7:25 AM