Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embed Image to Outlook using extended mapi functions

I need to embed images in an email and preview the email before it is sent in outlook. CDO and Redemption is not an option.

I have tried the following code, but the images just appears as a little block.

  procedure AddAttachment(FullFileName: String; Attachments: Outlook2000.Attachments; CID: String);
  const
    PR_ATTACH_CONTENT_ID   = $3712001E;
    PR_ATTACH_CONTENT_ID_W = $3712001F; // Unicode
    PR_ATTACH_MIME_TAG     = $370E001E;
    PR_ATTACH_ENCODING     = $37020102;
  var
    IAttach: IMAPIProp;
    Prop: PSPropValue;
    AAttachment: Outlook2000.Attachment;
    FileName: String;
    PropValue: TSPropValue;
    Prop1: TSPropTagArray;
  begin
    FileName := ExtractFileName(FullFileName);
    Prop := nil;
    try
      AAttachment := Attachments.Add(FullFileName, olByValue, 1, FileName);
      IAttach := AAttachment.MAPIOBJECT as IMAPIProp;
      if Assigned(IAttach) then
        try
          PropValue.ulPropTag := PR_ATTACH_MIME_TAG;
          PropValue.Value.lpszA := 'image/jpeg';
          HrSetOneProp(IAttach, @PropValue);
          PropValue.ulPropTag := PR_ATTACH_CONTENT_ID;
          PropValue.Value.lpszA := PAnsiChar(AnsiString(CID));
          HrSetOneProp(IAttach, @PropValue);
        finally
          if Assigned(Prop) then MAPIFreeBuffer(Prop);
          IAttach := nil;
        end;
    except
    end;
  end;
like image 377
Phillip Roux Avatar asked Nov 14 '22 08:11

Phillip Roux


1 Answers

Questioner hasn't posted his HTML text. I suspect the problem is that his CID urls were malformed - hwoever I have not tested this.

If the Content-ID header is set to this:

Content-Type: image/jpeg
Content-Disposition: inline
Content-ID: afd383988e86ad958709@u

Then the HTML should reference it like so:

<img width="100" height="100" href="cid:afd383988e86ad958709@u" />

In particular, the cid URL must have the prefix "cid:" but the content-id header must not. (A guid is a good choice for a content-id, except that it MUST contain an @ symbol. To comply, you can append '@u' to the guid.)

That is sufficent to have the email display correctly at the receiving end. Whether it will make it preview in outlook correctly before sending, I don't know.

You may wish to also see this question:

  • Embedding images into html email with java mail
like image 174
Ben Avatar answered Dec 15 '22 09:12

Ben