none
EWS 1.1 - File attachment using FileStream RRS feed

  • Question

  • I'm experimenting with creating an email message file attachment by reading the contents of a file into a FileStream and using that specific overload of EmailMessage.Attachments.AddFileAttachment. 

     

     

     

                var emailMessage = new EmailMessage(_exchangeService);
    
    
    
                emailMessage.Subject = "Picture";
    
                emailMessage.Body = "Check out this picture";
    
                emailMessage.ToRecipients.Add("danj@contoso.com");
    
    
    
                byte[] buffer;
    
                var fileStream = new FileStream("C:\\Picture.jpg", FileMode.Open);
    
    
    
                try
    
                {
    
                    int bytesToRead = (int)fileStream.Length;
    
                    int bytesRead = 0;
    
    
    
                    buffer = new byte[bytesToRead];
    
    
    
                    while (bytesToRead > 0)
    
                    {
    
                        // Read may return anything from 0 to bytesToRead.
    
                        int n = fileStream.Read(buffer, bytesRead, bytesToRead);
    
    
    
                        // Break when the end of the file is reached.
    
                        if (n == 0)
    
                            break;
    
    
    
                        bytesRead += n;
    
                        bytesToRead -= n;
    
                    }
    
    
    
                    emailMessage.Attachments.AddFileAttachment("Screenshot.jpg", fileStream);
    
                    emailMessage.Send();
    
                }
    
                catch (Exception e)
    
                {
    
                    throw e;
    
                }
    
                finally
    
                {
    
                    fileStream.Close();
    
                }

    I can step through the code and see that the FileStream gets "filled". When the person receives the message, it's always an empty file. I tried this with a simple text file as well; I'm pretty sure I'm missing something here in my code. 

     

    Thank you

     

    Monday, January 17, 2011 3:34 AM

Answers

  • You are reading the stream into buffer, you then completely discard buffer and pass the stream that is positioned at its end to AddFileAttachment. There is therefore nothing the API can read from it and the attachment is always an empty file...

    To solve your problem, you could reset the position of your stream to 0 before you pass it to AddFileAttachment, but there are easier ways:

    • Either don't read the stream since you don't do anything with what you read anyway. This way, it will be positioned at its beginning when you pass it to AddFileAttachment,
    • Or just pass the file name to AddFileAttachment. No need to create a stream at all.

    David Claux | Program Manager - Exchange Web Services
    Monday, January 17, 2011 4:01 AM

All replies

  • You are reading the stream into buffer, you then completely discard buffer and pass the stream that is positioned at its end to AddFileAttachment. There is therefore nothing the API can read from it and the attachment is always an empty file...

    To solve your problem, you could reset the position of your stream to 0 before you pass it to AddFileAttachment, but there are easier ways:

    • Either don't read the stream since you don't do anything with what you read anyway. This way, it will be positioned at its beginning when you pass it to AddFileAttachment,
    • Or just pass the file name to AddFileAttachment. No need to create a stream at all.

    David Claux | Program Manager - Exchange Web Services
    Monday, January 17, 2011 4:01 AM
  • Thanks David, good catch ... I adjusted the stream position back to 0 and everything works as expected. 

    What's a good use case for uploading using a stream, when you're generating the contents of the file at runtime? Reading from a file stream doesn't really get me anything unless I'm writing the contents to another stream or manipulating it somehow.

    Thank you 

    Monday, January 17, 2011 1:53 PM
  • If you're not doing anything with the bytes you read from the stream why read it at all? It's just a waste of time and resources. Just open it and pass it directly to AddFileAttachment. Or as I already indicated don't use a stream at all and just pass the file name to AddFileAttachment - the API will take care of opening a stream for you.

    You want to use a stream when all you have is a stream :) For instance, if you wanted to create a text file attachment from data read from a blob column in a database, you'd open a stream to the database and pass that to AddFileAttachment.

    In your case, there is apparently no reason why you'd use a stream instead of just passing the file name to AddFileAttachment.


    David Claux | Program Manager - Exchange Web Services
    Tuesday, January 18, 2011 12:02 AM
  • Thanks David, 

    I was just looking for an example of when I would ever use a stream; the data from a blob in a database is a good example.  Thanks

    Tuesday, January 18, 2011 3:20 AM