简体   繁体   中英

Base64 images to gmail in Delphi

I know this topic has been brought up many times but not specifically for Delphi. I am trying to send generated qr codes as inline img via mail. Currently i am succeeding in doing so but only as URL :

<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAABqQAAAakCAYAAA..." width="170" height="170">

But when it comes to Gmail it only adds an empty square. I have looked up many answers how to embedde an img to email such as this 2 posts:

Base64 images to gmail

Gmail blocking small embedded inline images in email template

But i do not understand how to implement the code in answers inside Delphi.

I also found code that gets the img as attachment from disk:

procedure TForm1.Button1Click(Sender: TObject);
var
 oSmtp : TMail;
 cid : WideString;
begin
 oSmtp := TMail.Create(Application);

 // Add embedded image and return the unique identifier of the attachment
 cid := oSmtp.AddInline('c:\test.jpg');

 // Set HTML body format
 oSmtp.BodyFormat := 1;
 // Set HTML body
 oSmtp.BodyText := '<html><body>Hello, this is an embedded <img src="cid:' +
 cid + '"> picture.</body></html>';
end;

// i do not use this code it is just an example of what i have found thus far

Currently all i am doing with img is this:

  HtmlBody = ReplaceStr(HtmlBody, [parameter_img],'<img src="data:image/png;base64,' + StreamToString(imgStream) + '"width="170" height="170"/>');

But i can not save the imgs so i want to insert the base64string directly.

UPDATE

I have managed to get the attachment in my email and it is there but in the text area there is only a blank square.

the img part of the code and original email msg:

<img src="cid:qrcode.png" width="170" height="170" />';
--wpn=_6hoco9kAT3gWlDy6D313EA3BETyyOfe
Content-Type: text/html; charset="windows-1250"
Content-Transfer-Encoding: base64
Content-Disposition: inline

PCFET0NUWVBFIGh0bWwgUFVCTElDICItLy9XM0MvL0RURCBIVE1MIDQuMCBUcmFuc2l0aW9uYWwv
L0VOIiAiaHR0cDovL3d3dy53My5vcmcvVFIvUkVDLWh0bWw0MC9sb29zZS5kdGQiPjxtZXRhIGh0
dHAtZXF1aXY9IkNvbnRlbnQtVHlwZSIgY29udGVudD0idGV4dC9odG1sOyBjaGFyc2V0PXdpbmRv
d3MtMTI1MCI+PGJvZHk+PGltZyBzcmM9Imh0dHBzOi8vbmFpcy1yYXp2b2ouaXBsdXMuc2kvX2Fz
...

--wpn=_6hoco9kAT3gWlDy6D313EA3BETyyOfe
Content-Type: image/png; name="qrcode.png"
Content-Transfer-Encoding: base64
Content-Disposition: inline; filename="qrcode.png"
Content-ID: qrcode.png


--wpn=_6hoco9kAT3gWlDyD6D313EA3BEyyOfe--

At first i thought the issue is this : PCFET0NUWVBFIGh0bWwgUFVCTElDICItL since it does not look like base64 code of png which starts with iVBOR.

But the issue was here : Content-ID: qrcode.png

When setting Content-ID you have to be carefull since in some versions simply setting it with quotation marks is not enough and you have to set it with adding <> like so : Content-ID: <qrcode.png> .

With this the result was as wanted.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM