当前位置 : 主页 > 编程语言 > delphi >

如何使用TIdMessage和Delphi XE * UPDATED *发送包含希腊字符的电子邮件*

来源:互联网 收集:自由互联 发布时间:2021-06-23
我们希望通过电子邮件发送,使用D-XE和Indy的TIdMessage组件将以下htm文件作为正文: htmlheadmeta http-equiv=Content-Type content="text/html; charset=windows-1253"meta name=Generator content="Microsoft Word 12 (filtere
我们希望通过电子邮件发送,使用D-XE和Indy的TIdMessage组件将以下htm文件作为正文:

<html>

<head>
<meta http-equiv=Content-Type content="text/html; charset=windows-1253">
<meta name=Generator content="Microsoft Word 12 (filtered)">
<style>
<!--
 /* Font Definitions */
 @font-face
    {font-family:"Cambria Math";
    panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
    {font-family:Tahoma;
    panose-1:2 11 6 4 3 5 4 4 2 4;}
 /* Style Definitions */
 p.MsoNormal, li.MsoNormal, div.MsoNormal
    {margin:0cm;
    margin-bottom:.0001pt;
    font-size:12.0pt;
    font-family:"Times New Roman","serif";
    color:black;}
.MsoChpDefault
    {font-size:10.0pt;}
@page Section1
    {size:595.3pt 841.9pt;
    margin:72.0pt 90.0pt 72.0pt 90.0pt;}
div.Section1
    {page:Section1;}
-->
</style>

</head>

<body bgcolor=white lang=EL>

<div class=Section1>

<p class=MsoNormal><span lang=EN-US style='font-family:"Tahoma","sans-serif"'>Abcd</span><span
lang=EN-US style='font-family:"Tahoma","sans-serif"'> </span><span
style='font-family:"Tahoma","sans-serif"'>αβγδ ά&#8118;&#8048;&#7938; </span></p>

</div>

</body>

</html>

(好吧,实际文件不同但问题是一样的).

在上面的文件中,如果您将其保存为temp.htm并将其加载到Internet Explorer中,您将看到4个拉丁字符,4个没有音调的希腊字符和4个带有音调的希腊字符(Alpha的变体 – 第一个字母希腊字母表).像这样的东西:

Abcdαβγδᾶὰἂᾶὰἂ

到现在为止还挺好.

如果我们在TIdMessage的Body属性中加载上述文件并通过电子邮件发送它,则显示如下:

A B C D ???? ?ᾶὰἂ

如你所见,单调字母表中的希腊字母被替换为???? ? – 在WinXP上使用Mozilla Thunderbird 3进行测试.

TIdMessage组件的属性如下:

我试图将CharSet设置为Windows-1253,但没有运气.

任何想法如何工作?

更新:

回答你的问题:

收到后的原始消息来源是:(电子邮件地址被编辑)

From - Thu Sep 15 11:11:06 2011
X-Account-Key: account3
X-UIDL: 00007715
X-Mozilla-Status: 0001
X-Mozilla-Status2: 00400000
X-Mozilla-Keys:                                                                                 
Return-Path: [redacted]
X-Envelope-To: [redacted]
X-Spam-Status: No, hits=0.0 required=5.0
    tests=AWL: 0.194,BAYES_20: -0.73,HTML_MESSAGE: 0.001,
    MIME_HEADER_CTYPE_ONLY: 0.56,MIME_HTML_ONLY: 0.001,MISSING_MID: 0.001,
    CUSTOM_RULE_FROM: ALLOW,TOTAL_SCORE: 0.027,autolearn=no
X-Spam-Level: 
Received: from localhost ([127.0.0.1])
    by [redacted]
    for [redacted];
    Thu, 15 Sep 2011 11:10:59 +0300
From: [redacted]
Subject: Test msg
To: [redacted]
Content-Type: text/html; charset=us-ascii
Sender: [redacted]
Reply-To: [redacted]
Disposition-Notification-To: [redacted]
Return-Receipt-To: [redacted]
Date: Thu, 15 Sep 2011 11:10:59 +0300

<html>

<head>
<meta http-equiv=Content-Type content="text/html; charset=windows-1253">
<meta name=Generator content="Microsoft Word 12 (filtered)">
<style>
<!--
 /* Font Definitions */
 @font-face
    {font-family:"Cambria Math";
    panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
    {font-family:Tahoma;
    panose-1:2 11 6 4 3 5 4 4 2 4;}
 /* Style Definitions */
 p.MsoNormal, li.MsoNormal, div.MsoNormal
    {margin:0cm;
    margin-bottom:.0001pt;
    font-size:12.0pt;
    font-family:"Times New Roman","serif";
    color:black;}
.MsoChpDefault
    {font-size:10.0pt;}
@page Section1
    {size:595.3pt 841.9pt;
    margin:72.0pt 90.0pt 72.0pt 90.0pt;}
div.Section1
    {page:Section1;}
-->
</style>

</head>

<body bgcolor=white lang=EL>

<div class=Section1>

<p class=MsoNormal><span lang=EN-US style='font-family:"Tahoma","sans-serif"'>Abcd</span><span
lang=EN-US style='font-family:"Tahoma","sans-serif"'> </span><span
style='font-family:"Tahoma","sans-serif"'>???? ?&#8118;&#8048;&#7938; </span></p>

</div>

</body>

</html>

Mozilla Thunderbird还说消息编码:Western(ISO-8859-1).我试图在IdMessage组件中添加不同的编码,如windows-1253(希腊语)或UTF-8 – 结果是一样的.此外,我试图将htm文件转换为UTF-8(使用记事本) – 它看起来一样(我在html的元信息中手动更改了charset).再次发送邮件.结果:Abcd ??? 2?3 ?? ??ᾶὰἂ

如果您查看自己的屏幕截图,您将看到TIdMessage和传输的电子邮件都设置为使用US-ASCII作为CharSet.这就是你的数据被改变的原因.

如果将HTML加载到TIdMessage.Body或TIdText.Body属性中,则必须将数据解码为UTF-16(因为这是Body属性在XE中使用的),然后设置TIdMessage.CharSet或TIdText.CharSet属性到windows-1253,以便在发送电子邮件时正确地重新编码UTF-16数据,例如:

Enc := CharsetToEncoding('windows-1253');
try
  IdMessage.Body.LoadFromFile('file.htm', Enc);
  IdMessage.ContentType := 'text/html';
  IdMessage.CharSet := 'windows-1253';
finally
  Enc.Free;
end;

要么:

Enc := CharsetToEncoding('windows-1253');
try
  with TIdText.Create(IdMessage.MessageParts, nil) do
  begin
    Body.LoadFromFile('file.htm', Enc);
    ContentType := 'text/html';
    CharSet := 'windows-1253';
  end;
finally
  Enc.Free;
end;

如果您将HTML加载到TIdAttachment对象中,那么您不必手动解码/编码任何内容,因为附件数据按原样发送.

with TIdAttachmentFile.Create(IdMessage.MessageParts, 'file.htm') do
begin
  ContentType := 'text/html';
end;
网友评论