scriptingexchangewebservicesexchange-server-2010powershell-4.0public-folders

EWS API updating an ItemAttachment


I am trying to remove some TNEF corruption on items in our PF structure. I have run into an issue with TNEF on an attached item.

I can find the item, load it, remove the property, but I am unable to save the attached item.

I get an Exception:

Calling "Update" with "1" argument(s): "This operation isn't supported on attachments."

$MSGID = $_
                    $psPropset = new-object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.ItemSchema]::Attachments)
                    $msMessage = [Microsoft.Exchange.WebServices.Data.EmailMessage]::Bind($exchService,$MSGID,$psPropset)
                    $msMessage.load()

                    "This message has attachments :" + $msMessage.hasattachments
                    " "|out-default

                    foreach($attach in $msMessage.Attachments){
                                "Loading attachments :"
                                $attach.Load()
                                        if ($attach.item.itemclass -eq "IPM.note")
                                            {"Found Attached email Message : Checking for TNEF Corruption on attached Message "
                                            $tnefProp1 = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(0x1204, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::ShortArray)
                                            $tnefProp2 = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(0x1205, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::ShortArray)
                                            $attach.Load($tnefProp1,$tnefProp2);

                                                    $propValue1 = $null
                                                    $propValue2 = $null
                                                    $foundProperty1 = $attach.item.TryGetProperty($tnefProp1, [ref]$propValue1)
                                                    $foundProperty2 = $attach.item.TryGetProperty($tnefProp2, [ref]$propValue2)

                                                    if ($foundProperty1 -or $foundProperty2)
                                                    {
                                                        "TNEF props found on item: " + $attach.item.Subject.ToString()

                                                        if ($Fix)
                                                        {
                                                            "    Removing TNEF properties..."

                                                            if ($foundProperty1)
                                                            {
                                                                $attach.item.RemoveExtendedProperty($tnefProp1) | Out-Null
                                                            }

                                                            if ($foundProperty2)
                                                            {
                                                                $attach.item.RemoveExtendedProperty($tnefProp2) | Out-Null
                                                            }


                                                            $attach.item.Update([Microsoft.Exchange.WebServices.Data.ConflictResolutionMode]::AlwaysOverwrite)




                                                            "    Finished removing TNEF properties from this item."
                                                        }

                                                    }

                                            }
                                          else {"Attachment was not an email"}


                                 }




            $msMessage.update([Microsoft.Exchange.WebServices.Data.ConflictResolutionMode]::AlwaysOverwrite)




        }

        }

Solution

  • You can't edit an attachment in place. You would need to download the attachment, remove it from the item, edit the downloaded copy, then add it as a new attachment.