簡體   English   中英

如何使用TIdMessage和Delphi XE * UPDATED *發送包含希臘字符的電子郵件*

[英]How to send an email containing Greek characters using TIdMessage and Delphi XE *UPDATED*

我們希望通過電子郵件發送,使用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屬性中加載上述文件並通過電子郵件發送它,則顯示如下:

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

如你所見,單調字母表中的希臘字母被替換為???? - 在WinXP上使用Mozilla Thunderbird 3進行測試。

TIdMessage組件的屬性如下:

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還說Message Encoding: Western (ISO-8859-1) 我試圖在IdMessage組件中添加不同的編碼,如windows-1253(希臘語)或UTF-8 - 結果是一樣的。 此外,我試圖將htm文件轉換為UTF-8(使用Notepad ++) - 它看起來一樣(我在html的meta信息中手動更改了charset)。 再次發送郵件。 結果:Abcd ??? 2?3 ?? ??ᾶὰἂ

如果您查看自己的屏幕截圖,您將看到TIdMessage和傳輸的電子郵件都設置為使用US-ASCII作為CharSet。 這就是你的數據被改變的原因。

如果您加載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;

我使用Indy 10和Delphi XE2(Unicode std Strings)將Message CharSet設置為'ISO-8859-7'並使用UTF8Encode向body添加文本

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

嘗試將ContentTransferEncoding設置為quoted-printable。 請記住,郵件仍然使用7位字符(除非服務器通告它可以處理8位或二進制數據),因此需要正確的傳輸編碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM