This documentation is archived and is not being maintained.

CreateAttachmentType Class

The CreateAttachmentType class represents a request to attach an item or file to a specified item in the Exchange database.

Namespace: ExchangeWebServices
Assembly: EWS (in ews.dll)

[SerializableAttribute] 
[XmlTypeAttribute(Namespace="http://schemas.microsoft.com/exchange/services/2006/messages")] 
[DesignerCategoryAttribute("code")] 
[GeneratedCodeAttribute("wsdl", "2.0.50727.42")] 
[DebuggerStepThroughAttribute] 
public class CreateAttachmentType : BaseRequestType
/** @attribute SerializableAttribute() */ 
/** @attribute XmlTypeAttribute(Namespace="http://schemas.microsoft.com/exchange/services/2006/messages") */ 
/** @attribute DesignerCategoryAttribute("code") */ 
/** @attribute GeneratedCodeAttribute("wsdl", "2.0.50727.42") */ 
/** @attribute DebuggerStepThroughAttribute() */ 
public class CreateAttachmentType extends BaseRequestType
SerializableAttribute 
XmlTypeAttribute(Namespace="http://schemas.microsoft.com/exchange/services/2006/messages") 
DesignerCategoryAttribute("code") 
GeneratedCodeAttribute("wsdl", "2.0.50727.42") 
DebuggerStepThroughAttribute 
public class CreateAttachmentType extends BaseRequestType

You cannot attach an existing item to another item. To attach the contents of an existing item, pass the MimeContent of the existing item to the attachment that you want to create. Make sure that you null out all properties that are returned for an existing item. Only the Multipurpose Internet Mail Extensions (MIME) content should be used in the attachment. If you try to attach an existing item to another item, the response will contain an error because the Exchange database cannot include duplicate item identifiers.


System.Object
   ExchangeWebServices.BaseRequestType
    ExchangeWebServices.CreateAttachmentType

The following code example shows a create attachment request that creates a file and item attachment on an existing item in the Exchange database.

static void CreateAttachment(ExchangeServiceBinding esb)
{
    // Create the item attachment.
    MessageType message = new MessageType();
    message.Subject = "Example subject";
    message.Importance = ImportanceChoicesType.Low;
    message.ImportanceSpecified = true;

    ItemAttachmentType itemAttach = new ItemAttachmentType();
    itemAttach.Name = "Message Attachment Example";
    itemAttach.Item = message;

    // Create the file attachment.
    FileAttachmentType fileAttach = new FileAttachmentType();
    fileAttach.Name = "filename.txt";
    fileAttach.ContentType = "text/plain";

    byte[] content;
    using (FileStream fileStream = new FileStream("C:\\cas.txt",
        FileMode.Open, FileAccess.Read))
    {
        content = new byte[fileStream.Length];
        fileStream.Read(content, 0, content.Length);
    }

    fileAttach.Content = content;

    // Create the CreateAttachment request.
    CreateAttachmentType reqCreateAttach= new CreateAttachmentType();

    // Identify the item that will have the attachments.
    ItemIdType item = new ItemIdType();
    item.Id = "AAAlAE1BQG1h";
    item.ChangeKey = "DwAAABYAAA"; 
    
    // Add the parent item to the request.
    reqCreateAttach.ParentItemId = item;

    // Add attachments to the request.
    reqCreateAttach.Attachments = new AttachmentType[2];
    reqCreateAttach.Attachments[0] = itemAttach;
    reqCreateAttach.Attachments[1] = fileAttach;

    try
    {
        CreateAttachmentResponseType resp = esb.CreateAttachment(reqCreateAttach);
        ArrayOfResponseMessagesType aorit = resp.ResponseMessages;
        ResponseMessageType[] rmta = aorit.Items;

        foreach (ResponseMessageType rmt in rmta)
        {
            if (rmt.ResponseClass == ResponseClassType.Success)
            {
                AttachmentInfoResponseMessageType airmt = rmt as AttachmentInfoResponseMessageType;
                foreach (AttachmentType atch in airmt.Attachments)
                {
                    if (atch is ItemAttachmentType)
                    {
                        Console.WriteLine("Created item attachment");
                    }
                    else if (atch is FileAttachmentType)
                    {
                        Console.WriteLine("Created file attachment");
                    }
                    else
                    {
                        throw new Exception("Unknown attachment");
                    }
                }
            }
        }
    }
    catch (Exception e)
    {
        Console.WriteLine(e.Message);
    }
}

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Development Platforms

Windows XP Professional with Service Pack 2 (SP2), Windows Server 2003,

Target Platforms

Windows 98, Windows 2000, Windows 2000 Server, Windows CE, Windows Longhorn, Windows 98 Second Edition, Pocket PC, Smart Phone, Windows Server 2003, Windows XP Professional with Service Pack 2 (SP2)
Show: