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

Append a MIME Message to an IMAP Mailbox

See more IMAP Examples

Demonstrates the Chilkat Imap.AppendMime method, which uploads a complete RFC 822/MIME message to a mailbox. The first argument is the destination mailbox name and the second is the MIME text. This example obtains the MIME from an Email object and uploads it to Drafts.

Background: Use AppendMime when you already hold a message's raw source — a .eml file, a message you assembled, or one you received. Chilkat sends the text exactly as supplied, without normalizing line endings or re-encoding headers, so every header and MIME boundary is preserved verbatim. The supplied text must be plain text (no raw non-text binary bytes).

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;

begin
  success := False;

  //  Demonstrates the Imap.AppendMime method, which uploads a complete MIME message to a mailbox.
  //  The 1st argument is the destination mailbox name and the 2nd is the MIME text.

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

  //  Obtain the MIME text to upload.  Here it is generated from the Email, but it could equally
  //  be read from a .eml file.
  mimeText := email.GetMime();
  if (email.LastMethodSuccess = False) then
    begin
      WriteLn(email.LastErrorText);
      Exit;
    end;

  //  Append (upload) the MIME to the "Drafts" mailbox.
  success := imap.AppendMime('Drafts',mimeText);
  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.