Solved

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

Posted on 2016-09-10
12
119 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
  • 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 

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

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

This article discusses how to create an extensible mechanism for linked drop downs.
Find out what Office 365 Transport Rules are, how they work and their limitations managing Office 365 signatures.
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

778 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