none
create item with attachment RRS feed

  • Question

  • Hi,

    I'm using exchange webservices api (not the managed apis).

    And I'm creating items in batch mode. 

               
               createItemRequest.Items.Items = new ItemType[readytosendList.Count()];
    
                for (int i = 0; i < readytosendList.Count(); i++)
                {
                    MessageType message = SetupEmailMessageEntry(readytosendList[i]);
                    createItemRequest.Items.Items[i] = message;
                }
              
                try
                {
                    // Send the request to create and send the e-mail item, and get the response.
                    CreateItemResponseType createItemResponse = _EwsBinding.CreateItem(createItemRequest);
                    ArrayOfResponseMessagesType responses = createItemResponse.ResponseMessages;
    

    Now, I'm willing to add attachments to some items.

    I'm trying the following code but it doesn't work:

             try
                {
                    if (mailMessage.HasAttachment == true)
                    {
                        if (mailMessage.Attachments.Count > 0)
                        {
                            i = 0;
                            message.HasAttachments = true;
                            message.HasAttachmentsSpecified = true;
                            message.Attachments = new AttachmentType[mailMessage.Attachments.Count];
                            foreach (var item in mailMessage.Attachments)
                            {
                                FileStream fsFileStream = new FileStream(item.Filename, System.IO.FileMode.Open, System.IO.FileAccess.Read);
                                byte[] bdBinaryData = new byte[fsFileStream.Length];
                                long brBytesRead = fsFileStream.Read(bdBinaryData, 0, (int)fsFileStream.Length);
                                fsFileStream.Close();
                                FileAttachmentType faFileAttach = new FileAttachmentType();
                                faFileAttach.Content = bdBinaryData;
                                faFileAttach.Name = item.Filename;
                                message.Attachments[i] = faFileAttach;
                            }
                        }
                    }
                }
                catch (Exception exc)
                {
                    System.Diagnostics.Trace.WriteLine("Error in Exchange AddFileAttachment : " + exc.Message);
                }        

    I've read Glen's blog : http://gsexdev.blogspot.fr/2007/01/sending-attachments-via-exchange-web.html

    None of his solutions works for me.

    sol 1. he is using managed api. I tried too and it works, but I cannot send items by batch (with exchange throttling, it takes ages to send hundred of items).

    sol 2. He creates an item. Then attach a doc, then send. It doesn't work either as we are sending more than one item at a time.

    Any suggestion or sample code showing how to save an item with an attachment using CreateItemRequest and MessageType?

    Thank you very much,

    Emmanuel.


    Cordialement,
    Emmanuel Dreux
    http://www.bcpsoft.fr
    Formation FIM 2010

    Friday, August 10, 2012 2:46 PM

Answers

  • Just to clarify

    Sol 1. Doesn't use the Managed API it use CDOSYS to create the MIME stream for the message you want to send you could use any MIME encoding library to do this. This is the only solution where you can create and send a message with attachment in one webservice call otherwise you always need to save then send (which you could batch just batch the create and then batch send).

       Throttling is always going to be an issue (no matter the API) for you if your trying to batch send a large volume of Items because throttling is biased towards the normal actions of a user. Specifically the MesssageRate Limit "The MessageRateLimit policy specifies the number of messages per minute that can be submitted by any Exchange client, including EWS. By default, this policy is set to 30 messages per minute. For ordinary users, this is generally sufficient. However, applications that send large batches of email messages, for example as part of an invoicing program, can run into problems. When this policy limit is exceeded, message delivery for the mailbox is delayed." http://msdn.microsoft.com/en-us/library/exchange/hh881884(v=exchg.140).aspx

    You may find that you need to disable throttling or it you can't (eg hosted Exchange) look at using SMTP instead

    Cheers
    Glen

    • Marked as answer by ilinfo Wednesday, August 15, 2012 11:32 AM
    Monday, August 13, 2012 6:21 AM

All replies

  • Just to clarify

    Sol 1. Doesn't use the Managed API it use CDOSYS to create the MIME stream for the message you want to send you could use any MIME encoding library to do this. This is the only solution where you can create and send a message with attachment in one webservice call otherwise you always need to save then send (which you could batch just batch the create and then batch send).

       Throttling is always going to be an issue (no matter the API) for you if your trying to batch send a large volume of Items because throttling is biased towards the normal actions of a user. Specifically the MesssageRate Limit "The MessageRateLimit policy specifies the number of messages per minute that can be submitted by any Exchange client, including EWS. By default, this policy is set to 30 messages per minute. For ordinary users, this is generally sufficient. However, applications that send large batches of email messages, for example as part of an invoicing program, can run into problems. When this policy limit is exceeded, message delivery for the mailbox is delayed." http://msdn.microsoft.com/en-us/library/exchange/hh881884(v=exchg.140).aspx

    You may find that you need to disable throttling or it you can't (eg hosted Exchange) look at using SMTP instead

    Cheers
    Glen

    • Marked as answer by ilinfo Wednesday, August 15, 2012 11:32 AM
    Monday, August 13, 2012 6:21 AM
  • Thanks you,

    it works. Indeed example 1 was not using the managed apis.

    Finally, I did not batch items containing attachements, as I'm getting lots of timeouts when size of data to send increase using the batch apis.

    Therefore now, I batching items that do not contains attachments and sending mails with attachments 1 by 1.

    I have to deal with the throttling limits as the code is used to migrate mailboxes and I want to keep the properties of the mails (not send new mails using smtp.


    Cordialement,
    Emmanuel Dreux
    http://www.bcpsoft.fr
    Formation FIM 2010

    Wednesday, August 15, 2012 11:32 AM