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

发布于 2024-12-04 10:59:10 字数 4325 浏览 3 评论 0原文

我们希望使用 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 αβγδ άᾶὰἂ

到目前为止,一切都很好。

如果我们将上述文件加载到 TIdMessageBody 属性中,并通过电子邮件发送它,它会显示如下:

Abcd ???? ?ᾶὰἂ

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

TIdMessage 组件的属性如下:

TIdMessage Properties

我尝试设置 CharSetWindows-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(使用 Notepad++) - 它看起来是一样的(我在 html 的 meta 信息中手动更改了字符集)。又发消息了。结果:ABCD ???2?3?? ??ᾶὰἂ

We want to send through email, using D-XE and Indy's TIdMessage component the following htm file as body:

<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"'>αβγδ άᾶὰἂ </span></p>

</div>

</body>

</html>

(Ok, the actual file is different but the problem is the same).

In the above file, if you'll save it as temp.htm and load it in the Internet Explorer, you'll see 4 latin characters, 4 Greek characters without tone and 4 Greek characters with tone (variations of Alpha - the first letter of Greek alphabet). Something like this:

Abcd αβγδ άᾶὰἂ

So far, so good.

If we load the above file in the Body property of the TIdMessage and send it through email it shows like this:

Abcd ???? ?ᾶὰἂ

As you see, the greek letters from the monotonic alphabet are replaced with ???? ? - tested using Mozilla Thunderbird 3 on WinXP.

The properties of the TIdMessage component are as follows:

TIdMessage Properties

I tried to set the CharSet to Windows-1253 but no luck.

Any ideas how this can work?

UPDATE:

Answering your questions:

The raw message source after it was received is: (the email addresses were redacted)

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"'>???? ?ᾶὰἂ </span></p>

</div>

</body>

</html>

Mozilla Thunderbird says also Message Encoding: Western (ISO-8859-1). I tried to put in the IdMessage component different encodings like windows-1253 (Greek) or UTF-8 - the result was the same. Also, I tried to convert the htm file to UTF-8 (using the Notepad++) - it looked the same (I changed the charset by hand in the html's meta info). Sent the message again. The result: Abcd ???2?3?? ??ᾶὰἂ

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夏了南城 2024-12-11 10:59:10

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

如果将 HTML 加载到 TIdMessage.BodyTIdText.Body 属性中,则必须将数据解码为 UTF-16(因为是 Body 属性在 XE 中使用的内容),然后将 TIdMessage.CharSetTIdText.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;

If you look at your own screenshots, you will see that TIdMessage and the transmitted email are both set to use US-ASCII as the CharSet. That is why your data is getting altered.

If you load the HTML into the TIdMessage.Body or TIdText.Body property, you have to decode the data to UTF-16 (since that is what the Body property uses in XE) and then set the TIdMessage.CharSet or TIdText.CharSet property to windows-1253 so the UTF-16 data gets re-encoded properly when the email is sent, eg:

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

Or:

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;

If you load the HTML into a TIdAttachment object instead, then you don't have to decode/encode anything manually, since the attachment data is sent as-is.

with TIdAttachmentFile.Create(IdMessage.MessageParts, 'file.htm') do
begin
  ContentType := 'text/html';
end;
凉城已无爱 2024-12-11 10:59:10

尝试设置ContentTransferEncoding,例如设置为quoted-printable。请记住,邮件仍然使用 7 位字符(除非服务器宣称它可以处理 8 位或二进制数据),因此需要适当的传输编码。

Try to set ContentTransferEncoding, for example to quoted-printable. Remember that mail still uses 7-bit charcters (unless a server advertise it can handle 8-bit or binary data), thereby a proper transfer encoding is needed.

枉心 2024-12-11 10:59:10

我使用 Indy 10 和 Delphi XE2(Unicode 标准字符串)
将 Message CharSet 设置为“ISO-8859-7”并使用 UTF8Encode 将文本添加到正文

TempMess := TIdMessage.Create(self);
TempMess.CharSet :='ISO-8859-7';
TempMess.Body.Add(UTF8Encode('Kαλnμέρα!!!'));

I use Indy 10 and Delphi XE2 (Unicode std Strings)
setting Message CharSet to 'ISO-8859-7' and adding text to body using UTF8Encode

TempMess := TIdMessage.Create(self);
TempMess.CharSet :='ISO-8859-7';
TempMess.Body.Add(UTF8Encode('Καλημέρα!!!'));

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文