LeTay
asked on
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=_6Ty64Bk uN4X4FCMT6 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline
--60YuqVcoHk37WTm=_6Ty64Bk uN4X4FCMT6 Content-Type: application/octet-stream; name="TheAttachmentFileNam e" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="TheAttachmentFil eName" Q2x1YiBkZSBCcmlkZ2UgU2Fpbn QgTGFtYmVy dCAtIFRvdX Jub2kgZH etc etc ...
What shall I change to have the attachment correctly ... attached and the text correctly displayed ?
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=_6Ty64Bk
--60YuqVcoHk37WTm=_6Ty64Bk
What shall I change to have the attachment correctly ... attached and the text correctly displayed ?
ASKER
I used the sample as referenced by Geert
The compiler does not like this :
with TIdText.Create(IdMessage1. MessagePar ts, nil) do begin
Body.Text := 'HTML goes here';
ContentType := 'text/html';
end;
It says E2003 Undeclared identifier : TIdText !
The compiler does not like this :
with TIdText.Create(IdMessage1.
Body.Text := 'HTML goes here';
ContentType := 'text/html';
end;
It says E2003 Undeclared identifier : TIdText !
ASKER
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>
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>
ASKER
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;
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
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
ASKER
ps : I can forward you the email I got as a result, if you want ...
i'll try a sample project
and see where it goes wrong
and see where it goes wrong
ASKER
Thanks, waiting ...
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 :
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;
ASKER
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
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
http://www.indyproject.org/sockets/blogs/rlebeau/2005_08_17_a.en.aspx
all is explained starting from sending simple text to adding attachments