Search results for 'Mail Header Encoding'. 1 post(s) found.

  1. 2009/07/20 How to send text/html based email in UTF-8 with TIdSMTP, TIdMessage VCL
2009/07/20 10:22

How to send text/html based email in UTF-8 with TIdSMTP, TIdMessage VCL


I had problem in encoding mail header with IdSMTP, IdMessage. I found some references in Internet, but the problem is the delivered mail subject is always filled with question marks except english letters.

I found the answer to clear the problem. Here's the answer for above problem
{
  Programmed 2009
  by Chunun Kang (kurapa@kurapa.com)
}

uses ..... , IdSMTP, IdMessage, IdCoderMIME; // must be added

procedure TMainFrm.DoInitializeISO(var VHeaderEncoding: Char; var VCharSet:string);
begin
  VHeaderEncoding := 'B';
  VCharSet := 'UTF-8';
end;

procedure TMainFrm.KMAIL( sTo, title, body:string);
var
  IdMsg : TIdMessage;

  function Base64Encode(const Text : utf8string): string;
  var
    Encoder : TIdEncoderMime;
  begin
    Encoder := TIdEncoderMime.Create(nil);
    try
      Result := Encoder.EncodeBytes( TBytes(Text));
    finally
      FreeAndNil(Encoder);
    end
  end;
begin
  IdMsg := TIdMessage.Create(nil);
  try
    with TIdSMTP.Create(nil) do
    try
      UserName := SMTPFrm.m_username.Text;
      Password := SMTPFrm.m_password.Text;
      Host := SMTPFrm.m_host.Text;

      IdMsg.OnInitializeISO := DoInitializeISO;
      IdMsg.ContentType := 'text/html';
      IdMsg.CharSet := 'UTF-8';
      IdMsg.ContentTransferEncoding := 'BASE64';

      IdMsg.Body.Add(body);
      IdMsg.Recipients.emailAddresses := sTo;

      // The mail subject must be encoded in mail header as below.
      IdMsg.Subject := '=?UTF-8?b?' + Base64Encode(ansitoutf8(title)) + '?=';

      // string type address must be given. ex) kurapa@kurapa.com
      IdMsg.From.Address := SMTPFRm.m_email.Text;
      // string type address must be given. ex) Chunun Kang
     IdMsg.From.Name := SMTPFRm.m_realname.Text

      Connect;
      Send(IdMsg);
      Disconnect;
    finally
      Free;
    end;
  finally
    IdMsg.free;
  end;
end;

procedure TMainFrm.Button2Click(Sender: TObject);
begin
  KMAIL( 'kurapa@kurapa.com', 'Message Text in Korean - 한글로 보내는 메일', '제대로 <font color=green>메일</font>이 보내지는지 궁금합니다.');
end;

In above example, the application is trying to send email in Korea, and the result is successful.
Trackback 0 Comment 0

Trackback : Cannot send a trackbact to this post.