【问题标题】:How to send a email with a base64 embedded image如何发送带有 base64 嵌入图像的电子邮件
【发布时间】:2014-01-25 05:41:53
【问题描述】:

场景:我有一个 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/html 和 image/jpeg、alternative/mixed 等...

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

【问题讨论】:

  • 不是重复的。你提到的图像不是base64的问题,开发语言是php而不是delphi
  • 但是 MIME 部分很有趣也很有用,谢谢。
  • 请将您的一封电子邮件的正文粘贴您遇到问题的图片附件。
  • 您使用哪个 tcp 库来创建邮件?超字节ICS?印地?亚拉腊突触?

标签: html image delphi email mime-types


【解决方案1】:

我自己找到了解决办法,问题出在 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字段返回图像的Stream。

而函数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

现在可以完美运行了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-07
    • 2021-10-31
    • 2011-04-16
    • 1970-01-01
    • 2015-07-29
    • 1970-01-01
    • 2012-12-08
    相关资源
    最近更新 更多