0

I am trying to send Email from my program via Outlook with embedded image. But a picture is not showing in the email. I set CID in HTML and property by attachment. Here is my simple code: For test you can create form and put button on it, replace E-Mailadress and picture path.

unit unit1;

interface

uses
 Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, OutlookXP, comobj,  System.Classes, Vcl.Graphics,
 Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, system.Diagnostics;

type
 TForm1 = class(TForm)
   Button1: TButton;
   procedure Button1Click(Sender: TObject);
 private
   { Private-Deklarationen }
 public
   { Public-Deklarationen }
 end;

 TOutlookEmail = class
 private
   FOutlookApp: OleVariant;
 public
   constructor Create;
   destructor Destroy; override;
   procedure SendEmailWithEmbeddedImage(const aRecipient, aSubject, aBody, aImagePath: string);
 end;

var
 Form1: TForm1;



implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
 b : String;
 mailOut : TOutlookEmail;
begin
 mailout := TOutlookEmail.Create;
 
 b := '<html><bodystyle=' +
 'font-family:"Tahoma";font-style:normal;font-weight:normal;font-size:8pt' +
 '><fontcolor="#000000">Hello world<IMG src="cid:1" width="606" height="606" ></font></body></html>';

 mailout.SendEmailWithEmbeddedImage('[email protected]', 'Test', b, 'C:\Delphi\1.png');
end;

constructor TOutlookEmail.Create;
begin
 inherited;
 FOutlookApp := CreateOleObject('Outlook.Application');
end;

destructor TOutlookEmail.Destroy;
begin
 FOutlookApp := Unassigned;
 inherited;
end;

procedure TOutlookEmail.SendEmailWithEmbeddedImage(const aRecipient, aSubject, aBody,
 aImagePath: string);
var
 MailItem,
 Attachment: OleVariant;
begin
   MailItem := FOutlookApp.CreateItem(0);

   MailItem.Subject := ASubject;
   MailItem.To := aRecipient;
   mailitem.BodyFormat := olFormatHTML;
   MailItem.htmlBody := aBody;

   if FileExists(aImagePath) then
   begin
     Attachment := MailItem.Attachments.Add(aImagePath);
     Attachment.PropertyAccessor.SetProperty('http://schemas.microsoft.com/mapi/proptag/0x3712001F', '<1>');
   end;
   MailItem.Send;
   MailItem := Unassigned;
end;

end.

If i do display instead of send. Outlook shows me image in the mail, after i press send button it reach recipient with only empty shape and picture as attachment.

I need some help how can I do it right. With best regards.

8
  • you could base64 encode the image into the html Sep 25 at 11:07
  • Use another name for the CID, p.e. ImageId1 not just a number
    – Garada
    Sep 25 at 11:43
  • @complete_stranger , I am not sure that is right way to encode pictures into base64. It will be very large HTML-Text. But its work. I tried it out.
    – sausagerus
    Sep 25 at 11:50
  • @Garada , I did it with "image001" too. Above is just an example. btw it doesnt work.
    – sausagerus
    Sep 25 at 11:52
  • 1
    The image shows inline in the email. Did you try image001 without <> symbols in HTML and the MAPI property?
    – Garada
    Sep 25 at 12:32

1 Answer 1

2

It seems that a name with only a number fails as a CID name, this is a working code using an alphanumeric CID name

var
  OL,
  MailItem,
  Attachment: OleVariant;
begin
  OL := CreateOleObject('Outlook.Application');
  MailItem := OL.CreateItem(0);

  MailItem.Subject := 'Subject';
  MailItem.To := '[email protected]';
  mailitem.BodyFormat := olFormatHTML;
  MailItem.htmlBody := '<html>' +
                       '<body style=font-family:"Tahoma";font-style:normal;font-weight:normal;font-size:8pt>' +
                       '<fontcolor="#000000">Hello world<IMG src="cid:ImageId1" width="606" height="606"></font></body></html>';

  Attachment := MailItem.Attachments.Add('C:\PathTo\1.png');
  Attachment.PropertyAccessor.SetProperty('http://schemas.microsoft.com/mapi/proptag/0x3712001F', 'ImageId1');

  MailItem.Recipients.ResolveAll;
  MailItem.Send;
  MailItem := Unassigned;
  OL := Unassigned;
end;
1
  • 1
    it works with numbers too, my problem was <> symbols in attachment property
    – sausagerus
    Sep 25 at 12:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.