我正在使用Delphi 2007,Indy版本10.6.1.5188。
当我使用常规的SMTP服务器和TIDSmtp发送邮件时,一切正常。
但是当我使用Amazon Simple Email Service SMTP(SES)发送时,味精正文中的所有特殊字符(如á,ç和é都被替换为½和½的奇怪符号。
我应该怎么做才能解决此问题,以及为什么只有在使用SES时才会发生?
谢谢!
我自己遇到了这个问题,这是我用来确保电子邮件内容正确地以UTF8编码的功能:
procedure SendEmailIndy(
const SMTPServer: string;
const SMTPPort: integer;
const SMTPUserName : string;
const SMTPPassword : string;
const FromName, FromAddress: string;
const ToAddresses: string; //comma "," separated list of e-mail addresses
const CCAddresses: string; //comma "," separated list of e-mail addresses
const BCCAddresses: string; //comma "," separated list of e-mail addresses
const Subject: string;
const EmailBody: string;
const IsBodyHtml: Boolean; //verses Plain Text
const Attachments: TStrings;
UseTLS : Boolean);
var
smtp: TIdSMTP; // IdSmtp.pas
TLSHandler : TIdSSLIOHandlerSocketOpenSSL; // TLS support
msg: TidMessage; // IdMessage.pas
builder: TIdCustomMessageBuilder; //IdMessageBuilder.pas
s: string;
emailAddress: string;
begin
{
Sample usage:
SendEmailIndy(
'smtp.stackoverflow.com', //the SMTP server address
'Spammy McSpamerson', '[email protected]', //From name, from e-mail address
'[email protected], [email protected]', //To addresses - comma separated
'[email protected]', //CC addresses - comma separated
'', //BCC addresses - comma separated
'Here is your sample spam e-mail', //Subject
'<!DOCTYPE html><html><body>Hello, world!</body></html>', //html body
True, //the body is HTML (as opposed to plaintext)
nil); //attachments
}
TLSHandler := nil;
msg := TidMessage.Create(nil);
try
if IsBodyHtml then
begin
builder := TIdMessageBuilderHtml.Create;
TIdMessageBuilderHtml(builder).Html.Text := DecodeTextTags(eMailBody,True);
end
else
begin
builder := TIdMessageBuilderPlain.Create;
end;
try
Try
if Attachments <> nil then for s in Attachments do builder.Attachments.Add(s);
builder.FillMessage(msg);
Except
on E : Exception do {$IFDEF TRACEDEBUG}AddDebugEntry('eMail_'+debugFileExceptions,'Exception : '+E.Message+CRLF+CRLF+'To:'+ToAddresses+CRLF+CRLF+'Content:'+CRLF+eMailBody){$ENDIF};
End;
finally
builder.Free;
end;
msg.From.Name := FromName;
msg.From.Address := FromAddress;
msg.Subject := Subject;
//If the message is plaintext then we must fill the body outside of the PlainText email builder.
//(the PlainTextBuilder is unable to build plaintext e-mail)
if not IsBodyHtml then msg.Body.Text := DecodeTextTags(eMailBody,True);
msg.ContentType := 'text/html';
msg.CharSet := 'UTF-8';
msg.ContentTransferEncoding := '8bit';
//msg.Encoding :=
for s in ToAddresses.Split([',']) do
begin
emailAddress := Trim(s);
if emailAddress <> '' then
begin
with msg.recipients.Add do
begin
//Name := '<Name of recipient>';
Address := emailAddress;
end;
end;
end;
for s in CCAddresses.Split([',']) do
begin
emailAddress := Trim(s);
if emailAddress <> '' then msg.CCList.Add.Address := emailAddress;
end;
for s in BCCAddresses.Split([',']) do
begin
emailAddress := Trim(s);
if emailAddress <> '' then msg.BccList.Add.Address := emailAddress;
end;
smtp := TIdSMTP.Create(nil);
try
smtp.Host := SMTPServer;
smtp.Port := SMTPPort;
smtp.Username := SMTPUserName;
smtp.Password := SMTPPassword;
If UseTLS = True then
Begin
TLSHandler := TIdSSLIOHandlerSocketOpenSSL.Create;
smtp.IOHandler := TLSHandler;
smtp.UseTLS := TIdUseTLS.utUseRequireTLS;
End;
Try
smtp.Connect;
try
try
smtp.Send(msg)
Except
on E : Exception do {$IFDEF TRACEDEBUG}AddDebugEntry('eMail_'+debugFileExceptions,'SMTP Send Exception : '+E.Message+CRLF+CRLF+'To:'+ToAddresses){$ENDIF};
End;
finally
smtp.Disconnect;
end;
Except
on E : Exception do {$IFDEF TRACEDEBUG}AddDebugEntry('eMail_'+debugFileExceptions,'SMTP Connect Exception : '+E.Message+CRLF+CRLF+'To:'+ToAddresses){$ENDIF};
End;
finally
smtp.Free;
If TLSHandler <> nil then TLSHandler.Free;
end;
finally
msg.Free;
end;
end;
此功能的基础完全归功于这里的人和delphi实践。