Home > Net >  Delphi - Using resource in Email rather than image
Delphi - Using resource in Email rather than image

Time:12-18

In Delphi, I create emails in HTML using the following code to display a signature in the message:

cMsg:= cMsg   ' <img src="BarrysSignature.jpg" '>

Which means I need to have the .jpg available in the current directory (and distribute it with the executable).

I also use these same signature .jpg files elsewhere in my program, but I've loaded them as resources. What would be better in the emails is if I used the resource for the signature in the email, rather than the external .jpg picture.

I've tried a few ways of doing this but can't get it working. Any thoughts, please?

CodePudding user response:

Similar to @Dmitry's answer, in Indy you would also need to attach the image data to an email (the TIdMessage component), assign the attachment's Content-ID header (the TIdMessagePart.ContentID property), and then refer to that ID in the HTML using a cid: URL where needed.

Refer to these blog articles on Indy's website for how to do this:

HTML Messages

New HTML Message Builder class

I do want to mention one thing, though. Where the articles talk about using TIdAttachmentFile for attachments, you actually don't need to save your image resource to a temporary file at all in this situation. You can alternatively derive your own class from TIdAttachment (let's call it TIdAttachmentResource), and have it override the virtual OpenLoadStream() and CloseLoadStream() methods to return/free a TResourceStream to your resource data, respectively (see the source codes for TIdAttachmentFile and TIdAttachmentMemory for examples). Then you can simply add TIdAttachmentResource objects to the TIdMessage.MessageParts collection as needed, and Indy will be able to encode the email using the image resource directly, no file needed.

CodePudding user response:

In Outlook, you will need to extract the resource to a temporary file, add the image as an attachment, set its PR_ATTACH_CONTENT_ID MAPI property, delete the file. Your HTML body would need to reference the image by its content-id, e.g. <img src="cid:xyz">, where "xyz" is the value of the PR_ATTACH_CONTENT_ID property.

See Including Pictures in an Outlook Email

  • Related