Scott Mitchell
Scott Mitchell

Reputation: 8759

System.InvalidOperationException error when attaching files to System.Net.Mail.MailMessage

I am getting an odd error when attaching one or more files to a System.Net.Mail.MailMessage object from an ASP.NET page.

I create a List(Of Attachment) and add new Attachment objects for each attached file requested by the user. These are files that reside on the web server's file system. For example, the code looks similar to the below, but rather than having hard-coded file paths it's getting them from the database. But while debugging I see that the file paths are valid, pointing to existing files that I can view from Explorer given the full file path or from the website using the virtual address (~/Documents/resume.pdf, for example).

Dim attachments As New List(Of Attachment)
attachments.Add(New Attachment("C:\Websites\Documents\resume.pdf"))
attachments.Add(New Attachment("C:\Websites\Documents\map.png"))
...

After constructing my attachments collection I send the email, adding each Attachment object to the Attachments collection like so:

Dim message As MailMessage = New MailMessage(from, toAddress, subject, body)

If attachments IsNot Nothing Then
    For Each att As Attachment In attachments
        message.Attachments.Add(att)
    Next
End If

Dim mailClient As SmtpClient = New SmtpClient
mailClient.Send(message)

However, when I run the code I get the following error:

System.InvalidOperationException: One of the streams has already been used and can't be reset to the origin.

System.Web.HttpUnhandledException: Exception of type 'System.Web.HttpUnhandledException' was thrown. ---> System.Net.Mail.SmtpException: Failure sending mail. ---> System.InvalidOperationException: One of the streams has already been used and can't be reset to the origin.
   at System.Net.Mime.MimePart.ResetStream()
   at System.Net.Mail.Attachment.PrepareForSending()
   at System.Net.Mail.MailMessage.SetContent()
   at System.Net.Mail.MailMessage.Send(BaseWriter writer, Boolean sendEnvelope)
   at System.Net.Mail.SmtpClient.Send(MailMessage message)
   --- End of inner exception stack trace ---
   ...

I've tried replacing my logic that adds attachments based on a database query to one that adds a single file with a hard-coded file path. I've tried using different SMTP clients (my web host provider's SMTP server and GMail). I get the same error regardless.

Thanks

Upvotes: 0

Views: 2668

Answers (2)

Jimmy Cheeks
Jimmy Cheeks

Reputation: 11

I think better solution is to set your message object to nothing and your client option to nothing as well, before looping back around for the second message. Good luck!

Upvotes: 1

Scott Mitchell
Scott Mitchell

Reputation: 8759

Found the answer... the problem was because I was trying to send two separate emails using the same attachments collection.

The email sending logic was in a function that was called like so:

SendEmail(from, to, subject, body, attachments)

If SomethingOrOther Then
   SendEmail(from, someoneElse, subject, body, attachments)
End If

My (cheesy) workaround was to just create two attachments lists, one for the first call to SendEmail, another one for the second call.

Upvotes: 2

Related Questions