如何发送包含base64嵌入式图像的电子邮件

时间:2014-01-07 13:51:37

标签: html image delphi email mime-types

场景:我有一个delphi 7应用程序,我有一个函数,使用base64编码和MIME类型发送包含嵌入图像的邮件。

电子邮件正文:

the message
<hr>
<img width=213 height=120 src="cid:CidKey"/> <!-- Important! here is the cid reffered to the cid that i previously set on the code! -->
<hr>
some text

问题:我的Gmail中的电子邮件是灰色的,而不是图像。

我尝试过的内容:我尝试将MIME Content-Type等更改为许多类型,例如text/htmlimage/jpeg,{{ 1}}等...

我想要什么:我想简单地发送一封可以在base64中包含可见图像的签名(某些文字)的电子邮件。

1 个答案:

答案 0 :(得分:0)

我自己找到了解决方案,问题出在MIME上。

之前,我发送的电子邮件只有一种MIME类型:Content-type: text/html

现在,我创建了一个TMimeMess,其TMimepart包含Multiparts

var
  m               : TMimemess;
  SubPartSignature,
  mix             ,
  rel             : TMimepart;

  m := TMimemess.create;

  mix := m.AddPartMultipart('mixed',nil);
  rel := m.AddPartMultipart('related', mix);

  m.AddPartHTML(FstlMensagemHTML, mix);//note that the FstlHTMLMessage is the body html of the email
  SubPartSignature := m.AddPartHTMLBinary(cprdbutils.getBlob_StringStream(dts.FieldByName('bl_logo')), 'logo.jpg',   'CidKey', rel);
  rel.AddSubPart;
  rel.AssignSubParts(SubPartSignature);

函数cprdbutils.getBlob_StringStream(dts.FieldByName('bl_logo'))从数据库blob字段返回图像的流。

函数AddPartHTMLBinary()在这里:

function TMimeMess.AddPartHTMLBinary(const Stream: TStream; const FileName, Cid: string; const PartParent: TMimePart): TMimepart;
begin
  Result := AddPart(PartParent);
  Result.DecodedLines.LoadFromStream(Stream);
  Result.MimeTypeFromExt(FileName);
  Result.Description := 'Included file: ' + FileName;
  Result.Disposition := 'inline';
  Result.ContentID := Cid;
  Result.FileName := FileName;
  Result.EncodingCode := ME_BASE64; //this is a constant containing base64
  Result.EncodePart;
  Result.EncodePartHeader;
end;

这是电子邮件的正文HTML:

the message
<hr>
<img width=213 height=120 src="cid:CidKey"/> <!-- Important! here is the cid reffered to the cid that i previously set on the code! -->
<hr>
some text

现在它完美无缺。