Codestone Ltd logo

Read all messages on a Pop3 Server and then save all the attachments

' Example:  CSMail VBScript Example 4
' Summary:  Read all messages on a Pop3 Server and then save all the attachments
' Usage:    cscript exvbs04.vbs
'           or
'           wscript exvbs04.vbs


set pop=CreateObject("csmail.Pop3client")             ' Create a Pop3Client Object
call pop.Connect("localhost","myusername","secret"
                                                      
' and connect to the server

wscript.echo "Retrieving Messages..."
call pop.RetrieveMessages                             ' Get all the messages on the server
wscript.echo "Done..."

pop.Close(false)                                      ' Close the connection to the server
                                                      ' (but don't delete the messages)

' Now we iterate through all the messages

for each msg in pop.Messages                          ' The messages property is a collection of 
                                                      ' all the messages we retrieved
  
wscript.echo msg.Subject
  
for each section in msg.Sections                    ' The sections property is a collection of 
                                                      ' all the sections (parts) of the message
    
if section.Filename<>"" then
      
wscript.echo section.Filename                   ' echo the filename
      
section.WriteBodyToFile("v:\attachments\"&section.Filename)
                                                      
' Save the attachment in a suitable place
      
end if
    next
  next