gpt4 book ai didi

c# - Mailbridge 非 SSL 到 SSL 服务器

转载 作者:太空宇宙 更新时间:2023-11-03 15:14:33 25 4
gpt4 key购买 nike

我尝试为没有 SSL 支持的旧应用程序编写一个“Mailbridge”(我知道缓冲区对于完整的邮件来说太小了)。 (应用程序连接到“本地主机”/我的服务器,我的服务器连接到真正的 SSL 邮件服务器)我的问题:

{
TcpClient tcpClient = (TcpClient)client;
NetworkStream clientStream = tcpClient.GetStream();


TcpClient sslmail = new TcpClient();
SslStream sslStream;

sslmail.Connect(server, sslport);
sslStream = new SslStream(sslmail.GetStream());

sslStream.AuthenticateAsClient(server);
byte[] buffer = new byte[4096];




int bytes=0;

while((sslmail.Connected) && (tcpClient.Connected))
{

try
{

Array.Clear(buffer, 0, buffer.Length);


bytes=sslStream.Read(buffer, 0, buffer.Length);

MessageBox.Show("To Client: "+Encoding.ASCII.GetString(buffer, 0, bytes));

clientStream.Write(buffer, 0, buffer.Length);
clientStream.Flush();




Array.Clear(buffer, 0, buffer.Length);



bytes=clientStream.Read(buffer, 0, buffer.Length);
MessageBox.Show("To Server: "+Encoding.ASCII.GetString(buffer, 0, bytes));

sslStream.Write(buffer, 0, buffer.Length);
sslStream.Flush();
}
catch (Exception err)
{
MessageBox.Show(err.Message);
}


/////////////////////////////////////
}

sslmail.Close();
tcpClient.Close();
}

连接有效:

To Client: +OK POP server ready H migmx018 0LaT8c-1ZFbyb06RD-00m4N0
To Server: USER xxxxxx
To Client: +OK password required for user "xxxxxx"

就是这样,现在它“挂”在

bytes=clientStream.Read(buffer, 0, buffer.Length);

使用 Windows Live Mail 进行测试,然后我使用自己的简单邮件应用程序,但如果我发送“PASS mypass”,服务器返回“-ERR 未知命令”(使用的服务器:pop.gmx.net)(使用 Windows Livemail 直接连接到服务器工作没有问题)

最佳答案

好的,我解决了问题,下面的代码可以工作,但是对于大邮件来说有点慢,因为超时

{
TcpClient tcpClient = (TcpClient)client;
NetworkStream clientStream = tcpClient.GetStream();



TcpClient sslmail = new TcpClient();
SslStream sslStream;

sslmail.Connect(server, sslport);
sslStream = new SslStream(sslmail.GetStream());

sslStream.AuthenticateAsClient(server);
int buffersize=1024;
byte[] buffer = new byte[buffersize];




int bytes=0;

while((sslmail.Connected) && (tcpClient.Connected))
{

try
{

Array.Clear(buffer, 0, buffer.Length);


try
{
sslStream.ReadTimeout = 100;
bytes = sslStream.Read(buffer, 0, buffer.Length);


clientStream.Write(buffer, 0, bytes);
clientStream.Flush();


}
catch (Exception err)
{
//timeout, server is waiting for client
}


Array.Clear(buffer, 0, buffer.Length);



try
{
clientStream.ReadTimeout = 100;
bytes = clientStream.Read(buffer, 0, buffer.Length);



sslStream.Write(buffer, 0, bytes);
sslStream.Flush();
}
catch (Exception err)
{
//timeout, client is waiting for Server
}
}
catch (Exception err)
{
sslmail.Close();
tcpClient.Close();
MessageBox.Show(err.Message);
}


/////////////////////////////////////
}

sslmail.Close();
tcpClient.Close();
}
}

关于c# - Mailbridge 非 SSL 到 SSL 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29113624/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com