I'm trying to send eMail via code using TIdSMTP but I'm having problems encoding Greek. I have the code
IdMessage1.Clear;
IdMessage1.CharSet := 'UTF-8';
with IdMessage1.Recipients.Add do begin
Address := eMail.text; // a TEdit component
Name := eMail.text;
end;
IdMessage1.From.Name := 'Ελληνικό κείμενο'; // doesn't transfer Greek
IdMessage1.From.Address := 'noreply@server.gr';
IdMessage1.MsgId := IdMessage1.From.Address := 'κείμενο στα Ελληνικά';
IdMessage1.Subject := 'Ελληνικό κείμενο eMail';
IdMessage1.Body.add(UTF8Encode('Ελληνικά : '+aStringValue); // only this Line shows Greek chars
IdMessage1.Body.add('');
IdMessage1.Body.add(UTF8Encode('άλλο κείμενο στα Ελληνικά')); // this Line shows strange chars
IdMessage1.CharSet := 'utf-8';
IdMessage1.ContentTransferEncoding := 'base64';
TRY
Connect();
Send(IdMessage1);
FINALLY
disconnect();
END;
The message is sent but there are strange characters in place of Greek.
If I write the three add commands as one ie.
IdMessage1.Body.Add(UTF8Encode('Ελληνικά: ' + aStringValue
+#13#13'άλλο κείμενο στα Ελληνικά'));
then they all appear normally in Greek.
If I use the function EncodeString(const AText, ACharSet: string): string;
var
Encoder: TIdEncoderMIME;
begin
Encoder := TIdEncoderMIME.Create(nil);
try
Result := Format('=?%s?B?%s?=', [ACharSet, Encoder.EncodeString(AText, IndyTextEncoding_UTF8)]);
finally
Encoder.Free;
end;
end;
and send the message with IdMessage1.From.Name := EncodeString('Ελληνικό κείμενο', 'UTF-8');
then the message displays Greek in the "From" field.
But no matter what I do I can't get it to work with the "subject" field.
ChatGPT gave me the above solutions but it failed for the "subject" field.
Has anyone encountered these problems and found solutions?