Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Get an Attachment as Text with CRLF Line Endings
See more Email Object Examples
Demonstrates the Chilkat Email.GetAttachmentStringCrLf method, which retrieves an attachment's data as text with all end-of-line sequences translated to CRLF (\r\n). The first argument is the zero-based attachment index and the second is the charset used to interpret the bytes. This example reads a text attachment and normalizes its line endings to CRLF.
Background: Different systems mark the end of a line differently — Unix uses a single
LF (\n), classic Mac used CR, and Windows/most internet protocols use CRLF (\r\n). Text pulled from an attachment can contain any of these. This method normalizes them all to CRLF, which is convenient when the text will be written to a protocol or format that expects consistent CRLF line endings, avoiding mixed-newline surprises.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
email: TEmail;
content: string;
begin
// Demonstrates the GetAttachmentStringCrLf method, which retrieves an attachment's data as
// text with all end-of-line sequences translated to CRLF. The first argument is the
// zero-based attachment index and the second is the charset used to interpret the bytes.
email := TEmail.Create;
email.Subject := 'Attachment as text (CRLF)';
email.AddStringAttachment('notes.txt','Line one.' + #10 + 'Line two.' + #10 + 'Line three.');
// Get the first attachment (index 0) as text with CRLF line endings.
content := email.GetAttachmentStringCrLf(0,'utf-8');
WriteLn('Attachment 0 text (CRLF-normalized):');
WriteLn(content);
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.