使用Exchange Web Services从电子邮件中删除附件

时间:2018-12-20 11:07:59

标签: c# email exchange-server exchangewebservices

我有以下问题。我想从电子邮件中删除附件。只要是带有附件的普通电子邮件,就没问题。但是,如果该电子邮件现在位于电子邮件中,则无法删除附件。我总是收到消息“至少一个附件无法删除”。

有人有什么主意吗?我正在使用Exchange Web Services的版本2。

       private void workEmail(EmailMessage rootMailMessage, EmailMessage subMailMessage, string filePath, int index)
    {

        EmailMessage eMessageToWork = null;
        if (subMailMessage == null)
        {
            eMessageToWork = rootMailMessage;
        }
        else
        {
            eMessageToWork = subMailMessage;
        }

        for (int i = eMessageToWork.Attachments.Count; i-- > 0; )
        {
            Microsoft.Exchange.WebServices.Data.Attachment rootAttachment = eMessageToWork.Attachments[i];

            if (rootAttachment is FileAttachment)
            {
                // For now, just .odt files are not supported and it throws an exception if theres any unsupported fileextension
                checkFileTypeSupported(rootAttachment.Name);

                string strType = Path.GetExtension(rootAttachment.Name);

                // check if it is any type of supported image or pdf file
                if (checkForImageOrPdfAttachment(strType))
                {
                    // just save the image to temp folder
                    string subAttRootFileName = saveImageFileAttachment(rootAttachment, index, filePath);

                    // remove attachment
                    eMessageToWork.Attachments.Remove(rootAttachment);
                    // save the updated mail
                    rootMailMessage.Update(ConflictResolutionMode.AlwaysOverwrite);
                }

                continue;
            }
            else // Attachment is an item attachment.
            {
                // convert attachment to itemattachment
                ItemAttachment itmAttach = rootAttachment as ItemAttachment;

                // save this item-attachment
                // Load Item with additionalProperties of MimeContent
                itmAttach.Load(EmailMessageSchema.MimeContent);

                // convert the itemattachment to a emailmessage
                EmailMessage ebMessage = itmAttach.Item as EmailMessage;

                // recursive call for possible attachments in this emailmessage
                this.workEmail(rootMailMessage, ebMessage, filePath, index + 1);

                // remove the attached mailitem from parent mail
                rootMailMessage.Attachments.Remove(rootAttachment);
                // update parent mail
                rootMailMessage.Update(ConflictResolutionMode.AlwaysOverwrite);
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

尝试一下:

EmailMessage message = EmailMessage.Bind(service, Id, new PropertySet(ItemSchema.Attachments));

    foreach (Attachment attachment in message.Attachments)
    {
            message.Attachments.Remove(attachment);
            break;
    }
    message.Update(ConflictResolutionMode.AlwaysOverwrite);