1

I'm trying to attach a file that's provided in a byte array as an attachment in an email but I'm getting the following error:

Exception: Failure sending mail.
InnerException: Stream does not support reading.

But that's it. Not much to go with!

I'm attaching the byte array file as follows:

using (MailMessage message = new MailMessage())
{
    message.Subject = subject;
    ....
    using (MemoryStream ms = new MemoryStream(attachment.Data))
    {
        message.Attachments.Add(new System.Net.Mail.Attachment(ms, attachment.Name));
    }
}

Note that the attachment variable is our own class which just provides additional information about the file including .Data which contains the byte array representing the file.

Any ideas?

Thanks.

Thierry
  • 6,142
  • 13
  • 66
  • 117
  • @IanKemp Thanks for that. It is now working. Kind of weird having a variable at the class level for something like that but it is being disposed, so no major concern. Main thing is that it is working. Thanks again! – Thierry Nov 19 '20 at 14:30
  • I'll go ahead and delete the question I guess – Thierry Nov 19 '20 at 14:31
  • 1
    No need to delete - this question still has value as a signpost to the other question that has an answer to this problem. From https://stackoverflow.com/help/duplicates "The fundamental goal of closing duplicate questions is to help people find the right answer by getting all of those answers in one place." – Ian Kemp Nov 19 '20 at 14:43
  • @IanKemp, ok fair. I'll leave it then :). Thanks – Thierry Nov 19 '20 at 16:19
  • I came here from Google and followed the link to fix my issue, thank you. – Matt Feb 15 '22 at 10:59

0 Answers0