加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

无法使用delphi indy发送UTF-8电子邮件

发布时间:2020-12-15 03:55:55 所属栏目:大数据 来源:网络整理
导读:这是我的代码 电子邮件正文有一些unicode字符 LSMTP := TIdSMTP.Create(nil); try LMsg := TIdMessage.Create(LSMTP); try with LMsg do begin Subject := Subj; Recipients.EMailAddresses := Email; WriteToLog(cInformation,'To: '+Recipients.EMailAddre
这是我的代码

电子邮件正文有一些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';

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读