Recent

Author Topic: TIdSMTP problems  (Read 701 times)

dpap

  • New Member
  • *
  • Posts: 36
TIdSMTP problems
« on: January 16, 2025, 03:10:00 pm »
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?

DragoRosso

  • Guest
Re: TIdSMTP problems
« Reply #1 on: January 16, 2025, 06:36:37 pm »
I think (related to old RFC5322, RFC5321 and RFC822) that the address the of e-mail SHOULD be in ASCII SET (numbers, letters and some other chars).

I remember something about a RFC proposal to expand to UTF-8, but I think that it's still not approved.

So, if you use utf-8 chars is highly possible that your e-mail is rejected.
« Last Edit: January 16, 2025, 06:39:56 pm by DragoRosso »

TRon

  • Hero Member
  • *****
  • Posts: 3995
Re: TIdSMTP problems
« Reply #2 on: January 16, 2025, 07:01:01 pm »
I do not have to remember anything anymore thanks to total-recall.

DragoRosso

  • Guest
Re: TIdSMTP problems
« Reply #3 on: January 16, 2025, 08:11:38 pm »

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1470
    • Lebeau Software
Re: TIdSMTP problems
« Reply #4 on: January 17, 2025, 09:30:03 am »
In order to use non-ASCII characters in the header of the email, you need to use the TIdMessage.OnInitializeISO event, eg:

Code: Pascal  [Select][+][-]
  1. procedure TMyForm.IdMessage1InitializeISO(
  2.   var VHeaderEncoding: Char; var VCharSet: string);
  3. begin
  4.   VHeaderEncoding := 'B';
  5.   VCharSet := 'utf-8';
  6. end;
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

 

TinyPortal © 2005-2018