由于目标计算机主动拒绝连接,因此无法建立连接74.125.25.109:993
本文关键字:连接 建立 目标计算机 拒绝 | 更新日期: 2023-09-27 18:21:32
嗨,我正在尝试使用C#中的ImapX库连接到gmail。但是出现这样的错误。由于目标计算机在创建TcpClient(在ImapX内部)时主动拒绝了它,因此无法建立连接。74.125.25.109:993。我在stackoverflow上浏览了几个相同的问题,但没有一个对我有帮助
这是我的密码。
static void Main(string[] args)
{
var client = new ImapClient("imap.gmail.com",993, true);
client.SslProtocol = System.Security.Authentication.SslProtocols.Ssl2;
client.UseSsl = true;
if (client.Connect()) // This method creates new instance of TcpClient which throws error so returning false from catch block method has been described below.
{
if (client.Login("example@gmail.com", "example123"))
{
// login successful
}
Console.WriteLine("Connection Successful...!");
Console.ReadLine();
}
else
{
Console.WriteLine("Connection UnSuccessful...!");
Console.ReadLine();
// connection not successful
}
}
这是ImapX库中的Client方法。
public bool Connect(string host, int port, SslProtocols sslProtocol = SslProtocols.None,
bool validateServerCertificate = true)
{
_host = host;
_port = port;
_sslProtocol = sslProtocol;
_validateServerCertificate = validateServerCertificate;
if (IsConnected)
throw new InvalidStateException("The client is already connected. Please disconnect first.");
try
{
_client = new TcpClient(_host, _port);
if (_sslProtocol == SslProtocols.None)
{
_ioStream = _client.GetStream();
_streamReader = new StreamReader(_ioStream);
}
else
{
_ioStream = new SslStream(_client.GetStream(), false, CertificateValidationCallback, null);
(_ioStream as SslStream).AuthenticateAsClient(_host, null, _sslProtocol, false);
_streamReader = new StreamReader(_ioStream);
}
string result = _streamReader.ReadLine();
_lastActivity = DateTime.Now;
if (result != null && result.StartsWith(ResponseType.ServerOk))
{
Capability();
return true;
}
else if (result != null && result.StartsWith(ResponseType.ServerPreAuth))
{
IsAuthenticated = true;
Capability();
return true;
}
else
return false;
}
catch (Exception)
{
return false;
}
finally
{
if (!IsConnected)
CleanUp();
}
}
提前谢谢。
当您将ImapX与GMail一起使用时,以下代码足以建立连接:
var client = new ImapClient("imap.gmail.com", true);
if (client.Connect()) {
// ...
}
它将使用标准993端口的SSL。如果您想手动指定SSL版本,对于GMail,您需要使用SslProtocols.Default
,这相当于SslProtocols.Ssl3 | SslProtocols.Tls