Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Extract Linked Domains from an HTML Email
See more Email Object Examples
Demonstrates the Chilkat Email.LinkedDomains method, which extracts the domain names from the hyperlinks in an HTML email. Lowercase domains are appended to a StringTable without creating duplicates. This example sets an HTML body with several links and lists the distinct domains.
Background: The set of domains a message links to is a strong signal for spam and phishing analysis — legitimate mail usually points at a few expected domains, while abusive mail often hides links to many unrelated or look-alike hosts.
LinkedDomains gives you that de-duplicated list directly. Note it only parses the links; it does not fetch them, so no network request is made.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,
Chilkat.StringTable;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
email: TEmail;
domains: TStringTable;
n: Integer;
i: Integer;
begin
success := False;
// Demonstrates the LinkedDomains method, which extracts the domain names from the
// hyperlinks in an HTML email. Lowercase domains are appended to a StringTable without
// creating duplicates.
email := TEmail.Create;
email.Subject := 'LinkedDomains example';
// An HTML body containing several hyperlinks.
email.SetHtmlBody('<html><body>Visit <a href="https://www.example.com/page">Example</a>, <a href="http://sales.contoso.com">Contoso</a>, and <a href="https://www.example.com/other">Example again</a>.</body></html>');
// Extract the linked domains into a StringTable.
domains := TStringTable.Create;
success := email.LinkedDomains(domains);
if (success = False) then
begin
WriteLn(email.LastErrorText);
Exit;
end;
n := domains.Count;
for i := 0 to n - 1 do
begin
WriteLn(domains.StringAt(i));
end;
email.Free;
domains.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.