5

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;
walther
  • 13,466
  • 5
  • 41
  • 67
Phillip Roux
  • 173
  • 2
  • 10
  • Are you using HTML format? Are you using a CID URL for the embedded image? – Ben Jun 16 '12 at 10:23
  • @Ben Should he use HTML format? Is there a solution involving a CID URL? i'm sure he'll use whatever your answer gives - as long as you're using MAPI and it works when the user's e-mail client is Outlook. – Ian Boyd Oct 12 '12 at 13:31
  • 1
    @IanBoyd, I expect he **is** using a CID URL *now he knows about them*... – Ben Oct 12 '12 at 13:42
  • @Ben Well maybe he shouldn't; maybe CID URLs are the problem. – Ian Boyd Oct 12 '12 at 13:47
  • @IanBoyd I expect he just misformatted the CID url, fixed it himself, and didn't bother to come back and tell us. – Ben Oct 12 '12 at 14:39
  • 1
    @IanBoyd, I have posted a potential answer. – Ben Oct 15 '12 at 09:25

1 Answers1

1

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:

Community
  • 1
  • 1
Ben
  • 34,935
  • 6
  • 74
  • 113