[英]Processing email from outlook 7bit encoding causes funny characters in output
我正在開發自己的SMTP服務器的項目。 (請問沒人問為什么,或者給我提供Postfix之類的東西,我有我的理由)。
除了使用Outlook以外,其他大多數情況下都可以正常工作,但從Outlook編碼的數據似乎似乎存在編碼問題。
我不斷獲得如下內容:
<html xmlns:v=3D"urn:schemas-microsoft-com:vml" =
xmlns:o=3D"urn:schemas-microsoft-com:office:office" =
xmlns:w=3D"urn:schemas-microsoft-com:office:word" =
代替:
<html xmlns:v="urn:schemas-microsoft-com:vml" =
xmlns:o="urn:schemas-microsoft-com:office:office" =
xmlns:w="urn:schemas-microsoft-com:office:word" =
請注意,有效內容上沒有3D。
我有一個函數可以監聽套接字中的SMTP數據,如下所示:
if (stream.CanRead)
{
byte[] serverData = new byte[1024];
StringBuilder stringBuilder = new StringBuilder();
int numberOfBytesRead = 0;
do
{
numberOfBytesRead = stream.Read(serverData, 0, serverData.Length);
Encoding encoding = Encoding.GetEncoding("UTF-7", new FallbackEncoding(), new FallbackDecoding());
stringBuilder.AppendFormat("{0}", encoding.GetString(serverData, 0, numberOfBytesRead));
} while (stream.DataAvailable);
return stringBuilder.ToString();
在我的FallbackDecoding函數中,我有以下代碼
class FallbackDecoding : DecoderFallback
{
public override int MaxCharCount
{
get
{
return 1;
}
}
public override DecoderFallbackBuffer CreateFallbackBuffer()
{
return new Buffer();
}
private class Buffer : DecoderFallbackBuffer
{
private int _fallbackIndex;
private string _fallbackString;
public override int Remaining
{
get
{
return _fallbackString.Length - _fallbackIndex;
}
}
public override bool Fallback(byte[] bytesUnknown, int index)
{
byte unknownChar = bytesUnknown[index];
_fallbackString = Encoding.ASCII.GetString(new[] { (byte)(unknownChar & 127) });
_fallbackIndex = 0;
return true;
}
public override char GetNextChar()
{
if (Remaining > 0)
{
return _fallbackString[_fallbackIndex++];
}
else
{
return '\0';
}
}
public override bool MovePrevious()
{
if (_fallbackIndex > 0)
{
_fallbackIndex--;
return true;
}
return false;
}
}
出於某種原因,解碼器后退類在public override bool Fallback
函數中引發異常。 它會引發異常,因為bytesunknown
在數組中只有1個項目,但是index
參數是128,因此它拋出了索引超出范圍的異常,但我不知道為什么。
我嘗試將ASCII更改為UTF-7,因為Outlook以7位發送數據,但似乎沒有任何區別。
由於我收到的電子郵件中的HTML,當我傳遞電子郵件時,格式錯誤,有時我會在電子郵件中得到垃圾。
根據要求提供完整的電子郵件標題
Message-ID: <000d01d0dc52$0c0d4690$2427d3b0$@chrisboard.co.uk>
MIME-Version: 1.0
Content-Type: multipart/alternative;
boundary="----=_NextPart_000_000E_01D0DC5A.6DD24AD0"
X-Mailer: Microsoft Outlook 15.0
Thread-Index: AdDcUeHbbPyOUTipQ462DEYroR+DWg==
Content-Language: en-gb
This is a multipart message in MIME format.
------=_NextPart_000_000E_01D0DC5A.6DD24AD0
Content-Type: text/plain;
charset="us-ascii"
Content-Transfer-Encoding: 7bit
This is the content of the message
------=_NextPart_000_000E_01D0DC5A.6DD24AD0
Content-Type: text/html;
charset="us-ascii"
Content-Transfer-Encoding: quoted-printable
<html xmlns:v=3D"urn:schemas-microsoft-com:vml" =
xmlns:o=3D"urn:schemas-microsoft-com:office:office" =
xmlns:w=3D"urn:schemas-microsoft-com:office:word" =
xmlns:m=3D"http://schemas.microsoft.com/office/2004/12/omml" =
xmlns=3D"http://www.w3.o
=
html附件使用quoted-printable encoding編碼 。 Quoted-printable使用以=
開頭的特殊3字節序列。 引用的可打印編碼=
=3D
。 它是唯一必須編碼的可打印ASCII字符(33-126)。
BTW =
行尾也是帶quoted-printable
編碼的乘積。 它“折斷”了長行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.