[英]Indy send email with receivers having UTF-8 characters
我正在嘗試在Indy 10中發送帶有TIdSMTP
組件的電子郵件,並且我的接收器列表中包含Unicode字符(例如Роман Безяк <roman_bezjak@yahoo.com>
)。 但是當郵件被發送時,在電子郵件的To
標題中我看到: "?????????" <roman_bezjak@yahoo.com>
"?????????" <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;
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.Name
和TIdEmailAddressItem.Address
屬性。
TIdEmailAddressItem.Address
屬性根本沒有setter方法,因此無論你分配什么都按原樣使用。
編碼電子郵件時,如果存在任何非ASCII字符,則TIdEmailAddressItem.Name
值TIdEmailAddressItem.Name
RFC 2047進行MIME編碼。 TIdEmailAddressItem.Address
值未進行MIME編碼,因為假設電子郵件地址僅包含ASCII字符(Unicode電子郵件地址確實存在但尚未常用)。 電子郵件標題必須是ASCII格式,因此您會看到Роман Безяк
成為?????????
因為你把它粘在TIdEmailAddressItem.Address
屬性中並且它TIdEmailAddressItem.Address
轉換為ASCII(其中非ASCII字符變為?
)而不進行MIME編碼。
因此,通過將Name
與Address
分開,您應該看到Роман Безяк
正確處理。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.