Mail to file sample.

Mail to file sample - demonstrates how to save all mail massages from all mailboxes to a files, with using the Mail Processor API.

The  part of  source code of the sample:
	var
	 Handle: dword;
	 MailboxCount, FolderCount, MailCount: integer;
	 TotalCount: integer;
	 Result: dword;
	begin
	 Handle: = OpenMailbox('MSOE'); // open mailbox file 
	 if Handle> OMS_NOERROR then 
	  begin 
	   for FolderCount: = 0 to GetFolderCount(Handle) - 1 do // enumerate folders 
	    begin 
	     for MailCount: = 0 to GetMailCount(Handle, FolderCount) - 1 do // enumerate mail messages inside the mailbox 
	       begin 
		// Save objects to files - mail messages, mailmessages as text or attached files 
	       Result: = SaveMailToFile(Handle, FolderCount, MailCount, IntToStr (FolderCount) + '_' + IntToStr (MailCount))); 
	       end; 
	     if Result = OMS_MAILPROCESSORNOTREGISTER then Break; 
	    end; 
	 end; 
	 CloseMailbox (Handle); 
	end; 


Source code of Mail To File console version on Delphi.
program EmailToFileConsole;

{$APPTYPE CONSOLE}

uses
  SysUtils, Classes,   Engine in '..\..\..\Interfaces\Engine.pas';

var
  Path: string;
  Mailboxes: TMString;
  MailboxesList: TStringList;
  MailboxNumber, MailboxHandle, FolderCount,
  MailCount, Result, i, TotalCount: integer;
begin
  WriteLn('Mail Processor. Mailbox to file console application.');

  Path := ExtractFileDir(ParamStr(0)) + '\EmailToFileConsole store folder\';
  ForceDirectories(Path);

  SetRegisterKey('put your registration code here');

  GetAllMailboxes(Mailboxes); //get all registered mailboxes from the system

  MailboxesList := TStringList.Create;
  MailboxesList.Text := StrPas(Mailboxes); //parse to Delphi string list class

  for i := 0 to MailboxesList.Count - 1 do
    WriteLn(IntToStr(i + 1) + ') ' + MailboxesList[i]);

  WriteLn;
  Write('Please enter mailbox number and press enter: ');
  ReadLn(MailboxNumber);
  WriteLn('Save mail messages form ' + MailboxesList[MailboxNumber] + ' to ' + Path);

  TotalCount := 0;
  MailboxHandle := OpenMailbox(PChar(MailboxesList[MailboxNumber])); //open mailbox file
  if MailboxHandle > OMS_NOERROR then
  begin
    for FolderCount := 0 to GetFolderCount(MailboxHandle) - 1 do //enumerate folders
      for MailCount := 0 to GetMailCount(MailboxHandle, FolderCount) - 1 do //enumerate mail messages inside the mailbox
      begin
        //Save objects to files - mail messages, mailmessages as text or attached files only
        Result := SaveMailToFile(MailboxHandle, FolderCount, MailCount, PChar(Path + IntToStr(MailboxHandle) + '_' + 
         IntToStr(FolderCount) + '_' + IntToStr(MailCount)));
        if Result = OMS_OK then TotalCount := TotalCount + 1;
        if Result = OMS_MAILPROCESSORNOTREGISTER then break;
        Write('Save mail count: ' + IntToStr(TotalCount) + #$0D);
      end;
  end;
  CloseMailbox(MailboxHandle);
  MailboxesList.Free;
  ReadLn;
end.