I should save the entire structure of the emails that arrive at a mail server. Using the "poco ++" libraries I was unable to save the email entirely in the file system. For the moment inside the class that inherits from MailMessage I am doing this:
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;
}
With the write method I can only save a part:
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--
and with the writeMultiPart method:
Content-Type: multipart/mixed; boundary="----=_20220810124128_35847"
Mime-Version: 1.0
------=_20220810124128_35847--
I have to save the entire email with also the contents of the attachment or attachments in binary (and of the body if it is sent) like so :
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
so that it is possible to pass the file to the email decryption function and read it later as if it were going to read it directly from the email server.
EDIT
as suggested by @GusEvan i implemented latest function
void retrieveMessage(
int id,
std::ostream & ostr
);
To backup the email in raw format and then process it. Here is the working version of the snippet:
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();
}
The last retrieveMessage() overloaded method should be for you.
void retrieveMessage(
int id,
MailMessage & message,
PartHandler & handler
);
Retrieves the raw message with the given id from the server and copies it to the given output stream.
Check the documentation:
https://docs.pocoproject.org/current/Poco.Net.POP3ClientSession.html#27007