2

I want to format attachment info as a string with File Name and File Size

I can get FileName in Attachments.ContentDisposition, but Size is null. Is any other way to get?

Eg : string.Join(',' + Environment.NewLine, mailData[i].Attachments.ToList().Select(y => y.ContentDisposition.FileName + '(' + y.ContentDisposition.Size.ToString() + ')').ToList())

Example output i want :

xxx.jpg(123 bytes), xxx.png(456 bytes), xxx.pdf(123456 bytes),

jstedfast
  • 35,744
  • 5
  • 97
  • 110
Tan Boon Jun
  • 131
  • 12

1 Answers1

4

There's no property that will get you the size (the Size property on ContentDisposition relies on the sending client to set that and even when set might not be accurate).

So to get the size of the attachment, you would need to do this:

static long MeasureAttachmentSize (MimePart part)
{
    using (var measure = new MimeKit.IO.MeasuringStream ()) {
        part.Content.DecodeTo (measure);
        return measure.Length;
    }
}
jstedfast
  • 35,744
  • 5
  • 97
  • 110