Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Set a MIME Body from Transfer-Encoded Text
See more MIME Examples
Demonstrates the Chilkat Mime.SetBodyFromEncoded method, which sets the body from already transfer-encoded text. The first argument is the encoding (base64 or quoted-printable) and the second is the encoded text.
Background: Sometimes you already hold content in its encoded form — a Base64 string from a database or another API — and re-decoding just to have Chilkat re-encode it is wasteful. This method stores the encoded text as-is and records the matching transfer encoding, so the body is correct without a needless round trip. Only the standard reversible encodings, Base64 and quoted-printable, are accepted.
Chilkat Pascal (Lazarus/Delphi) Downloads
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;
base64Body: string;
decoded: string;
begin
success := False;
// Demonstrates the Mime.SetBodyFromEncoded method, which sets the body from already transfer-
// encoded text. The 1st argument is the encoding ("base64" or "quoted-printable") and the 2nd is
// the encoded text.
mime := TMime.Create;
mime.ContentType := 'text/plain';
// The body is provided already Base64-encoded; Chilkat stores it without re-encoding.
base64Body := 'SGVsbG8sIHRoaXMgaXMgdGhlIGJvZHku';
success := mime.SetBodyFromEncoded('base64',base64Body);
if (success = False) then
begin
WriteLn(mime.LastErrorText);
Exit;
end;
// The decoded body is the original text.
decoded := mime.GetBodyDecoded();
if (mime.LastMethodSuccess = False) then
begin
WriteLn(mime.LastErrorText);
Exit;
end;
WriteLn(decoded);
mime.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.