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

Get the text body content of a MIME part.

See more MIME Examples

Explains and demonstrates the GetBodyEncoded and GetBodyDecoded methods. This example uses the MIME test data located at http://www.chilkatsoft.com/testData/sampleMime1.txt

The sampleMime1.txt contains:

Content-Type: multipart/mixed;
 boundary="------------070404010201060604000708";

This is a multi-part message in MIME format.

--------------070404010201060604000708
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain; charset="utf-8";

Falsches =C3=9Cben von Xylophonmusik qu=C3=A4lt jeden gr=C3=B6=C3=9Feren Zwe=
rg.
--------------070404010201060604000708
Content-Transfer-Encoding: base64
Content-Type: text/plain; charset="utf-8";

RmFsc2NoZXMgw5xiZW4gdm9uIFh5bG9waG9ubXVzaWsgcXXDpGx0IGplZGVuIGdyw7bDn2VyZW4g
Wndlcmcu

--------------070404010201060604000708
Content-Transfer-Encoding: 8bit
Content-Type: text/plain; charset="iso-8859-1";

Falsches Üben von Xylophonmusik quält jeden größeren Zwerg.
--------------070404010201060604000708--

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

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

procedure RunDemo;
var
  success: Boolean;
  mime: TMime;
  part1: TMime;
  part2: TMime;
  part3: TMime;

begin
  success := False;

  //  This example assumes the Chilkat API to have been previously unlocked.
  //  See Global Unlock Sample for sample code.

  mime := TMime.Create;

  //  Load the sampleMime1.txt file into the MIME object.
  //  (This file is available at http://www.chilkatsoft.com/testData/sampleMime1.txt )

  success := mime.LoadMimeFile('sampleMime1.txt');
  if (success = False) then
    begin
      WriteLn(mime.LastErrorText);
      Exit;
    end;

  //  The sampleMime1.txt is a MIME document with a top-level 
  //  multipart/mixed containing 3 sub-parts, each of which has the 
  //  same body text but with different content-transfer-encodings and 
  //  using different character encodings (utf-8 and iso-8859-1).

  //  Calling mime.GetBodyEncoded or mime.GetBodyDecoded on the 
  //  top-level multipart/mixed MIME object will return an empty string.
  //  It is because the "body" of a multipart MIME object is always empty.
  //  A multipart MIME object contains sub-parts (each a MIME object),
  //  and it is only the leaf-objects that can have non-empty bodies.

  //  Get GetBodyDecoded method returns the body text decoded
  //  from whatever the content-transfer-encoding may be, and
  //  converted from whatever charset encoding might be used.
  //  In this case, calling GetBodyDecoded on each of the three
  //  sub-parts will return the same string.
  //  To demonstrate:
  part1 := TMime.Create;
  success := mime.PartAt(0,part1);
  if (success = False) then
    begin
      WriteLn(mime.LastErrorText);
      Exit;
    end;

  WriteLn(part1.GetBodyDecoded());

  part2 := TMime.Create;
  success := mime.PartAt(1,part2);
  if (success = False) then
    begin
      WriteLn(mime.LastErrorText);
      Exit;
    end;

  WriteLn(part2.GetBodyDecoded());

  part3 := TMime.Create;
  success := mime.PartAt(2,part3);
  if (success = False) then
    begin
      WriteLn(mime.LastErrorText);
      Exit;
    end;

  WriteLn(part3.GetBodyDecoded());

  //  The GetBodyEncoded method will NOT decode from 
  //  whatever content-transfer-encoding is used.  However, it will
  //  convert from whatever internal character encoding
  //  may be used to return a string appropriate for the calling
  //  programming language (for example, in .NET or any language
  //  using ActiveX, all strings are Unicode..)
  WriteLn(part1.GetBodyEncoded());
  WriteLn(part2.GetBodyEncoded());
  WriteLn(part3.GetBodyEncoded());


  mime.Free;
  part1.Free;
  part2.Free;
  part3.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.