none
How to save System.Net.Mail.Attachment into file? RRS feed

  • Question

  • This sounds a simple thing, but first I tried Streamread, StreamWrite, then BinaryRead and BinaryWrite. With StreamRead/Write, none of the files saved are right, with BinaryRead/Write, it seems binary files like PDF, image etc. are right, but csv, excel file etc, are in zero length. Any idea?

     

    Thanks

    Wednesday, August 10, 2011 5:37 AM

Answers

  • Hello David,

    Below code should help you. just pass your attchment to this function.

    public void SaveMailAttachment(System.Net.Mail.Attachment attachment)
    {
         byte[] allBytes = new byte[attachment.ContentStream.Length];
         int bytesRead = attachment.ContentStream.Read(allBytes, 0, (int)attachment.ContentStream.Length);
    
         string destinationFile = @"C:\" + attachment.Name;
    
         BinaryWriter writer = new BinaryWriter(new FileStream(destinationFile, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None));
         writer.Write(allBytes);
         writer.Close();
    }
    

    Right now it saves attachment in C:\. Alter the code to save it at your desired location.
     


    Please mark this post as answer if it solved your problem. Happy Programming!
    Wednesday, August 10, 2011 6:22 AM
  • Hi david

     

    Plz check this link 

    http://stackoverflow.com/questions/1264672/how-to-save-mailmessage-object-to-disk-as-eml-or-msg-file

     

    hope it helps


    Best Regards...Please mark as answer if my post is helpful
    Wednesday, August 10, 2011 8:20 AM

All replies

  • Hello David,

    Below code should help you. just pass your attchment to this function.

    public void SaveMailAttachment(System.Net.Mail.Attachment attachment)
    {
         byte[] allBytes = new byte[attachment.ContentStream.Length];
         int bytesRead = attachment.ContentStream.Read(allBytes, 0, (int)attachment.ContentStream.Length);
    
         string destinationFile = @"C:\" + attachment.Name;
    
         BinaryWriter writer = new BinaryWriter(new FileStream(destinationFile, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None));
         writer.Write(allBytes);
         writer.Close();
    }
    

    Right now it saves attachment in C:\. Alter the code to save it at your desired location.
     


    Please mark this post as answer if it solved your problem. Happy Programming!
    Wednesday, August 10, 2011 6:22 AM
  • thanks for you code.

    Your code is similar to what I have, except you allocate whole file into a buffer.

    I just tried your code, I got same result, csv, excel etc. saved with size 0. Image and PDF are fine.

    Wednesday, August 10, 2011 8:11 AM
  • Hi david

     

    Plz check this link 

    http://stackoverflow.com/questions/1264672/how-to-save-mailmessage-object-to-disk-as-eml-or-msg-file

     

    hope it helps


    Best Regards...Please mark as answer if my post is helpful
    Wednesday, August 10, 2011 8:20 AM
  • thanks for you code.

    Your code is similar to what I have, except you allocate whole file into a buffer.

    I just tried your code, I got same result, csv, excel etc. saved with size 0. Image and PDF are fine.


    David, have you tried my code? I have tested and then given the code to you. It works for csv, xlsx, pdf, jpeg, video files and all. Check it again


    Please mark this post as answer if it solved your problem. Happy Programming!

    Wednesday, August 10, 2011 9:02 AM
  • Any updates on this?
    Please mark this post as answer if it solved your problem. Happy Programming!
    Friday, August 12, 2011 5:49 AM
  • it is not working for Zip Attachment. Please help

    Friday, August 23, 2019 8:27 AM
  • Hello,

    Please start a new question and post your code along with what you mean by it's not working as "not working" is a vague response.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, August 23, 2019 9:37 AM
    Moderator