Solved

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

Posted on 2016-09-10
12
326 Views
Last Modified: 2016-09-11
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 ?
0
Comment
Question by:LeTay
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
12 Comments
 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 41792775
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
 

Author Comment

by:LeTay
ID: 41793185
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
 

Author Comment

by:LeTay
ID: 41793187
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:LeTay
ID: 41793189
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
 
LVL 37

Accepted Solution

by:
Geert Gruwez earned 500 total points
ID: 41793257
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
 

Author Comment

by:LeTay
ID: 41793287
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
 

Author Comment

by:LeTay
ID: 41793288
ps : I can forward you the email I got as a result, if you want ...
0
 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 41793302
i'll try a sample project
and see where it goes wrong
0
 

Author Comment

by:LeTay
ID: 41793319
Thanks, waiting ...
0
 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 41793324
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
 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 41793325
in outlook i then see this:
screenshot mail
0
 

Author Comment

by:LeTay
ID: 41793334
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

Featured Post

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question