in the .NET MailMessage class, how do you remove an attachment?

2.2k Views Asked by At

in the .NET MailMessage class, how do you remove an attachment?

We have modified the code on a project I am working on. Part of the modifications seem to impact code that has not changed at all.

It would appear that now a part of the code that added attachments to email (using the "MailMessage" class) is failing because those email attachments are located on a secure server.

And so, as a test, it would be interesting to implement a piece of code that would remove the attachment upon a failure of the SmtpClient Send (which passes a MailMessage class as a parameter). If I am using .Attachments.Remove, it asks for an Attachment class. Where and how do I assign this? enter image description here

2

There are 2 best solutions below

0
On

Remove removes a specific item. Use mail.Attachments.Clear() to remove all items.

0
On

Attachments is a collection, and, as every collection, has a Clear method as well a Remove method, but while Remove requires the single attachment to remove, the Clear acts on the whole Attachments collection removing any item present in it