Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Set a MIME Body, Preserving Content Headers
See more MIME Examples
Demonstrates the Chilkat Mime.SetBody method, which sets the current part's body while preserving the existing content headers (Content-Type, charset, Content-Transfer-Encoding). The only argument is the body text; it is a void method.
Background: Unlike the
SetBodyFrom... methods, which set both the content and the headers that describe it, SetBody replaces only the body text and leaves the existing Content-Type and encoding in place. That is what you want when the media type is already correct and you simply need to swap in new content — updating a templated part, for instance — without re-declaring its type.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;
mimeText: string;
begin
success := False;
// Demonstrates the Mime.SetBody method, which sets the current part's body while preserving the
// existing content headers (Content-Type, charset, Content-Transfer-Encoding). The only argument
// is the body text. It is a void method.
mime := TMime.Create;
// Establish the content headers first.
success := mime.SetBodyFromPlainText('initial');
if (success = False) then
begin
WriteLn(mime.LastErrorText);
Exit;
end;
// Replace just the body text, keeping the existing Content-Type and encoding.
mime.SetBody('This is the replacement body text.');
mimeText := mime.GetMime();
if (mime.LastMethodSuccess = False) then
begin
WriteLn(mime.LastErrorText);
Exit;
end;
WriteLn(mimeText);
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.