Question

Using: Delphi XE2, Windows 8 with US-English as default language

I am writing an email client with Delphi. I'm using TIdIMAP4 to connect to a GMail mailbox via IMAP and getting the message list like this:

  var
   MessageList: TIdMessageCollection;
  begin
   IMAPClnt.SelectMailBox('INBOX');
   IMAPClnt.UIDRetrieveAllEnvelopes(IMAPClnt.MessageList);

Then I'm retrieving the message subjects like this:

  var
   IdMsg: TIdMessage;
   s: String
  begin
   for c := 0 to FIMAPClnt.MessageList.Count - 1 do
    begin
     IdMsg := FIMAPClnt.MessageList[c];
     s :=  IdMsg.Subject;

However, if the message subject is in a different language (say, Hebrew) then the message subjects are not displayed properly (see attached image) even on a computer with Hebrew set as the default Windows language.

How can I correct the code to ensure that it works properly, retrieving the language in the correct Unicode characters?

Screen capture: enter image description here

TIA.

Était-ce utile?

La solution

The email headers in your screenshot have been encoded per RFC 2047 ("MIME Part Three: Message Header Extensions for Non-ASCII Text"). TIdIMAP4.UIDRetrieveAllEnvelopes() captures and stores the raw data and does not automatically decode it. You can use the various Decode...() functions in the IdCoderHeaader.pas unit to decode the headers manually, eg:

uses
  ..., IdCoderHeader;

var
  IdMsg: TIdMessage;
  s: String
begin
  ...
  for c := 0 to FIMAPClnt.MessageList.Count - 1 do
  begin
    IdMsg := FIMAPClnt.MessageList[c];
    IdMsg.Subject := DecodeHeader(IdMsg.Subject);
    DecodeAddresses(IdMsg.FromList);
    DecodeAddress(IdMsg.Sender);
    DecodeAddresses(IdMsg.ReplyTo);
    DecodeAddresses(IdMsg.Recipients);
    DecodeAddresses(IdMsg.CCList);
    DecodeAddresses(IdMsg.BccList);
    ...
  end;
  ...
end;
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top