Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Save an Email to a Temporary MHT File
See more Email Object Examples
Demonstrates the Chilkat Email.CreateTempMht method, which prepares the email for display in a web browser and saves it as a temporary MHT file, returning the path that was written. If the filename argument is empty, Chilkat chooses a temporary filename.
To make the message viewable as a standalone web page, the method transforms it: all attachments are dropped; if the email has both HTML and plain-text alternatives, the plain-text alternative is dropped; and if the email has only a plain-text body, it is converted to HTML. This example loads an HTML email and creates the temporary MHT.
Background: MHT (MIME HTML) is a single-file web-page-archive format — it packs an HTML document together with its images and style sheets into one file, which is essentially what an HTML email already is. Because a Windows WebBrowser control can navigate directly to an
.mht file, writing the message out as MHT is a quick way to render a received email, inline images and all, inside a desktop application. The transformations (dropping attachments, keeping just the HTML representation) ensure the result is a clean, self-contained page. For unpacking to loose files served by a web application instead, see AspUnpack.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.Email;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
email: TEmail;
mhtPath: string;
begin
success := False;
// Demonstrates the CreateTempMht method, which prepares the email for viewing in a browser
// and saves it as a temporary MHT (MIME HTML) file, returning the file path.
//
// To produce browser-viewable output, CreateTempMht transforms the message: it drops all
// attachments; if the email has both HTML and plain-text alternatives it drops the
// plain-text alternative; and if the email has only a plain-text body it converts that
// body to HTML. Passing an empty filename lets Chilkat choose a temporary filename.
email := TEmail.Create;
success := email.LoadEml('qa_data/eml/html_with_images.eml');
if (success = False) then
begin
WriteLn(email.LastErrorText);
Exit;
end;
// Transform the email for browser display and save it as a temporary MHT file
// (empty filename = auto-generated temp file).
mhtPath := email.CreateTempMht('');
WriteLn('Temporary MHT file: ' + mhtPath);
// Note: The path "qa_data/..." is a relative local filesystem path,
// relative to the current working directory of the running application.
email.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.