我有一个应用程序扫描电子邮件帐户的返回邮件。它使用POP3,并在多个客户端系统上成功运行。但是,对于一个客户端,当我们尝试连接时,我们得到一个SocketException - 没有这样的主机是已知的。使用POP3连接到SSL
我的第一个想法是无论是地址还是端口都无法访问,但他们回来说它是一个SSL端口,我认为我的代码可能无法处理SSL。但是,当我打电话给tcpClient = new TcpClient(Host, Port);
时,发生错误,所以我回到了我以前的假设。 TcpClient需要连接到SSL端口的特殊方式吗?
我的第二个问题是,是否有一种简单的方法可以将代码转换为使用SSL,而无需创建常规的POP3连接类和SSL POP3连接类?我相信我需要使用SslStream
而不是StreamReader
,这意味着我将不得不修改任何访问POP3服务器的代码,因为SslStream
没有ReadLine()
方法。
我在下面添加了我的初始连接代码(或重要位)。
try
{
tcpClient = new TcpClient(Host, Port);
}
catch (SocketException e)
{
logger.Log(...);
throw (e);
}
String response = "";
try
{
streamReader = new StreamReader(tcpClient.GetStream());
// Log in to the account
response = streamReader.ReadLine();
if (response.StartsWith("+OK"))
{
response = SendReceive("USER ", UserName.Trim() + "@" + Domain.Trim());
if (response.StartsWith("+OK"))
{
response = SendReceive("PASS ", Password);
}
}
if (response.StartsWith("+OK"))
result = true;
}
catch (Exception e)
{
result = false;
}
的SendReceive
方法很简单:
private String SendReceive(String command, String parameter)
{
String result = null;
try
{
String myCommand = command.ToUpper().Trim() + " " + parameter.Trim() + Environment.NewLine;
byte[] data = System.Text.Encoding.ASCII.GetBytes(myCommand.ToCharArray());
tcpClient.GetStream().Write(data, 0, data.Length);
result = streamReader.ReadLine();
}
catch { } // Not logged in...
return result;
}
这似乎是主要的ReadLine()
方法不起作用,但在看完了顾名思义是难以阅读与线因为你不知道它是否发送完毕。是这样的,还是我只需要写一个快速的方法来阅读,直到我打到\r
或\n
?
尝试使用Tls10或Tls11 ,也许GMail不支持TLS v1.2。另外,GMail不支持USER/PASS;它只支持SASL AUTH。 – jstedfast