0

I have this line of code that creates an attachment.

email.Attachments.Add(new Attachment(new MemoryStream(filebytes), "QRCode.png"));

now I am trying to apply ContentDisposition.Inline property to it....how would I do this when creating my attachment the way I did it?

Alex K.
  • 171,639
  • 30
  • 264
  • 288
user979331
  • 11,039
  • 73
  • 223
  • 418

2 Answers2

0

I suppose you want a solution in one line, this is one way of doing it:

email.Attachments.Add((new Func<Stream, Attachment>(stream => { var attachment = new Attachment(stream, "QRCode.png"); attachment.ContentDisposition.Inline = true; return attachment; }).Invoke(new MemoryStream(fileBytes))));

However, I strongly discourage this approach, it is awfully hard to read.

Darjan Bogdan
  • 3,780
  • 1
  • 22
  • 31
0

@darjan-bogdan's solution would be the way to go if it absolutely needs to be one line for some reason.

However, if the goal is to just have a single-line logic statement for adding an attachment, I'd just use a helper method, à la:

Attachment data = BuildAttachment(new MemoryStream(filebytes), "QRCode.png"));
...

private Attachment BuildAttachment(MemoryStream stream, string name) {
    Attachment attachment = new Attachment(stream, name);
    attachment.ContentDisposition.Inline = true;

    return attachment;
}

This preserves your "single line" in what I assume is your for-loop.

arychj
  • 711
  • 3
  • 21