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

Append (Upload) an Email to an IMAP Mailbox

See more IMAP Examples

Demonstrates the Chilkat Imap.AppendMail method, which uploads an Email object to a mailbox on the server. The first argument is the destination mailbox name and the second is the Email. After a successful append, the AppendUid property holds the UID the server assigned (or 0 if none was reported). This example uploads a newly built message to the Drafts mailbox.

Background: The IMAP APPEND command lets a client add a message directly to any mailbox without sending it through SMTP — it is how "Save to Drafts" works, and how a client keeps its own copy in Sent. The initial \Seen state is governed by the AppendSeen property (or by the flag arguments of the AppendMimeWithFlags variants).

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;

begin
  success := False;

  //  Demonstrates the Imap.AppendMail method, which uploads an Email object to a mailbox on the
  //  server.  The 1st argument is the destination mailbox name and the 2nd is the Email.

  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.';

  //  Append (upload) the email to the "Drafts" mailbox.
  success := imap.AppendMail('Drafts',email);
  if (success = False) then
    begin
      WriteLn(imap.LastErrorText);
      Exit;
    end;

  //  AppendUid holds the UID assigned by the server (0 if the server did not report one).
  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.