Question

I am using Delphi XE with Indy 10 SMTP client (TIdSMTP) - on a Win 7 64 machine. I'm generating a simple text file report that is sent as an email attachment with the *.txt extension. Users will normally be viewing the report attachment with MS Win Notepad.exe, the default Win 7 text editor (That cannot/will not change).

As always, I used #13+#10 in this project to represent CR/LF in Delphi strings. Never had a problem with it (although I never had to use it in Notepad AFAIK.)

But I found that the #13+#10 - CR/LF is ignored in Notepad.exe - as if it's not there, although the EOL in the attachment is recognized and rendered properly in every other editor I have around, Including NotePad++, MS Write, MS Word, Delphi, EditPad, etc. etc... I know there are problems with Notepad.exe dealing with CR alone or LF alone but I thought CR/LF always worked - but it isn't working here.

What am I doing wrong? Is this a unicode or 64 bit issue? How/what do I have to embed in my string in Delphi XE on Win7 64 platform, so that Notepad.exe will recognize and render properly EOL - CR/LF.

Relevant code as per Remy's comment:

const
  EOL = #13+#10;
  EMAIL_TITLE2 = 'Value expressed in $ 000''s' + EOL;
  REPORT_NAME='Report';

var
  list: TStringList;
  s: string;
  ...
begin    
  ...
  list.Add (EMAIL_TITLE2)
  ... add more text lines + EOL;

  s := list.Text;

  Attachment := TIdAttachmentMemory.Create(msg.MessageParts, s);
  Attachment.FileName := REPORT_NAME + '.txt';
  smtpClient.Send(msg);
Was it helpful?

Solution

Indy has its own EOL constant for #13#10, so you do not need to declare your own constant.

Do not include line breaks when adding a string to the TStringList. Let Add() handle the line break for you. If you need to Add() a string with line breaks in it, split the string first and Add() the pieces. And if your code is running on a non-Windows system, use the TStringList.LineBreak property to specify #13#10 as the line break used when outputting text, otherwise a platform-specific default is used instead.

Something else to watch out for is that TIdAttachmentMemory will encode a string using Indy's default charset, which is set to ASCII by default. So if you are sending text with non-ASCII characters in it, use TStringList.SaveToStream() so you can specify whatever encoding you want, such as UTF-8, and then have TIdAttachmentMemory send that TStream data instead of the original string. You can either save the TStringList to your own TStream, such as a TMemoryStream or TStringStream, and pass that to the TIdAttachmentMemory constructor, or you can use the TIdAttachmentMemory.PrepareTempStream() method to get a TStream owned by TIdAttachmentMemory that you can write data to.

Try this:

const
  EMAIL_TITLE2 = 'Value expressed in $ 000''s';

var
  list: TStringList;
  strm: TStream;
  ...
begin    
  ...
  list.LineBreak := #13#10;
  ...
  list.Add (EMAIL_TITLE2);
  ... add more text lines

  Attachment := TIdAttachmentMemory.Create(msg.MessageParts);
  Attachment.ContentType := 'text/plain';
  Attachment.CharSet := 'utf-8';
  Attachment.FileName := ReportName + '.txt';
  strm := Attachment.PrepareTempStream;
  try
    list.SaveToStream(strm, TEncoding.UTF8);
  finally
    Attachment.FinishTempStream;
  end; 

  smtpClient.Send(msg);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top