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);