Search code examples
delphidelphi-7indy

Sending email with Indy 9 with an embedded picture


I am trying to add to a program of mine the capability of sending html email via SMTP with Indy 9. If the program only contains text (the text will be in Hebrew so I need to display it right to left, which means that I am using HTML statements), then the email is sent correctly. My problem lays with embedding pictures into the HTML stream.

The HTML stream will use a command like

<IMG SRC="cid:foo4atfoo1atbar.net" ALT="IETF logo">

Whilst the Indy 10 component TIdAttachmentFile has a ComponentID property whose value has to be set to the value that 'cid' references, I can't find where to set the ComponentID property in Indy 9.

At the moment, the code which deals with adding the picture (whose name is in laPicture.text) looks like this

  if laPicture.text <> '' then
  with TIdAttachment.Create (email.MessageParts, laPicture.text) do
   begin
    ContentDisposition:= 'inline';
    ContentType:= 'image/jpeg';
    DisplayName:= ExtractFileName (laPicture.text);
    filename:= ExtractFileName (laPicture.text);
   end;

Where do I define the ContentID?

And, although this is a stupid question, how do I know which version of Indy I have?


Solution

  • TIdAttachment derives from TIdMessagePart, which has a public ContentID property. If your installed version of Indy 9 does not have that property, then you are using an outdated version, so use the ExtraHeaders property instead to add a Content-ID header manually.

    Have a look at the following blog article on Indy's website for more information about working with HTML emails:

    HTML Messages

    Update: so, if the HTML says cid:foo4atfoo1atbar.net then you need to do this in your code to match it:

    with TIdAttachment.Create (email.MessageParts, laPicture.text) do
    begin
      ...
      ContentID := '<foo4atfoo1atbar.net>';
      // or this, if you do not have the ContentID property available:
      // ExtraHeaders.Values['Content-ID'] := '<foo4atfoo1atbar.net>';
    end;
    

    Note that in Indy 9, you have to provide the brackets manually. Indy 10 inserts them for you if they are omitted, eg:

    ContentID := 'foo4atfoo1atbar.net';