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.