4 Replies - 1195 Views - Last Post: 16 December 2013 - 09:57 PM Rate Topic: -----

#1 complete   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 113
  • Joined: 12-April 07

In the Visual C# MailMessage class, how do you remove an attachment?

Posted 16 December 2013 - 02:33 PM

In the Visual C# 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?

Here is the error that I get which describes the proper use of the Remove method, when I type "Mail.Attachments.Remove()" with "Mail" being a MailMessage class:
  • bool Collection<Attachment>.Remove(Attachment item) Removes the first occurance of a specific object from the System.Collections.ObjectMode.Collection<T>, C#: Unknown method 'Remove()' of 'system.Net.Mail.AttachmentCollection'


Is This A Good Question/Topic? 0
  • +

Replies To: In the Visual C# MailMessage class, how do you remove an attachment?

#2 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: In the Visual C# MailMessage class, how do you remove an attachment?

Posted 16 December 2013 - 02:37 PM

Moved to regular C# forum. This is not an advanced discussion.
Was This Post Helpful? 0
  • +
  • -

#3 Momerath   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1021
  • View blog
  • Posts: 2,463
  • Joined: 04-October 09

Re: In the Visual C# MailMessage class, how do you remove an attachment?

Posted 16 December 2013 - 04:08 PM

You could iterate through the collection and find the attachment you want by name, then remove it. You'll have to find the actual object in the collection you want removed, as creating a new Attachment class will be new, and won't exist in the collection even if everything in the class is identical to an object that is already in the collection.
Was This Post Helpful? 1
  • +
  • -

#4 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6218
  • View blog
  • Posts: 21,468
  • Joined: 05-May 12

Re: In the Visual C# MailMessage class, how do you remove an attachment?

Posted 16 December 2013 - 05:33 PM

Momerath is right on with his advice.

I'm a little concerned thought that you have gotten far advanced into programming in C# and .NET, but you don't understand this common concept/pattern that is used all through out the framework.
Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6534
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: In the Visual C# MailMessage class, how do you remove an attachment?

Posted 16 December 2013 - 09:57 PM

Quote

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.


They didn't say they wrote the code. Just that they modified something and don't have a clue about why it caused the changes the saw.

Classic copy/paste whackers.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1