如何将整个原始电子邮件保存在文件系统中



我应该保存到达邮件服务器的电子邮件的整个结构。使用";poco++";库我无法将电子邮件完全保存在文件系统中。目前,在从MailMessage继承的类中,我正在这样做:

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;
}

使用写入方法,我只能保存一部分:

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方法:

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

从而可以将文件传递给电子邮件解密功能,并在以后读取它,就好像它将直接从电子邮件服务器读取它一样。

编辑

正如@GusEvan所建议的,我实现了最新的功能

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

以原始格式备份电子邮件,然后进行处理。以下是该片段的工作版本:

POP3ClientSession ppcs(MAIL_SERVER);
std::ofstream outfile("email.eml");
std::ifstream infile("email.eml");
for (unsigned int i = 1; i <= messageInfoVec.size(); i++) {
ppcs.retrieveMessage(i,outfile);
mailMessage.read(infile,partHandler);
ProcessEmail(mailMessage,partHandler);
outfile.close();
infile.close();
}

最后一个retrieveMessage((重载方法应该是为您准备的。

void retrieveMessage(
int id,
MailMessage & message,
PartHandler & handler
);

从服务器检索具有给定id的原始消息,并将其复制到给定的输出流。

检查文档:

https://docs.pocoproject.org/current/Poco.Net.POP3ClientSession.html#27007

相关内容

  • 没有找到相关文章

最新更新