我使用的是Delphi 2007 Indy版本10.6.1.5188。
当我使用常规的SMTP服务器和TIdSMTP
发送邮件时,一切正常。
但是,当我使用Amazon Simple Email Service SMTP(SES)发送时,味精正文中的所有特殊字符(例如á
,ç
,é
)都被替换为{{ 1}}和¿½
。
我应该怎么做才能解决此问题,为什么只有在使用SES时才会发生?
这是我当前的代码:
�
这是TIDMessage.savetofile的内容:
idsmtp1.Host := 'email-smtp.us-west-2.amazonaws.com';
idsmtp1.username := 'myusername';
idsmtp1.password := 'mypassword';
idsmtp1.Port := 587;
idsmtp1.IOHandler := IdServerIOHandlerSSLOpenSSL1;
idsmtp1.usetls := utUseExplicitTLS;
idsmtp1.UseEhlo := true;
idmessage1.body.text := 'This is a test é á ó ç';
with IdServerIOHandlerSSLOpenSSL1 do
begin
SSLOptions.Method := sslvTLSv1;
SSLOptions.VerifyMode := [];
SSLOptions.VerifyDepth := 0;
end;
idsmtp1.Connect;
idsmtp1.Send(idmessage1);
答案 0 :(得分:3)
Delphi 2007是Delphi的 pre-Unicode 版本,在Indy中使用的所有字符串均为AnsiString
。因此,您需要手动将电子邮件文本编码为8位编码(例如UTF-8),然后将电子邮件的ContentType
和CharSet
属性设置为匹配,以及{{ 1}}属性,因此UTF-8字节可以通过7位电子邮件系统而不会丢失数据。例如:
ContentTransferEncoding
如果您不这样做,那么电子邮件最终将被解释为其他随机字符集,例如US-ASCII,Windows-1252等,这些字符集将不会为您提供所需的结果。
如果您将代码升级到Delphi 2009或更高版本,而Indy中使用的所有字符串都是IdSMTP1.Host := 'email-smtp.us-west-2.amazonaws.com';
IdSMTP1.Username := 'myusername';
IdSMTP1.Password := 'mypassword';
IdSMTP1.Port := 587;
IdSMTP1.IOHandler := IdServerIOHandlerSSLOpenSSL1;
IdSMTP1.UseTLS := utUseExplicitTLS;
IdSMTP1.UseEhlo := True;
IdMessage1.Body.Text := UTF8Encode('This is a test é á ó ç');
IdMessage1.ContentType := 'text/plain';
IdMessage1.CharSet := 'utf-8';
//alternatively
// IdMessage1.ContentType := 'text/plain; charset=utf-8';
IdMessage1.ContentTransferEncoding := 'base64';
with IdServerIOHandlerSSLOpenSSL1 do
begin
SSLOptions.Method := sslvTLSv1;
SSLOptions.VerifyMode := [];
SSLOptions.VerifyDepth := 0;
end;
IdSMTP1.Connect;
IdSMTP1.Send(IdMessage1);
,则仍然需要UnicodeString
分配,但是您可以删除手册{{1 }}通话:
CharSet