HTML text in the body of an email (delphi code)

I use the TIdMessage component with Delphi XE2 for sending email from an application
I want the body of the email to be HTML text
So i put these lines in the Body of the TidMessage :

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Strict//EN"
<HTML>
<HEAD>
<TITLE>Title...</TITRE>
</HEAD>
 <BODY> Text... </BODY>
</HTML>

There is also an attachement to the email I send
If I do not set the ContentType property of the TidMessage, the recipient receives an email with an attachment, but the body looks exactly like the example above
Now if I set the ContentType property of the TidMessage to 'text/html', the recipient receives an email without attachment, but with something like this in the body ;

This is a multi-part message in MIME format --60YuqVcoHk37WTm=_6Ty64BkuN4X4FCMT6 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline
--60YuqVcoHk37WTm=_6Ty64BkuN4X4FCMT6 Content-Type: application/octet-stream; name="TheAttachmentFileName" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="TheAttachmentFileName" Q2x1YiBkZSBCcmlkZ2UgU2FpbnQgTGFtYmVydCAtIFRvdXJub2kgZH etc etc ...

What shall I change to have the attachment correctly ... attached and the text correctly displayed ?
LVL 1
LeTayAsked:
Who is Participating?
 
Geert GConnect With a Mentor Oracle dbaCommented:
what are you trying to send ?

html body with attachments referenced inside the html ?
> multipart/related

if the attachment is a standalone text file, for the user to open/read/save etc then the ContentType
> multipart/mixed
0
 
Geert GOracle dbaCommented:
There are very clear instructions on how to send messages with indy  here
http://www.indyproject.org/sockets/blogs/rlebeau/2005_08_17_a.en.aspx

all is explained starting from sending simple text to adding attachments
0
 
LeTayAuthor Commented:
I used the sample as referenced by Geert
The compiler does not like this :
  with TIdText.Create(IdMessage1.MessageParts, nil) do begin
      Body.Text := 'HTML goes here';
      ContentType := 'text/html';
    end;
It says E2003 Undeclared identifier : TIdText !
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
LeTayAuthor Commented:
Sorry, found the problem
I did not "uses" IdText, so compilation is fixed
But now, the receiver gets this : an email with the correct attachment but the message body is completely empty !
When asking (Outlook) to show the body source, I get what I put in the body :

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Strict//EN"
<HTML>
<HEAD>
<TITLE>The title ...</TITRE>
</HEAD>
<BODY>The text of the body here ...</BODY>
</HTML>
0
 
LeTayAuthor Commented:
To be complete, here is the code for text and attachment :

M  := TIDMessage.Create(Application);
M.ContentType := 'multipart/related; type="text/html"';
 with M do
  begin
   with TIdText.Create(M.MessageParts,nil) do begin
     Body.Text   := MessageBody;   // Message body built somewhere else, content is in my previous comment 
     ContentType := 'text/html';
   end;
       with TIDAttachmentFile.Create(MessageParts,TheAttachmentFileName])
        do begin
          FileName    := ExtractFileName(TheAttachmentFileName);
          ContentID   := FileName;
          ContentType := 'text/plain';
        end;
  end;

Open in new window

0
 
LeTayAuthor Commented:
Yes I realised that and changed it to multipart/mixed
But the result is the same, the attachment looks correct, but the body of the message is empty, except that when asking to view its sources, it shows the html as text as I described earlier
0
 
LeTayAuthor Commented:
ps : I can forward you the email I got as a result, if you want ...
0
 
Geert GOracle dbaCommented:
i'll try a sample project
and see where it goes wrong
0
 
LeTayAuthor Commented:
Thanks, waiting ...
0
 
Geert GOracle dbaCommented:
i was going to indicate to use the IdMessage.Body ...
but after testing it, and finally noting there is a note in Remy Lebeau's website about that
I came up with this solution :
procedure TForm1.Button1Click(Sender: TObject);
var m: TIdMessage;
  List: TStrings;
  f: TIdAttachmentFile;
  mailer: TIdSMTP;
  mt1: TIdText;
begin
  mailer := TIdSMTP.Create(nil);
  try
    mailer.Host := 'your_smtp_host';
    mailer.Port := 25;

    m := TIdMessage.Create(nil);
    try
      m.From.Address := 'your_email_address';
      m.Recipients.EMailAddresses := 'your_email_address';
      m.Subject := 'Test mail';

      m.ContentType := 'multipart/alternative';
      // m.Body.Text := '<table border="1"><tr><td>Test column</td></tr><tr><td>Data Test</td></tr></table>';

      mt1 := TIdText.Create(m.MessageParts, nil);
      mt1.Body.Text := '<html><body><table border="1"><tr><td>Test column</td></tr><tr><td>Data Test</td></tr></table></body></html>';
      mt1.ContentType := 'text/html';

      List := TStringList.Create;
      try
        List.Add('this is a plain text file');
        List.SaveToFile('c:\temp\plain.txt');
      finally
        List.Free;
      end;
      f := TIdAttachmentFile.Create(m.MessageParts, 'c:\temp\plain.txt');
      f.ContentType := 'text/plain';
      f.ContentID := 'plain.txt';

      mailer.Connect;
      mailer.Send(m);
    finally
      m.Free;
    end;
  finally
    mailer.Free;
  end;
end;

Open in new window

0
 
Geert GOracle dbaCommented:
in outlook i then see this:
screenshot mail
0
 
LeTayAuthor Commented:
Many thanks for your help
In fact my code was correct (multipart/mixed)
The error (mine) was that the html body contained a tag error !
And so Outlook did not show anything in the body
I corrected it and it is fine now
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.