You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Delphi从OpenPGP加密消息及Multipart邮件提取文本内容?

Delphi邮件内容处理相关问题解答

问题1:从OpenPGP加密的IdMessage中获取TextBody

IdMessage本身没有内置OpenPGP解密能力,需借助第三方OpenPGP库(如Delphi OpenPGP组件、GnuPG的Delphi封装)实现,步骤如下:

  1. 提取加密内容
    OpenPGP加密邮件通常将加密数据放在text/plain类型的BodyPart中,内容开头一般带有-----BEGIN PGP MESSAGE-----标识,遍历BodyParts提取对应内容:
var
  i: Integer;
  EncryptedContent: string;
begin
  for i := 0 to IdMessage1.BodyParts.Count - 1 do
  begin
    if Pos('-----BEGIN PGP MESSAGE-----', IdMessage1.BodyParts[i].ContentText) > 0 then
    begin
      EncryptedContent := IdMessage1.BodyParts[i].ContentText;
      Break;
    end;
  end;
end;
  1. 使用OpenPGP库解密
    以Delphi OpenPGP组件为例,加载私钥后解密内容:
var
  OpenPGP: TOpenPGP;
  DecryptedContent: string;
begin
  OpenPGP := TOpenPGP.Create(nil);
  try
    OpenPGP.PrivateKey.LoadFromFile('your_private_key.asc');
    OpenPGP.PrivateKey.Passphrase := 'your_passphrase';
    DecryptedContent := OpenPGP.DecryptString(EncryptedContent);
    // DecryptedContent即为解密后的文本内容
  finally
    OpenPGP.Free;
  end;
end;

注意:需确保使用的OpenPGP库适配你的Delphi版本,且私钥格式正确。

问题2:提取multipart/mixed类型IdMessage的文本内容

multipart/mixed类型邮件由多个部分(文本、附件等)组成,IdMessage会自动根据boundary解析出所有BodyParts,只需遍历筛选文本类型内容即可:

基础遍历提取

var
  i: Integer;
  TextContent: string;
begin
  TextContent := '';
  for i := 0 to IdMessage1.BodyParts.Count - 1 do
  begin
    if IdMessage1.BodyParts[i].ContentType = 'text/plain' then
      TextContent := TextContent + IdMessage1.BodyParts[i].ContentText + #13#10
    else if IdMessage1.BodyParts[i].ContentType = 'text/html' then
      TextContent := TextContent + IdMessage1.BodyParts[i].ContentText + #13#10;
  end;
  // TextContent为提取到的所有文本内容
end;

递归处理嵌套多部分

若邮件存在嵌套multipart(如multipart/mixed包含multipart/alternative),需递归遍历避免遗漏:

procedure ExtractTextFromPart(APart: TIdMessagePart; var AText: string);
var
  i: Integer;
begin
  if APart is TIdMultipartMessage then
  begin
    for i := 0 to TIdMultipartMessage(APart).BodyParts.Count - 1 do
      ExtractTextFromPart(TIdMultipartMessage(APart).BodyParts[i], AText);
  end
  else if APart.ContentType.StartsWith('text/') then
    AText := AText + APart.ContentText + #13#10;
end;

// 调用示例
var
  TotalText: string;
begin
  TotalText := '';
  ExtractTextFromPart(IdMessage1, TotalText);
end;

问题3:检查邮件内容的正确方法

需遵循MIME规范分类型处理,步骤如下:

  1. 判断Content-Type类型

    • 单类型邮件(如text/plain、text/html):直接通过IdMessage1.TextBody获取纯文本,IdMessage1.HtmlBody获取HTML内容(对应text/html类型)。
    • 多类型邮件(multipart/*):遍历BodyParts,递归处理每个子部分,区分文本、附件、嵌套多部分。
  2. 处理编码问题
    IdMessage会自动处理base64、quoted-printable等编码,但需确保IdMessage1.CharSet设置正确,避免乱码。必要时可手动转换编码:

var
  Encoding: TEncoding;
  RawBytes: TBytes;
  DecodedText: string;
begin
  RawBytes := TEncoding.ASCII.GetBytes(APart.ContentText);
  Encoding := TEncoding.GetEncoding(APart.CharSet);
  try
    DecodedText := Encoding.GetString(RawBytes);
  finally
    Encoding.Free;
  end;
end;
  1. 区分内容与附件
    通过APart.Disposition判断:值为attachment则是附件,无需提取文本;值为inline或空则是内联内容,需处理。

  2. 优先提取纯文本
    若邮件同时包含text/plain和text/html(如multipart/alternative),优先提取text/plain内容,兼容性更强;需HTML内容时再提取对应部分。

内容的提问来源于stack exchange,提问作者陈发条

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 06:37:36