HOWTO: EWS: Add attachments to existing items on server
Forgot to add that attachment? Using exchange web service you can add it back to the item on the server. The code is simple and very easy to understand. It takes two parameters itemID – where attachment will be added, & strFileName – complete path to the local file.
private bool AddAttachment(String itemID,String strFileName)
{
ExchangeServiceBinding esb = new ExchangeServiceBinding();
esb.Credentials = new System.Net.NetworkCredential("username", "password", "domain");
esb.Url = "https://exchange-cas-server/ews/exchange.asmx";
//byte array to hold the attachment content
byte[] bArray;
//read the content from file into a byte array
using (Stream fStream = new FileStream(strFileName, FileMode.Open))
{
int length;
bArray = new byte[fStream.Length];
fStream.Seek(0, SeekOrigin.Begin);
length = fStream.Read(bArray, 0, (int)fStream.Length);
}
FileInfo fi = new FileInfo(strFileName);
//create and populate the FileAttachment
FileAttachmentType[] fata = new FileAttachmentType[1];
fata[0] = new FileAttachmentType();
fata[0].Name = fi.Name;
fata[0].Content = bArray;
//create the CreateAttachmentType object and make the CreateAttachment request
CreateAttachmentType cat = new CreateAttachmentType();
cat.ParentItemId = new ItemIdType();
cat.ParentItemId.Id = itemID;
cat.Attachments = fata;
CreateAttachmentResponseType cart = esb.CreateAttachment(cat);
ResponseMessageType rmt = ((ResponseMessageType)cart.ResponseMessages.Items[0]);
if (rmt.ResponseClass == ResponseClassType.Success)
return true;
else
return false;
}
Keywords: CreateAttachment, Exchange Web Services, add attachments