I've got a project where I'm using a PDF generator to send a file to a user. We'd like to give the user the option to attach this file to an email instead, and we're having trouble using the Stream object and Attachment logic together.
We start with ABCpdf, which has two save methods: it can save to a Stream, or if you give it a string, it'll try to save to a file on the disk there. We've done both no problem.
Stream stream = new MemoryStream();
myPdf.Save(stream);
Everything is mostly cool at this point - stream
has several kilobytes of data, and if you .Save() to a file, you get an actual file with the same number of bytes.
So we attach to an email at this point (after initializing the mail object, setting To: and From:, etc.):
mail.Attachments.Add(new Attachment(stream, "myPdf.pdf"));
mail.Send();
...which gets us as far as receiving an email with 0 bytes, but the proper filename.
All the examples I'm finding online use a StreamReader or a StreamWriter or a Flush() or something. It always seems like it's more complicated than simply passing a Stream, but maybe only one or two lines more complicated. None of those examples start with a Stream - they're always trying to turn an array into a Stream to show you how easy that is, or grab a file from disk (which we can't do, which is why we're excited to use a Stream).
Anyway, if anyone can explain what I'm doing wrong or what I ought to be doing, I'd really appreciate it. Thanks.
A guess... Back dat stream up to the beginning before sending nit
// Set the position to the beginning of the stream.
stream.Seek(0, SeekOrigin.Begin);
mail.Attachments.Add(new Attachment(stream, "myPdf.pdf"));
mail.Send();