I placed one TWPRichEdit and a TWPToolBar on a Form.
Than I entered some formated text into the editor and added an image.
The content of the editor needs to be saved as HTML in order to insert it into the body of an E-mail.
For sending the Email I use Indy 10 -> TIdSMTP and TIdMessage.
As far as I know you cannot use HTML in the body of an HTML file. HTML is always attached to a message. So you need to create a HTML file with all the images on same directory and add all of it to the mail as attachments.
Since Julian is on his vacation i have dug up this code that i wrote a long time ago. This code has not been used or maintained since, so i can't be certain if it will still work, but it is something we are going to use sometime soon.
TCsTreeDoc is an override class for TWpRichtext.
procedure TCsTreeDoc.PrepareRTFAttachments(pvTempFolder: string);
{* @bug [2251] 21.09.2005 LE procedure updates all Image objects inside the RTF to
support linked images. If no link exists then a file for the image object is created
in the folder pvTempFolder. All paths to the images are stored in the list FAttachments.
Attachment list can be retrieved using TCsTreeDoc.Attachments *}
var
i : integer;
begin
FAttachments.Clear;
for i := 0 to textObjects.ObjCount - 1 do
begin
with TextObjects.ObjList[i] do
begin
if ObjType = wpobjImage
then if objref <> nil then
begin
if ObjRef.StreamName = '' then //if image isn't linked to a file then a file
//will be created and the object linked to this file
begin
inc(FlinkedImgID); //increase ID to make sure the Filename is unique.
ObjRef.StreamName := //Link the image...
ObjRef.SaveToFile( //..to the location where we save the file
pvTempFolder,
Format( 'img~%d', [FlinkedImgID] ), '' {)});
ObjRef.FileName := ObjRef.StreamName; //Link for HTML mail.
end;
// Add to the attachment list if file is not yet included in this list
// Multiple images can be linked to the same file
if FAttachments.IndexOf(ObjRef.StreamName) = -1
then FAttachments.Add(ObjRef.StreamName);
end;
end;
end;
I think WpTools will generate HTML with the correct Content Identifiers for the attachments. These content identifiers must match the content identifiers in the email file (content-id) for the TIdAttachment object.
I hope I found the problem for my issue. Currently I'm able to sent the email, but images that are included in the HTML aren't visible in Outlook Express (they are in MS Outlook). This has to do with the reference to the image in the created HTML-file.
<img src="Img1.JPG" style="width:1.04in;height:0.78in;"/>
Should be altered to:
<img src="cid:Img1.JPG" style="width:1.04in;height:0.78in;"/>
MS Outlook is more lenient about that kind of error, but the HTML-code should be fixed for other readers.