Search code examples
c#imapattachmentlumisoft

Lumisoft IMAP attachment saved 0 bytes


I am trying to save an attachment via IMAP protocol in .NET c#, using Lumisoft library. The problem is that the file is downloaded and can be saved, but it's filesize is 0B.

Attachment is fetched by IMAP_Fetch_DataItem_Rfc822(). Variable mail is message parsed from stream: var mail = Mail_Message.ParseFromStream(stream);

foreach (var att in mail.Attachments)
{
  if (att.ContentType.Type == "image")
  { 
    try
    {
      var fPath = Path.Combine(dirPath, att.ContentType.Param_Name);
      if (!File.Exists(fPath))
      {
        Stream data = ((MIME_b_SinglepartBase)att.Body).GetDataStream();

        using (FileStream fs = File.Create(fPath))
        {
           LumiSoft.Net.Net_Utils.StreamCopy(data, fs, 4096);
        }  
     }
   Console.WriteLine("Storing image attachment into: " + fPath);

 ...

Solution

  • You need to close Stream data:

    Stream data = ((MIME_b_SinglepartBase)att.Body).GetDataStream();
    using (FileStream fs = File.Create(fPath))
    {
           LumiSoft.Net.Net_Utils.StreamCopy(data, fs, 4096);
    }
    data.Close(); // missing close
    ...