处理来自outlook 7bit编码的电子邮件会导致输出中出现有趣的字符
本文关键字:输出 字符 outlook 7bit 编码 处理 电子邮件 | 更新日期: 2023-09-27 18:09:17
我正在进行一个项目,我正在构建自己的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();
在我的回退解码功能中,我有以下代码
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
在数组中只有一个项,但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
带长行和=
的带引号的可打印ASCII文本
html附件使用带引号的可打印编码进行编码。报价可打印使用以=
开头的特殊3字节序列。引用可打印将=
编码为=3D
。它是唯一必须编码的可打印ascii字符(33-126(。
行尾的BTW =
也是quoted-printable
编码的产物。它"打断"了长队。