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

如何使用Poco++库在文件系统中保存完整原始邮件?

问题:使用Poco++库无法保存完整邮件结构

需要保存邮件服务器接收的完整邮件(包含附件二进制内容及正文),以便后续传入解密函数。但通过继承MailMessage类实现的WriteRawContent方法,调用write或writeMultipart仅能保存部分内容,无法包含正文和附件数据。


现有实现代码

MyMailMessage.h

class MyMailMessage: public MailMessage {
  public:
    bool WriteRawContent(int i);
};

MyMailMessage.cpp

bool MyMailMessage::WriteRawContent(int i)
{
  std::cout << "Write Raw Content" << std::endl;
  std::ofstream outfile("email.eml");
  MessageHeader messageheader;
  if(isMultipart()){
    writeMultipart(messageheader, outfile);
  }
  else{
    write(outfile);
  }
  std::cout << "Wrote it" << std::endl;
  outfile.close();
  return true;
}

当前问题现象

  • 调用write方法的输出仅包含邮件头和空的多部分边界:
Return-Path: <mail-test@cartellini.info>
Delivered-To: mail-test2@cartellini.info
Received: from server.mail  (unknown [122.130.30.20])   by server.mail 
Message-ID: <12314142test@webmail.it>
Date: Wed, 10 Aug 2022 12:41:28 +0200
Subject: test_subject
From: email-test@webmail.com
To: email-test2@webmail.com
User-Agent: Web-mail
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="----=_20220810124128_35847"
X-Priority: 3 (Normal)
Importance: Normal

------=_20220810124128_35847--
  • 调用writeMultipart方法的输出仅包含部分MIME头和空边界:
Content-Type: multipart/mixed; boundary="----=_20220810124128_35847"
Mime-Version: 1.0


------=_20220810124128_35847--

期望的完整邮件格式

Return-Path: <mail-test@webmail.com>
Delivered-To: mail-test2@webmail.com
Message-ID: <232131mail-test@webmail.com>
Date: Wed, 10 Aug 2022 12:41:28 +0200
Subject: test_subject
From: mail-test@webmail.com
To: mail-test2@webmail.com
User-Agent: WebMail
MIME-Version: 1.0
Content-Type: multipart/mixed;boundary="----=_20220211113523_23522"
X-Priority: 3 (Normal)
Importance: Normal

------=_20220211113523_23522
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 8bit


------=_20220211113523_23522
Content-Type: text/html; name="example.html"
Content-Transfer-Encoding: 8bit
Content-Disposition: attachment; filename="example.html"
<html>
<head><title>Email from webmail server</title></head>
<body>
<br>
Hi email test
</body>
</html>
------=_20220211113523_23522
Content-Type: application/xml; name="example.xml"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="example.xml"

PDS9DSAKQWEK123òSAD9213031ASDKSADSAKA9DSSDAU1293219KSADSAKSDA9213
PD93MNbnasdoaKSDAOWEQLDOWQELDWQDOSAD.SAD

可行解决方案

直接使用POP3ClientSession的retrieveMessage方法保存原始完整邮件,再读取处理:

核心函数定义

void retrieveMessage(
    int id,
    std::ostream & ostr
);

实现代码片段

POP3ClientSession ppcs(MAIL_SERVER);
for (unsigned int i = 1; i <= messageInfoVec.size(); i++) {
      std::ofstream outfile("email_" + std::to_string(i) + ".eml");
      // 保存原始完整邮件
      ppcs.retrieveMessage(i, outfile);
      outfile.close();

      // 读取保存的邮件进行业务处理
      std::ifstream infile("email_" + std::to_string(i) + ".eml");
      mailMessage.read(infile, partHandler);
      ProcessEmail(mailMessage, partHandler);
      infile.close();
}

内容的提问来源于stack exchange,提问作者Gus Evan

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.22 23:45:45