Exchange で EWS を使用して添付物を削除する

Exchange で EWS マネージ API または EWS を使用して、EWS アイテムから添付物を削除する方法について説明します。

EWS マネージ API を使用して、アイテムからファイルおよびアイテム添付を削除する場合、いくつかのオプションがあります。 メッセージからすべての添付物を削除して、ファイル名による削除やコレクション内の位置による削除を行うことができます。 これらのオプションそれぞれについて、AttachmentCollection メソッドがあります。

一方、EWS を使用して、アイテムからすべての添付物を削除する場合も、1 つだけ削除する場合も、一連の操作は同じです。 EWS マネージ API とは異なり、EWS には添付物の配列内の名前または位置に基づいて削除するための別の操作はありません。

表 1. 添付物を削除するための EWS マネージ API メソッドと EWS 操作

タスク EWS マネージ API メソッド EWS 操作
アイテムからすべての添付物を削除します。
Item.Bind、その後に AttachmentCollection.Clear、その後に EmailMessage.Update
GetItem、その後に DeleteAttachment
名前でアイテムから添付物を削除します。
Item.Bind、その後に AttachmentCollection.Remove、その後に EmailMessage.Update
GetItem、その後に DeleteAttachment
コレクション内の位置でアイテムから添付物を削除します。
Item.Bind、その後に AttachmentCollection.RemoveAt、その後に EmailMessage.Update
GetItem、その後に DeleteAttachment

EWS マネージ API を使用して、電子メールからすべての添付物を削除する

次のコード例は、電子メールから添付物をすべて削除する方法を示しています。

  1. EmailMessage.Bind メソッドを使用して、既存の電子メール メッセージにバインドし、Attachments のコレクションを取得します。

  2. AttachmentCollection.Clear メソッドを使用して、電子メールからすべての添付物を削除します。

  3. EmailMessage.Update メソッドを使用して、変更を保存します。

この例では、service が有効な ExchangeService オブジェクトであり、itemId が添付物を削除するメッセージの ItemId であり、ユーザーが Exchange Server に認証されたと想定しています。

public static void DeleteAllAttachments(ExchangeService service, ItemId itemId)
{
    // Bind to an existing message by using its item ID and requesting its attachments collection.
    // This method results in a GetItem call to EWS.
    EmailMessage message = EmailMessage.Bind(service, itemId, new PropertySet(ItemSchema.Attachments));
    // Delete all attachments from the message.
    message.Attachments.Clear();
    // Save the updated message.
    // This method results in an DeleteAttachment call to EWS.
    message.Update(ConflictResolutionMode.AlwaysOverwrite);
}

EWS マネージ API を使用して、電子メールから名前ですべての添付物を削除する

次のコード例では、名前で添付物を削除する方法を示しています。

  1. EmailMessage.Bind メソッドを使用して、既存の電子メール メッセージにバインドし、Attachments のコレクションを取得します。

  2. AttachmentCollection.Remove メソッドを使用して、FileAttachment.txt という名前の添付物を削除します。

  3. EmailMessage.Update メソッドを使用して、変更を保存します。

この例では、service が有効な ExchangeService オブジェクトであり、itemId が添付物を削除するメッセージの ItemId であり、ユーザーが Exchange Server に認証されたと想定しています。

public static void DeleteNamedAttachments(ExchangeService service, ItemId itemId)
{
    // Bind to an existing message by using its item ID and requesting its attachments collection.
    // This method results in a GetItem call to EWS.
    EmailMessage message = EmailMessage.Bind(service, itemId, new PropertySet(ItemSchema.Attachments));
    // Iterate through the attachments collection and delete the attachment named "FileAttachment.txt," if it exists.
    foreach (Attachment attachment in message.Attachments)
    {
        if (attachment.Name == "FileAttachment.txt")
        {
            message.Attachments.Remove(attachment);
            break;
        }
    }
    // Save the updated message.
    // This method results in an DeleteAttachment call to EWS.
    message.Update(ConflictResolutionMode.AlwaysOverwrite);
}

EWS マネージ API を使用して位置で添付物を削除する

次のコード例は、添付物を位置で削除する方法を示しています。

  1. EmailMessage.Bind メソッドを使用して、既存の電子メール メッセージにバインドし、Attachments のコレクションと EmailMessage.HasAttachments プロパティを取得します。

  2. AttachmentCollection.Remove メソッドを使用して、コレクションの最初の添付物を削除します。

  3. EmailMessage.Update メソッドを使用して、変更を保存します。

