这是我的代码
电子邮件正文有一些unicode字符
LSMTP := TIdSMTP.Create(nil);
try
LMsg := TIdMessage.Create(LSMTP);
try
with LMsg do
begin
Subject := Subj;
Recipients.EMailAddresses := Email;
WritetoLog(cinformation,'To: '+Recipients.EMailAddresses);
From.Address := ReplaceVariables(From_Address);
From.Name := ReplaceVariables(From_Name);
Body.Text := EmailMessage;
ContentTransferEncoding := '8bit';
CharSet := 'UTF-8';
ContentType := 'text/plain';
end;
这就是我得到的
Content-Type: text/plain; charset=us-ascii <<<<< WRONG Content-transfer-encoding: 8bit Date: Fri,23 Mar 2012 17:53:19 +0000
使用delphi 2009
解决方法
那是设计上的.当设置ContentType属性时,如果输入未明确指定字符集,则属性setter可以使用默认值更新CharSet属性.某些内容类型(尤其是文本/领域中的内容类型)具有在各种RFC中指定的特定字符集默认值. Indy尽力遵循这些规则.因此,在设置ContentType属性后,需要将CharSet属性设置为预期值,如您所发现的:
//LMsg.CharSet := 'UTF-8'; LMsg.ContentType := 'text/plain'; LMsg.CharSet := 'UTF-8';
您也可以这样做:
LMsg.ContentType := 'text/plain; charset=UTF-8';