I am working with sending mails through the smtp protocol using the Indy idMessage object in delphi 7 (therefore the Indy version is 9). I was sending messages in html format without problems, but now I would like to embed an image in the body of the message. I saw that it would not be as easy as putting:
<img src='C:\Foo\image.png'>
From what I saw, you have to initialize an IdAttachment and reference it in the html, but I couldn't make it work as such.
Next I leave the code used to create the body of the message
procedure TfmMail.SendMail;
var
IdMensaje: TIdMessage;
smtp: TIdSMTP;
begin
IdMensaje := TIdMessage.Create(nil);
IdMensaje.Clear;
IdMensaje.Body.Clear;
IdMensaje.ContentType := 'text';
IdMensaje.From.Text := 'Title from email';
IdMensaje.Body.Text := 'greeting';
IdMensaje.ContentType := 'text/html';
//<img src='C:\Foo\image.png> -> Don't work
IdMensaje.Body.Text := IdMensaje.Body.Text + '<p> Other text to body </p>;
You can either point the
imgtag to an external url (keep in mind that Outlook will block image downloads by default unless the user explicitly clicks on "Download images") oryou can add images as attachments, set their
Content-IdMIME header (e.g. to'xyz') on the attachment MIME part, then refer to that image in the HTML body though that content-id, e.g.,<img src="cid:xyz">Third option would be embedding image data in the
imgtag, but not all email clients understand that - older versions of Outlook won't render images like that:<img src="data:image/jpeg;base64, LzlqLzRBQ..." />