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

Append MIME from a StringBuilder with Flags

See more IMAP Examples

Demonstrates the Chilkat Imap.AppendMimeWithFlagsSb method, which uploads MIME contained in a StringBuilder and sets its initial system flags. The first argument is the mailbox, the second is the StringBuilder, and the third through sixth set \Seen, \Flagged, \Answered, and \Draft. This example uploads to Drafts with the draft flag set.

Background: This is the StringBuilder form of AppendMimeWithFlags. Passing the MIME as a StringBuilder avoids copying a large message into an intermediate string and is convenient when you already built or loaded the message into a StringBuilder (for example with LoadFile).

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.StringBuilder,
  Chilkat.Imap,
  Chilkat.Email;

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

procedure RunDemo;
var
  success: Boolean;
  imap: TImap;
  email: TEmail;
  mimeText: string;
  sbMime: TStringBuilder;
  seen: Boolean;
  flagged: Boolean;
  answered: Boolean;
  draft: Boolean;

begin
  success := False;

  //  Demonstrates the Imap.AppendMimeWithFlagsSb method, which uploads MIME contained in a
  //  StringBuilder and sets its initial system flags.  The 1st argument is the mailbox, the 2nd
  //  is the StringBuilder, and the 3rd through 6th set \Seen, \Flagged, \Answered, and \Draft.

  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;

  //  Load the MIME into a StringBuilder.  (A StringBuilder can also be filled via LoadFile.)
  sbMime := TStringBuilder.Create;
  sbMime.Append(mimeText);

  //  Choose the initial flags.  Named variables make each argument's meaning clear at the call.
  seen := False;
  flagged := False;
  answered := False;
  draft := True;

  //  Upload to "Drafts" with the \Draft flag set.
  success := imap.AppendMimeWithFlagsSb('Drafts',sbMime,seen,flagged,answered,draft);
  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;
  sbMime.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.