この例では、service が有効な ExchangeService オブジェクトであり、itemId が添付物を削除するメッセージの ItemId であり、ユーザーが Exchange Server に認証されたと想定しています。

public static void DeleteAttachmentByPosition(ExchangeService service, ItemId itemId)
{
    // Bind to an existing message by using its item ID and requesting the HasAttachments property and the attachments collection.
    // This method results in a GetItem call to EWS.
    EmailMessage message = EmailMessage.Bind(service, itemId, new PropertySet(EmailMessageSchema.HasAttachments, ItemSchema.Attachments));
    // Remove attachments using the zero-based index position of the attachment in the attachments collection.
    if (message.HasAttachments)
    {
        message.Attachments.RemoveAt(0);
    }
    // Save the updated message.
    // This method results in an DeleteAttachment call to EWS.
    message.Update(ConflictResolutionMode.AlwaysOverwrite);
}

EWS を使用してアイテムから添付物を削除する

EWS を使用して添付物を削除するには、最初にメッセージと添付物のコレクションを取得して、削除する添付物の AttachmentId (GetAttachment と DeleteAttachment) を取得します。 削除する 1 つ以上の AttachmentId の値を指定してから、DeleteAttachment 操作を呼び出して指定された添付物をメッセージから削除します。

次のコード例は、GetItem 操作を使用して電子メール メッセージとメッセージの添付物のコレクションを取得する方法を示しています。 これは、EWS マネージ API を使用して電子メールからすべての添付物を削除するときに、EWS マネージ API が送信する最初の XML 要求でもあります。 読みやすくするため、一部の属性の値が短縮されています。

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages"
               xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types"
               xmlns:soap="https://schemas.xmlsoap.org/soap/envelope/">
  <soap:Header>
    <t:RequestServerVersion Version="Exchange2007_SP1" />
    <t:TimeZoneContext>
      <t:TimeZoneDefinition Id="Central Standard Time" />
    </t:TimeZoneContext>
  </soap:Header>
  <soap:Body>
    <m:GetItem>
      <m:ItemShape>
        <t:BaseShape>IdOnly</t:BaseShape>
        <t:AdditionalProperties>
          <t:FieldURI FieldURI="item:Attachments" />
        </t:AdditionalProperties>
      </m:ItemShape>
      <m:ItemIds>
        <t:ItemId Id="uqE1AAA=" />
      </m:ItemIds>
    </m:GetItem>
  </soap:Body>
</soap:Envelope>

サーバーは、 GetItem 要求に GetItemResponse メッセージで応答します。このメッセージには、電子メールが正常に作成されたことを示す NoErrorResponseCode 値 、および既存の添付物の AttachmentId が含まれます。

<?xml version="1.0" encoding="utf-8"?>
<s:Envelope xmlns:s="https://schemas.xmlsoap.org/soap/envelope/">
  <s:Header>
    <h:ServerVersionInfo MajorVersion="15"
                         MinorVersion="0"
                         MajorBuildNumber="939"
                         MinorBuildNumber="12"
                         Version="V2_11"
                         xmlns:h="http://schemas.microsoft.com/exchange/services/2006/types"
                         xmlns="http://schemas.microsoft.com/exchange/services/2006/types"
                         xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" />
  </s:Header>
  <s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <m:GetItemResponse xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages"
                       xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">
      <m:ResponseMessages>
        <m:GetItemResponseMessage ResponseClass="Success">
          <m:ResponseCode>NoError</m:ResponseCode>
          <m:Items>
            <t:Message>
              <t:ItemId Id="uqE1AAA="
                        ChangeKey="CQAAABYAAAAFI5DJmZv+TLtyLOLIF1S5AAAXulcd" />
              <t:Attachments>
                <t:FileAttachment>
                  <t:AttachmentId Id="IpHLObE=" />
                  <t:Name>FileAttachment.txt</t:Name>
                </t:FileAttachment>
                <t:FileAttachment>
                  <t:AttachmentId Id="QuHSSmY=" />
                  <t:Name>SecondAttachment.txt</t:Name>
                </t:FileAttachment>
                <t:FileAttachment>
                  <t:AttachmentId Id="qf2KoPo=" />
                  <t:Name>ThirdAttachment.jpg</t:Name>
                </t:FileAttachment>
                <t:FileAttachment>
                  <t:AttachmentId Id="NFQMnMc=" />
                  <t:Name>FourthAttachment.txt</t:Name>
                </t:FileAttachment>
                <t:ItemAttachment>
                  <t:AttachmentId Id="jJvbLXQ=" />
                  <t:Name>Attached Message Item</t:Name>
                </t:ItemAttachment>
              </t:Attachments>
            </t:Message>
          </m:Items>
        </m:GetItemResponseMessage>
      </m:ResponseMessages>
    </m:GetItemResponse>
  </s:Body>
