Indy使用UTF-8字符的接收者发送电子邮件

时间:2015-02-25 10:55:43

标签: delphi email utf-8 smtp indy

我尝试在Indy 10中发送包含TIdSMTP组件的电子邮件,并且我的接收器列表中包含Unicode字符(如Роман Безяк <roman_bezjak@yahoo.com>)。但是当邮件发送时,我会在电子邮件的To标题中看到:"?????????" <roman_bezjak@yahoo.com>。任何人都可以帮我解决这个编码问题吗?

这是我的程序的样子:

procedure TMailClientForm.btnSendEmailClick(Sender: TObject);
var
  mes : TIdMessage;
  i : Integer;
begin
  with SMTPClient do begin
    Host := serverHost;
    Port := SmtpServerPort;
    Username := myUserName;
    Password := myPassword;
    UseTLS := utUseImplicitTLS;
  end;
  try 
    mes := tidmessage.Create(nil);
    try
      with mes do begin
        ContentType := 'text/plain';
        ClearBody;
        Body.Text := memoEmailBody.Text;
        Subject := txtEmailSubject.text;
        From.Address := SMTPClient.Username; 
        From.Name := myName; // cyrillic symbols!
        Recipients.Add.Address := myReceiver; // cyrillic symbols! like 'Роман Безяк <roman_bezjak@yahoo.com>'
        CharSet := 'utf-8';
      end;
      if fileNames.Count > 0 then // attachments - the files are in the stringlist fileNames
        mes.ContentType := 'multipart/mixed';
      for i := 0 to fileNames.count - 1 do begin
        if FileExists(fileNames[i]) then
          TIdAttachmentFile.Create(mes.MessageParts, fileNames[i]);
      end;
      try
        try
          try
            SMTPClient.Connect;
          except
            on e : Exception do begin
              MessageDlg('ERROR=' + SMTPClient.LastCmdResult.Text.Text, mtError,   [mbOK], 0);
              Exit;
            end;
          end;
          try
            SMTPClient.Send(mes);            
          except
            on e : Exception do begin
              MessageDlg('ERROR=' + SMTPClient.LastCmdResult.Text.Text, mtError, [mbOK], 0);
              Exit;
            end;
          end;
        finally
          if SMTPClient.Connected then
            SMTPClient.Disconnect;
        end;
        fileNames.clear;
      except
        on e:exception do begin
          MessageDlg(e.message, mtError, [mbOK], 0);
        end;
      end;
    finally
      mes.Free;
    end;
  except
    on e:exception do begin
      MessageDlg(e.message, mtError, [mbOK], 0);
    end;
  end;
end;

1 个答案:

答案 0 :(得分:3)

Recipients.Add.Address := myReceiver; // cyrillic symbols! like 'Роман Безяк <roman_bezjak@yahoo.com>'

如果myReceiver同时包含姓名和电子邮件地址,则需要使用TIdEMailAddressItem.Text属性而不是TIdEMailAddressItem.Address属性:

Recipients.Add.Text := myReceiver; // cyrillic symbols! like 'Роман Безяк <roman_bezjak@yahoo.com>'
// Name becomes 'Роман Безяк'
// Address becomes 'roman_bezjak@yahoo.com'...

TIdEmailAddressItem.Text属性setter方法解析输入字符串并相应地将其拆分为TIdEmailAddressItem.NameTIdEmailAddressItem.Address属性。

TIdEmailAddressItem.Address属性根本没有setter方法,因此无论你分配什么都按原样使用。

对电子邮件进行编码时,如果存在任何非ASCII字符,TIdEmailAddressItem.Name值将按RFC 2047进行MIME编码。由于假设电子邮件地址仅包含ASCII字符(Unicode电子邮件地址确实存在但尚未常用),因此TIdEmailAddressItem.Address值未进行MIME编码。电子邮件标题必须是ASCII格式,因此您看到Роман Безяк变为?????????,因为您将其粘贴在TIdEmailAddressItem.Address属性中并且按原样转换为ASCII(其中非ASCII字符变为?)没有进行MIME编码。

因此,通过将NameAddress分开,您应该会看到Роман Безяк被正确处理。