Codestone Ltd logo

Nested multipart messages

' Example:  CSMail VBScript Example 9
' Summary:  Nested multipart messages
' Usage:    cscript exvbs09.vbs
'           or
'           wscript exvbs09.vbs

' Notes:
' In this example we will create a nested multipart messages and show one 
' technique you may use for accessing such a message
'
' A common real-life example of a  nested multipatr message involves a message which has a text section 
' represented in two alternative formats and a single attached file ie:
'
' Message multipart/mixed
'   Section1 multipart/alternative
'     Section11 [Normal Text]
'     Section12 [HTML]
'   Section2  [Attached File]
'

set MyMsg=CreateObject("CSMail.Message")                    ' Create the message object

' Sections(1) has two sub-sections and the default Content-Type for Section(1) would be 
' multipart/mixed but we need it to be mulipart/alternative so we set the Mime Header Field
' appropriately - in this case we MUST use the Header collection rather than the Type/
' SubType properties - try doing it the otherway and you'll see why!
'
' Note that the control will handle the boundary parameter for us which saves some hassle!

MyMsg.Sections(1).Header("Content-Type")="multipart/alternative"    

' Sections(1).Sections(1) - Content type will default to text/plain
MyMsg.Sections(1).Sections(1).Body="This is the plain text message."

' Sections(1).Sections(2) - Need to set Content-Type to text/html
' - this time we can use the properties.
MyMsg.Sections(1).Sections(2).Body="This is the &ltB>html&lt/B&gt version."
MyMsg.Sections(1).Sections(2).Type="text"
MyMsg.Sections(1).Sections(2).SubType="html"

MyMsg.Sections(2).AttachBodyFromFile("V:\test\tribbles.giff"

' ... Could send this with the SMTPClient now... 

' ... but we'll pretend we've just received the message with the POP3Client object and we need to 
' process _all_ the sections

' This is how NOT to do it -  we miss  the sub-sections of Section(1)
wscript.echo "The wrong way!"
for each section in MyMsg.Sections
  wscript
.echo "Processing section Content-Type is "&section.Header("Content-Type"
  
next

' The easiest way is probably to use recursion - the ProcessSections subroutine declared 
' at the bottom of the file does this
wscript.echo "One right way!"
ProcessSections(MyMsg.Sections)

sub ProcessSections(Sections)

for each section in Sections
  
if (section.type<>"multipart"then
    
wscript.echo "Recursively Processing section Content-Type is "&section.Header("Content-Type"
    
end if
  
ProcessSections(section.Sections' Process sub-sections
  
next

end sub