Codestone Ltd logo

Search FAQ

Internet Mail Client Control FAQs/Objects/Message Object

How can I remove attachments from a message?


The .Remove() method of the SectionCollection object allows you to remove sections from a message. The tricky part is deciding how you want to classify a section as an attachment and making sure that you examine all the sections in a message.

What is an attachment?

In the strictest MIME sense an attachment is probably a Section with the .Disposition property equal to "attachment" so a simple test on the .Disposition property is enough to classify the Section as an attachment:

if Section.Disposition "attachment" then
' classify as attachment
end if

In many cases, however, this simple test will not be enough - you may, for example, want to remove all potentially harmful sections before presenting the message to the user. If this is the case then testing the .Disposition property is probably not going to catch all the sections you wish it to and an alternative is to test the .Type and .SubType properties. The most aggressive example of this might be to define an attachment as any section which does not have have .Type="text" and .SubType="plain":

if not (Section.Type "text" and Section.SubType "plain"then
' classify as attachment
end if
end sub

To be slightly less aggressive you could, for example, allow plain text and images through the net:

if not ((Section.Type "text" and Section.SubType "plain"or (Section.Type "image")) then
' classify as attachment
end if

In summary, then, you should think carefully of how an attachment should be defined in the context of your application. We've given a few examples here but you may well wish to combine the testing of the .Type and .SubType properties with the .Disposition and possibly .Filename properties to define the correct set of rules in your application.

Examining all the sections in the message.

Message structures can be hierarchical so it is important to recurse through the full structure in order to examine all the sections (this is decribed more fully in FAQ-4-19). Here, then, is a modified version of the ProcessSections() subroutine from FAQ-4-19 to agressively remove attachments from a message:

' ---------------------------------------------------------
' Sub RemoveAttachments 
' Removes attachments from a SectionCollection object
' eg:
' RemoveAttachments(MyMessage.Sections)
' ---------------------------------------------------------

sub RemoveAttachments(Sections)

For iIndex message.Sections.Count To Step -1
if (section.type=="multipart"then   
' recurse
else if not (Section.Type "text" and Section.SubType "plain"then
' classified as an attachment so remove it
' handle as 'normal' section
end if


Related questions

Can't find the answer to your question? We're here to help!