Sample code for 30+ languages & platforms
Pascal (Lazarus/Delphi)

Append MIME and Set the IMAP Internal Date

See more IMAP Examples

Demonstrates the Chilkat Imap.AppendMimeWithDateStr method, which uploads a MIME message while explicitly setting the server-side internal date. The first argument is the mailbox, the second is the MIME text, and the third is an RFC 822 date/time string (for example Fri, 10 Jul 2026 20:15:30 GMT). This example uploads to Archive with a specific internal date.

Background: The IMAP internal date is mailbox metadata, distinct from the message's own Date header. It is normally the time the message arrived, and it is what the server uses for SINCE and BEFORE date searches. Setting it explicitly matters when migrating or importing messages, so imported mail keeps its original chronology instead of all appearing to arrive at import time.

Chilkat Pascal (Lazarus/Delphi) Downloads

Pascal (Lazarus/Delphi)
program ChilkatDemo;

// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  SysUtils,
  CkDllLoader,
  Chilkat.Imap,
  Chilkat.Email;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  imap: TImap;
  email: TEmail;
  mimeText: string;
  internalDate: string;

begin
  success := False;

  //  Demonstrates the Imap.AppendMimeWithDateStr method, which uploads a MIME message and sets
  //  its server-side internal date.  The 1st argument is the mailbox, the 2nd is the MIME text,
  //  and the 3rd is an RFC 822 date/time string for the internal date.

  imap := TImap.Create;

  imap.Ssl := True;
  imap.Port := 993;

  success := imap.Connect('imap.example.com');
  if (success = False) then
    begin
      WriteLn(imap.LastErrorText);
      Exit;
    end;
  success := imap.Login('user@example.com','myPassword');
  if (success = False) then
    begin
      WriteLn(imap.LastErrorText);
      Exit;
    end;

  //  Build the email to be uploaded.
  email := TEmail.Create;
  email.Subject := 'Meeting agenda';
  email.From := 'Alice <alice@example.com>';
  success := email.AddTo('Bob','bob@example.com');
  email.Body := 'Let''s meet at 10am to review the agenda.';

  mimeText := email.GetMime();
  if (email.LastMethodSuccess = False) then
    begin
      WriteLn(email.LastErrorText);
      Exit;
    end;

  //  Upload to "Archive" with an explicit internal date.
  internalDate := 'Fri, 10 Jul 2026 20:15:30 GMT';
  success := imap.AppendMimeWithDateStr('Archive',mimeText,internalDate);
  if (success = False) then
    begin
      WriteLn(imap.LastErrorText);
      Exit;
    end;

  WriteLn('Appended UID: ' + imap.AppendUid);

  success := imap.Disconnect();
  if (success = False) then
    begin
      WriteLn(imap.LastErrorText);
      Exit;
    end;


  imap.Free;
  email.Free;

end;

// ---------------------------------------------------------------------------

begin

  try
    RunDemo;
  except
    on E: Exception do
      WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
  end;

  WriteLn;
  {$IFDEF MSWINDOWS}
  WriteLn('Press Enter to exit...');
  ReadLn;
  {$ENDIF}
end.