Question

I have TIdMessage with text or HTML part and want to save it - unaltered.

Currently my code does it like this:

static_cast<TIdText*>(IdMsg->MessageParts->Items[0])->Body->SaveToFile("file.htm");

Works fine but there is a problem. In one part of the message there is a quoted printable encoding that looks like: =C4=87 - This is UTF-8 code for ć letter. However, Indy converts it to E6 which is the same letter but in Central European encoding (it might be it picks that as target from Windows settings). So the conversion is OK but I'd prefer to save the TIdText attachments in unmodified form - keeping the original encoding.

The encoding of the TIdText is UTF-8 and HTML also contains UTF-8 meta tag.

Is there a way to:

a) keep original encoding - save as "raw" data with just encoding converted to bytes (e.g. for =C4=87 to be saved as binary data C487).

b) set specific encoding, for example if I want to save it in UTF-8 coding?

Was it helpful?

Solution

When TIdMessage parses an email, by default it decodes transfer encodings (quoted printable, etc) to raw bytes and then charset-decodes those bytes to UTF-16 in memory. To avoid that, you would have to set TIdMessage::NoDecode to true before loading the email, but then you will not be able to use the TIdMessage::MessageParts anymore. And in either case, you will not get raw bytes anyway since TIdMessage deals primarily in String data.

Body->SaveToFile(...) is calling the standard VCL TStrings::SaveToFile(), which saves the TString data using the OS default encoding unless you explicitly specify a different encoding in its Encoding parameter, eg:

static_cast<TIdText*>(IdMsg->MessageParts->Items[0])->Body->SaveToFile("file.htm", Sysutils::TEncoding::UTF8);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top