Solved

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

Posted on 2016-09-10
12
77 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
 

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Create high volume marketing opportunities using email signatures with these top 10 DOs and DON'Ts of email signature marketing.
Check out this infographic on what you need to make a good email signature that will work perfectly for your organization.
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …

744 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now