Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Create DSN (Delivery Status Notification) Email
See more Email Object Examples
Demonstrates how to create a DSN (Delivery Status Notification) Email having the format as defined in RFC 3464.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.StringBuilder,
Chilkat.Xml,
Chilkat.Email,
Chilkat.CkDateTime;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
email: TEmail;
xml: TXml;
dtNow: TCkDateTime;
headerOnly: Boolean;
sbText: TStringBuilder;
explain: string;
dsnEmail: TEmail;
begin
success := False;
email := TEmail.Create;
email.Subject := 'Test';
email.From := 'joe@example.com';
email.Body := 'This is a test';
email.AddTo('','recipient@example.com');
WriteLn(email.GetMime());
WriteLn('');
WriteLn('-------------------------------------------------------------');
WriteLn('');
// This is the email we just created:
// MIME-Version: 1.0
// Date: Mon, 12 May 2025 11:13:15 -0500
// Message-ID: <917FA49F75544EF51948B0A52F403B925B51073F@SLICE>
// Content-Type: text/plain; charset=us-ascii; format=flowed
// Content-Transfer-Encoding: 7bit
// X-Priority: 3 (Normal)
// Subject: Test
// From: joe@example.com
// To: recipient@example.com
//
// This is a test
// -----------------------------------------------------------------
// Convert the above email into a DSN (Delivery Status Notification)
xml := TXml.Create;
xml.Tag := 'DeliveryStatusFields';
xml.NewChild2('Final-Recipient','rfc822; recipient@example.com');
xml.NewChild2('Action','failed');
xml.NewChild2('Status','5.1.2');
xml.NewChild2('Diagnostic-Code','smtp; 550 5.1.2 Host unknown (Domain name not found)');
dtNow := TCkDateTime.Create;
dtNow.SetFromCurrentSystemTime();
xml.NewChild2('Last-Attempt-Date',dtNow.GetAsRfc822(True));
headerOnly := True;
sbText := TStringBuilder.Create;
sbText.Append('This is an automatically generated Delivery Status Notification.' + #13#10 + #13#10);
sbText.Append('Delivery to the following recipient failed permanently:' + #13#10 + #13#10);
sbText.Append(' recipient@example.com' + #13#10 + #13#10);
sbText.Append('Technical details of permanent failure:' + #13#10);
sbText.Append('DNS Error: Domain name not found' + #13#10);
explain := sbText.GetAsString();
dsnEmail := TEmail.Create;
success := email.ToDsn(explain,xml.GetXml(),headerOnly,dsnEmail);
if (success = False) then
begin
WriteLn(email.LastErrorText);
Exit;
end;
WriteLn(dsnEmail.GetMime());
// Here's the MIME of the DNS email we just created:
// -------------------------------------------------
// Content-Type: multipart/report; report-type="delivery-status"; boundary="------------060100020300020303000802"
//
// --------------060100020300020303000802
// Content-Type: text/plain; charset=windows-1252; format=flowed
// Content-Transfer-Encoding: 7bit
//
// This is an automatically generated Delivery Status Notification.
//
// Delivery to the following recipient failed permanently:
//
// recipient@example.com
//
// Technical details of permanent failure:
// DNS Error: Domain name not found
//
// --------------060100020300020303000802
// Content-Type: message/delivery-status
//
// Final-Recipient: rfc822; recipient@example.com
// Action: failed
// Status: 5.1.2
// Diagnostic-Code: smtp; 550 5.1.2 Host unknown (Domain name not found)
// Last-Attempt-Date: Mon, 12 May 2025 11:30:39 -0500
//
// --------------060100020300020303000802
// Content-Type: text/rfc822-headers; charset=windows-1252
//
// MIME-Version: 1.0
// Date: Mon, 12 May 2025 11:30:39 -0500
// Message-ID: <B8E6875D582A78AE779FC0B46ACC8C858CEAF608@SLICE>
// Content-Type: text/plain; charset=us-ascii; format=flowed
// Content-Transfer-Encoding: 7bit
// X-Priority: 3 (Normal)
// Subject: Test
// From: joe@example.com
// To: recipient@example.com
// --------------060100020300020303000802--
email.Free;
xml.Free;
dtNow.Free;
sbText.Free;
dsnEmail.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.