Search code examples
delphiindy10

How to use TMemo for email message body in Delphi


I have a TMemo object with about 30 lines (some text, some blank lines) of text in it representing the message body of an email I want the application to send. In the code below I'm passing the text in the TMemo object to IdMessage1.Body.Text (Indy 10), but my resulting email message body appears that it's just one continuous line wrapped. I would like to see the spacing I have that contains some blank lines. If I copy the contents from the Lines parameter of the TMemo and paste it into a text editor I can see the CR & LF at the end of the lines.

Does the CR & LF get stripped off in this translation, should I be using a different component rather than TMemo for this, or am I passing the lines in the TMemo to the IdMessage1.Body.Text incorrectly?

IdMessage1.Body.Text := EmailBodyMemo.Text;
IdMessage1.ContentType := 'text/html';

Solution

  • The TMemo.Text property returns the entire content as a single string without any "soft" (word-wrapping) line breaks inserted between each line, only "hard" line breaks (explicit CR/LF characters).

    Use this instead:

    IdMessage1.Body.Text := EmailBodyMemo.Lines.Text;
    

    Lines.Text returns a string that has each line separated by Lines.LineBreak (CRLF by default on Windows).

    Or better, use this instead:

    IdMessage1.Body := EmailBodyMemo.Lines;
    

    Which is the same as doing this:

    IdMessage1.Body.Assign(EmailBodyMemo.Lines);