我写了一个C#程序,它连接到本地SMTP中继以发送电子邮件。它会观察EML文件的目录并将它们发送到SMTP中继。对我来说重要的是,电子邮件是在没有任何修改的情况下发送的。 该程序按预期工作,但它有中文编码的问题。我不知道在哪里搜索这个问题 - 是我的程序还是接力?
以下是我要发送的示例(EML文件的内容):
这就是我发送时收到的内容:
我认为编码时不应该有任何问题,因为我正在读取文件,因为它是并读取二进制文件,所以它应该是一个精确的副本。
查看我的代码,也许您可以找到任何问题:
using (var client = new TcpClient())
{
client.ReceiveTimeout = 500;
client.Connect(endPt);
using (var stream = client.GetStream())
{
using (var writer = new StreamWriter(stream) { AutoFlush = true })
using (var reader = new StreamReader(stream))
{
//Some boring authentification, not shown here ...
sendData(writer, reader, "DATA", false, DELAY_COMMANDS);
sendDataBinary(writer, reader, info.BinaryContent, false, DELAY_COMMANDS);
sendData(writer, reader, "", false, DELAY_COMMANDS);
sendData(writer, reader, "\n", false, DELAY_COMMANDS);
sendData(writer, reader, "\n", false, DELAY_COMMANDS);
sendData(writer, reader, "\n", false, DELAY_COMMANDS);
sendData(writer, reader, "\n", false, DELAY_COMMANDS);
sendData(writer, reader, ".", false, DELAY_COMMANDS);
sendData(writer, reader, "QUIT", false, DELAY_COMMANDS);
Console.Write("End of mail sending..");
}
client.Close();
}
}
}
static void sendDataBinary(StreamWriter writer, StreamReader reader, byte[] data, bool bBase64 = false, int iWait = 0)
{
writer.BaseStream.Write(data, 0, data.Length);
writer.WriteLine();
System.Threading.Thread.Sleep(iWait);
Console.WriteLine(reader.ReadLine());
}
static void sendData(StreamWriter writer, StreamReader reader, string data, bool bBase64 = false, int iWait = 0)
{
if (bBase64)
writer.WriteLine(Convert.ToBase64String(Encoding.UTF8.GetBytes(data)));
else
writer.WriteLine(data);
System.Threading.Thread.Sleep(iWait);
Console.WriteLine(reader.ReadLine());
}
也许你们中的任何人都可以帮助我,谢谢!
答案 0 :(得分:0)
尝试使用其中一个StreamWriter构造函数重载并指定类似UTF8的编码
答案 1 :(得分:0)