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
Comments
Anonymous
February 17, 2008
Thanks for your codes. However I still have a issue not solved. After you had added some attachments to the Item,what should I do if i want to send the item?Anonymous
February 20, 2008
I am not able to understand your question, this code add attachment to an existing item... do you want to send the existing item or want to create a new item and send it to people ?Anonymous
February 04, 2009
How do I send an email with attachments in a single request-response operation?Anonymous
August 09, 2010
I ran into an interesting issue recently and am at a loss how to fix the issue so I'm glad if you could help me find a fix for it. I removed an attachment from an email using EWS' DeleteAttachment function and later added the same attachment file using CreateAttachment function. The original attachment size was 2.2MB but after creating the same attachment again the size bumped up to 4MB. The file I used was a pdf and then repeated the experiment with different types of attachments (jpg, doc, txt etc) and found out everytime I add through EWS the size gets almost doubled. Although the binary contents are different I didn't find any difference in contents, ie. pdf's opened fine on acrobat and had the same contents and jpegs opened fine etc. etc. I suspect there's some sort of an internal compression taking place in Outlook and OWA that EWS misses as that is the only logical explanation I can think of. Do you know any way I can re-include the same attachment file without increasing the size of the file? Thanks Duke