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


pop=new ActiveXObject("csmail.Pop3client");             // Create a Pop3Client Object
pop.Connect("localhost","myusername","secret");         // and connect to the server

WScript.echo("Retrieving Messages...");
pop.RetrieveMessages();                                 // Get all the messages on the server
WScript.echo("Done...");

pop.Close(false)                                        // Close the connection to the server
                                                        // (but dont delete the messages)

// Now we iterate through all the messages

Messages=new Enumerator(pop.Messages);

for (;!Messages.atEnd();Messages.moveNext()) {
  
msg=Messages.item();
  
WScript.echo(msg.Subject);
  
  
Sections=new Enumerator(msg.Sections);

  
for (;!Sections.atEnd();Sections.moveNext())  {
    
section=Sections.item();
    
if (section.Filename!="") {
      
WScript.echo(section.Filename);                   // echo the filename
      
section.WriteBodyToFile("v:\\attachments\\"+section.Filename);
                                                      
// Save the attachment in a suitable place
      
}
    }
  }