</s:Envelope>

どの添付物を削除するか判別し、DeleteAttachment 操作を呼び出し、削除する添付物の AttachmentId 値を含めます。

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages"
               xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types"
               xmlns:soap="https://schemas.xmlsoap.org/soap/envelope/">
  <soap:Header>
    <t:RequestServerVersion Version="Exchange2007_SP1" />
    <t:TimeZoneContext>
      <t:TimeZoneDefinition Id="Central Standard Time" />
    </t:TimeZoneContext>
  </soap:Header>
  <soap:Body>
    <m:DeleteAttachment>
      <m:AttachmentIds>
        <t:AttachmentId Id="IpHLObE=" />
        <t:AttachmentId Id="QuHSSmY=" />
        <t:AttachmentId Id="qf2KoPo=" />
        <t:AttachmentId Id="NFQMnMc=" />
        <t:AttachmentId Id="jJvbLXQ=" />
      </m:AttachmentIds>
    </m:DeleteAttachment>
  </soap:Body>
</soap:Envelope>

サーバーは、DeleteAttachment 要求に DeleteAttachmentResponse メッセージで応答します。このメッセージには、それぞれの添付物が正常に削除されたことを示す、DeleteAttachmentResponseMessage ごとの NoErrorResponseCode 値が含まれます。 読みやすくするため、一部の属性の値が短縮されています。

<?xml version="1.0" encoding="utf-8"?>
<s:Envelope xmlns:s="https://schemas.xmlsoap.org/soap/envelope/">
  <s:Header>
    <h:ServerVersionInfo MajorVersion="15"
                         MinorVersion="0"
                         MajorBuildNumber="939"
                         MinorBuildNumber="12"
                         Version="V2_11"
                         xmlns:h="http://schemas.microsoft.com/exchange/services/2006/types"
                         xmlns="http://schemas.microsoft.com/exchange/services/2006/types"
                         xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" />
  </s:Header>
  <s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <m:DeleteAttachmentResponse xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages"
                                xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">
      <m:ResponseMessages>
        <m:DeleteAttachmentResponseMessage ResponseClass="Success">
          <m:ResponseCode>NoError</m:ResponseCode>
          <m:RootItemId RootItemId="uqE1AAA=" RootItemChangeKey="AAAXulck" />
        </m:DeleteAttachmentResponseMessage>
        <m:DeleteAttachmentResponseMessage ResponseClass="Success">
          <m:ResponseCode>NoError</m:ResponseCode>
          <m:RootItemId RootItemId="uqE1AAA=" RootItemChangeKey="AAAXulck" />
        </m:DeleteAttachmentResponseMessage>
        <m:DeleteAttachmentResponseMessage ResponseClass="Success">
          <m:ResponseCode>NoError</m:ResponseCode>
          <m:RootItemId RootItemId="uqE1AAA=" RootItemChangeKey="AAAXulck" />
        </m:DeleteAttachmentResponseMessage>
        <m:DeleteAttachmentResponseMessage ResponseClass="Success">
          <m:ResponseCode>NoError</m:ResponseCode>
          <m:RootItemId RootItemId="uqE1AAA=" RootItemChangeKey="AAAXulck" />
        </m:DeleteAttachmentResponseMessage>
        <m:DeleteAttachmentResponseMessage ResponseClass="Success">
          <m:ResponseCode>NoError</m:ResponseCode>
          <m:RootItemId RootItemId="uqE1AAA=" RootItemChangeKey="AAAXulck" />
        </m:DeleteAttachmentResponseMessage>
      </m:ResponseMessages>
    </m:DeleteAttachmentResponse>
  </s:Body>
</s:Envelope>

関連項目