使用TcpClient类连接到smtp.live.com [英] Connecting to smtp.live.com with the TcpClient class
问题描述
我正在尝试使用TcpClient类连接到smtp.live.com.这里有一个很棒的连接Gmail的示例:测试SMTP服务器是否通过C#运行
I'm trying to connect to smtp.live.com using the TcpClient class. There is a wonderful example of connecting to Gmail here: Testing SMTP server is running via C#
不幸的是,当更新它以与smtp.live.com一起使用时,调用AuthenticateAsClient方法时,我收到"IOException:握手由于意外的数据包格式而失败."
Unfortunately, when updating this to work with smtp.live.com, I'm getting an "IOException: The handshake failed due to an unexpected packet format" when the AuthenticateAsClient method is called.
我该如何解决?
class Program
{
static void Main(string[] args)
{
using (var client = new TcpClient())
{
var server = "smtp.live.com";
var port = 25;
client.Connect(server, port);
using (var stream = client.GetStream())
using (var sslStream = new SslStream(stream))
{
// Getting an IOException here
sslStream.AuthenticateAsClient(server);
using (var writer = new StreamWriter(sslStream))
using (var reader = new StreamReader(sslStream))
{
writer.WriteLine("EHLO " + server);
writer.Flush();
Console.WriteLine(reader.ReadLine());
}
}
}
Console.WriteLine("Press Enter to exit...");
Console.ReadLine();
}
}
我尝试在AuthenticateAsClient中指定SslProtocol.Tls或Ssl3都不起作用.
I tried specifying the SslProtocol in AuthenticateAsClient. Neither Tls or Ssl3 worked.
还尝试提供RemoteCertificateValidation的回调,以防服务器证书无效时始终返回true.那也不起作用.
Also tried providing a callback for RemoteCertificateValidation that alway returned true just in case the server cert was invalid. That didn't work either.
注意:请不要建议我使用SmtpClient.我需要更多的控制权.
NOTE: Please do not suggest that I use SmtpClient; I need more control than it provides.
推荐答案
感谢您将我带入正确的轨道.smtp.live.com服务器需要以下事件序列:
Thanks to nos for putting me on the right track. The smtp.live.com servers require the following sequence of events:
- 连接
- HELO-在发送STARTTLS之前,它将不接受
- STARTTLS-显然,这将服务器设置为接受加密连接
- SslStream.AuthenticateAsClient()-这似乎使C#框架和SMTP服务器了解":)
- 现在我们已经建立了加密连接,通常的SMTP命令就可以使用
无论如何,此代码对端口587上的smtp.live.com和smtp.gmail.com均有效:
Anyway, this code works for both smtp.live.com AND smtp.gmail.com on port 587:
class Program
{
static void Main(string[] args)
{
const string server = "smtp.live.com";
const int port = 587;
using (var client = new TcpClient(server, port))
{
using (var stream = client.GetStream())
using (var clearTextReader = new StreamReader(stream))
using (var clearTextWriter = new StreamWriter(stream) { AutoFlush = true })
using (var sslStream = new SslStream(stream))
{
var connectResponse = clearTextReader.ReadLine();
if (!connectResponse.StartsWith("220"))
throw new InvalidOperationException("SMTP Server did not respond to connection request");
clearTextWriter.WriteLine("HELO");
var helloResponse = clearTextReader.ReadLine();
if (!helloResponse.StartsWith("250"))
throw new InvalidOperationException("SMTP Server did not respond to HELO request");
clearTextWriter.WriteLine("STARTTLS");
var startTlsResponse = clearTextReader.ReadLine();
if (!startTlsResponse.StartsWith("220"))
throw new InvalidOperationException("SMTP Server did not respond to STARTTLS request");
sslStream.AuthenticateAsClient(server);
using (var reader = new StreamReader(sslStream))
using (var writer = new StreamWriter(sslStream) { AutoFlush = true })
{
writer.WriteLine("EHLO " + server);
Console.WriteLine(reader.ReadLine());
}
}
}
Console.WriteLine("Press Enter to exit...");
Console.ReadLine();
}
}
这篇关于使用TcpClient类连接到smtp.live.com的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!