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

Get an Alternative Body into a BinData

See more Email Object Examples

Demonstrates the Chilkat Email.GetAlternativeBodyBd method, which copies the contents of the Nth alternative body into a BinData object. The first alternative body is at index 0, and it should only be called when NumAlternatives is greater than 0. This example builds a message with plain-text and HTML alternatives and copies the first into a BinData.

Background: This is the binary counterpart to GetAlternativeBody (which returns text). Reading an alternative into a BinData preserves the exact bytes — important when a body part uses a binary or unusual transfer encoding, or when you intend to hash it or write it out verbatim rather than decode it to a string.

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

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

procedure RunDemo;
var
  success: Boolean;
  email: TEmail;
  bd: TBinData;

begin
  success := False;

  //  Demonstrates the GetAlternativeBodyBd method, which copies the contents of the Nth
  //  alternative body into a BinData object.  The first alternative body is at index 0; call
  //  only when NumAlternatives is greater than 0.

  email := TEmail.Create;
  email.Subject := 'GetAlternativeBodyBd example';

  email.SetTextBody('This is the plain-text alternative.','text/plain');
  email.AddHtmlAlternativeBody('<html><body>This is the HTML alternative.</body></html>');

  //  Copy the first alternative body (index 0) into a BinData object.
  bd := TBinData.Create;

  success := email.GetAlternativeBodyBd(0,bd);
  if (success = False) then
    begin
      WriteLn(email.LastErrorText);
      Exit;
    end;

  WriteLn('Alternative 0 size (bytes) = ' + bd.NumBytes);


  email.Free;
  bd.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.