0

In one method I have the below code:

var newMessage = new MailMessage();
newMessage.Attachments.Add(new Attachment(ToStream("testAttachment"), MediaTypeNames.Text.Plain));

Then I have this method which is called from above. I have done some research but am not sure still, does the Stream method actually generate a File on my machine or in this case is it just adding an attachment?

If it does actually create a file on my machine, where would it go as I am not defining a path? Then id also want to know how I can add an attachment to Email from a string without generating a file on my machine

private Stream ToStream(string text)
{
   var stream = new MemoryStream();
   var writer = new StreamWriter(stream);
   writer.Write(text);
   writer.Flush();
   stream.Position = 0;
   return stream;
}
5
  • It depends on how many free ram is available - if not enough there is some potential chance it will be stored in the swap file on disk
    – Sir Rufo
    Commented Nov 8, 2023 at 17:38
  • @SirRufo That doesn't create a file as such. Swap isn't considered a "file" in the normal sense. Commented Nov 8, 2023 at 20:33
  • MemoryStream is entirely in memory. If you wanted a file you'd need FileStream. Commented Nov 8, 2023 at 20:34
  • This has nothing to do with the Stream, and everything to do with the implementation of the MailMessage.Attachments collection. Commented Nov 8, 2023 at 21:47
  • @Charlieface I never said "create a file" and I only wanted to point out that there is a (theoretical) chance that the data will get on the disk.
    – Sir Rufo
    Commented Nov 9, 2023 at 3:17

0

Browse other questions tagged or ask your own question.