Home > Software engineering >  How to save the whole raw email on the file system?
How to save the whole raw email on the file system?

Time:08-10

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: <[email protected]>
Delivered-To: [email protected]
Received: from server.mail  (unknown [122.130.30.20])   by server.mail 
Message-ID: <[email protected]>
Date: Wed, 10 Aug 2022 12:41:28  0200
Subject: test_subject
From: [email protected]
To: [email protected]
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: <[email protected]>
Delivered-To: [email protected]
Message-ID: <[email protected]>
Date: Wed, 10 Aug 2022 12:41:28  0200
Subject: test_subject
From: [email protected]
To: [email protected]
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.

CodePudding user response:

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

  • Related