如何使用TCPCLIENT登陆搭到GMAIL邮箱? 如何使用TCPCLIENT登陆搭到GMAIL邮箱?前面的步骤都可以实现。但是后面的登陆却无法使用! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你打算用什么协议访问?IMAP?POP?HTTP? GMAIL的是SSL方式的登录,你和TCPClient是否符合要求? 就是想知道如何是TCPCLIENT来登陆gmail目的就想知道很多电子邮件列表中那些邮件对于GMAIL来说是合法的电子邮件。 /// <summary> /// 连接邮件服务器 /// </summary> /// <param name="ServerIP">服务器地址</param> /// <param name="Port">服务器端口</param> /// <param name="UserName">用户名</param> /// <param name="UserPassword">密码</param> /// <param name="IsSSL">是否加密</param> /// <returns>是否连接成功</returns> public bool Connect(string ServerIP, int Port, string UserName, string UserPassword, bool IsSSL) { try { MyTcpClient.ReceiveTimeout = 5000; MyTcpClient.SendTimeout = 5000; MyTcpClient.Connect(ServerIP, Port); //MyTcpClient.ReceiveTimeout = 200000; //MyTcpClient.SendTimeout = 20000; Stream stream; if (IsSSL) { SslStream sslStream = new SslStream(MyTcpClient.GetStream(), false); try { sslStream.AuthenticateAsClient(ServerIP); } catch { } stream = sslStream; } else { NetworkStream netstream = MyTcpClient.GetStream(); stream = netstream; } stream.ReadTimeout = 60000; sreader = new StreamReader(stream, Encoding.Default); swriter = new StreamWriter(stream); string result = sreader.ReadLine(); //验证用户和密码 swriter.WriteLine("USER " + UserName); swriter.Flush(); result = sreader.ReadLine(); if (result.Substring(0, 3) == "-ER") { throw new Exception("用户名错误");//没有该用户名; } swriter.WriteLine("PASS " + UserPassword); swriter.Flush(); try { result = sreader.ReadLine(); } catch (IOException) { throw new Exception("密码错误");//未能读取行 } if (result.Substring(0, 3) == "-ER") { throw new Exception("密码错误"); //无法登录,可能使用户名密码错误! } this.Connected = true;//设置连接属性 return true; } catch (Exception err) { throw; } } 接5楼,使用SSL流连接GMail服务器就可以了,上面代码是我写的一个pop类,可以连接Gmail to :ufostop能不能吧整个类发出来我看看! using System;using System.Collections.Generic;using System.Text;using System.IO;using System.Net.Security;using System.Net.Sockets;namespace OpenSSL{ class Program { TcpClient MyTcpClient; StreamReader sreader; StreamWriter swriter; /// <summary> /// 连接邮件服务器 /// </summary> /// <param name="ServerIP">服务器地址</param> /// <param name="Port">服务器端口</param> /// <param name="UserName">用户名</param> /// <param name="UserPassword">密码</param> /// <param name="IsSSL">是否加密</param> /// <returns>是否连接成功</returns> public bool Connect(string ServerIP, int Port, string UserName, string UserPassword, bool IsSSL) { try { MyTcpClient.ReceiveTimeout = 5000; MyTcpClient.SendTimeout = 5000; MyTcpClient.Connect(ServerIP, Port); //MyTcpClient.ReceiveTimeout = 200000; //MyTcpClient.SendTimeout = 20000; Stream stream; if (IsSSL) { SslStream sslStream = new SslStream(MyTcpClient.GetStream(), false); try { sslStream.AuthenticateAsClient(ServerIP); } catch { } stream = sslStream; } else { NetworkStream netstream = MyTcpClient.GetStream(); stream = netstream; } stream.ReadTimeout = 60000; sreader = new StreamReader(stream, Encoding.Default); swriter = new StreamWriter(stream); string result = sreader.ReadLine(); //验证用户和密码 swriter.WriteLine("USER " + UserName); swriter.Flush(); result = sreader.ReadLine(); if (result.Substring(0, 3) == "-ER") { throw new Exception("用户名错误");//没有该用户名; } swriter.WriteLine("PASS " + UserPassword); swriter.Flush(); try { result = sreader.ReadLine(); } catch (IOException) { throw new Exception("密码错误");//未能读取行 } if (result.Substring(0, 3) == "-ER") { throw new Exception("密码错误"); //无法登录,可能使用户名密码错误! } return true; } catch (Exception err) { throw; } } public string OperaStream( string strCmd) { swriter.WriteLine(strCmd); swriter.Flush(); string result = sreader.ReadLine(); return result; } static void Main(string[] args) { Program p = new Program(); p.MyTcpClient = new TcpClient(); if (p.Connect("pop.gmail.com", 995, "[email protected]", "15972572759", true) == true) { Console.WriteLine("Connect Is OK!"); string Cmd = Console.ReadLine(); while (Cmd.ToLower() != "quit") { string temp = p.OperaStream( Cmd); Console.WriteLine(temp); Cmd = Console.ReadLine(); } } } }}这是根据U的帖子修改的。拷贝到VS里面可以直接编译的。共同学习一下! C# 将 XML 转化成 HTML 问题,各位帮帮忙。。。。 急呀 网站集成支付接用什么比较阿訇啊 如果仅仅是对数据进行增删改查的基本操作还用使用业务层吗? c#生成word文档表格 在Winform DataGrid中 仅在行头按下Delete键时 弹出确认对话框 求助:使用Relation后,更新表的问题 有开发过移动CMPP3.0的朋友吗? hotmail邮箱到底还增加空间么?散分 C#.NET中如何实现折叠菜单按钮 在visual studio中如何实现下拉式菜单 tcpclient连接ssl的问题
/// <summary>
/// 连接邮件服务器
/// </summary>
/// <param name="ServerIP">服务器地址</param>
/// <param name="Port">服务器端口</param>
/// <param name="UserName">用户名</param>
/// <param name="UserPassword">密码</param>
/// <param name="IsSSL">是否加密</param>
/// <returns>是否连接成功</returns>
public bool Connect(string ServerIP, int Port, string UserName, string UserPassword, bool IsSSL)
{
try
{
MyTcpClient.ReceiveTimeout = 5000;
MyTcpClient.SendTimeout = 5000;
MyTcpClient.Connect(ServerIP, Port);
//MyTcpClient.ReceiveTimeout = 200000;
//MyTcpClient.SendTimeout = 20000;
Stream stream;
if (IsSSL)
{
SslStream sslStream = new SslStream(MyTcpClient.GetStream(), false);
try
{
sslStream.AuthenticateAsClient(ServerIP);
}
catch
{
}
stream = sslStream;
}
else
{
NetworkStream netstream = MyTcpClient.GetStream();
stream = netstream;
}
stream.ReadTimeout = 60000;
sreader = new StreamReader(stream, Encoding.Default);
swriter = new StreamWriter(stream);
string result = sreader.ReadLine(); //验证用户和密码 swriter.WriteLine("USER " + UserName);
swriter.Flush(); result = sreader.ReadLine();
if (result.Substring(0, 3) == "-ER")
{
throw new Exception("用户名错误");//没有该用户名;
} swriter.WriteLine("PASS " + UserPassword);
swriter.Flush(); try
{
result = sreader.ReadLine();
}
catch (IOException)
{
throw new Exception("密码错误");//未能读取行
} if (result.Substring(0, 3) == "-ER")
{
throw new Exception("密码错误"); //无法登录,可能使用户名密码错误!
}
this.Connected = true;//设置连接属性
return true; }
catch (Exception err)
{
throw;
}
}
能不能吧整个类发出来我看看!
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net.Security;
using System.Net.Sockets;namespace OpenSSL
{
class Program
{
TcpClient MyTcpClient;
StreamReader sreader;
StreamWriter swriter;
/// <summary>
/// 连接邮件服务器
/// </summary>
/// <param name="ServerIP">服务器地址</param>
/// <param name="Port">服务器端口</param>
/// <param name="UserName">用户名</param>
/// <param name="UserPassword">密码</param>
/// <param name="IsSSL">是否加密</param>
/// <returns>是否连接成功</returns>
public bool Connect(string ServerIP, int Port, string UserName, string UserPassword, bool IsSSL)
{
try
{
MyTcpClient.ReceiveTimeout = 5000;
MyTcpClient.SendTimeout = 5000;
MyTcpClient.Connect(ServerIP, Port);
//MyTcpClient.ReceiveTimeout = 200000;
//MyTcpClient.SendTimeout = 20000;
Stream stream;
if (IsSSL)
{
SslStream sslStream = new SslStream(MyTcpClient.GetStream(), false);
try
{
sslStream.AuthenticateAsClient(ServerIP);
}
catch
{
}
stream = sslStream;
}
else
{
NetworkStream netstream = MyTcpClient.GetStream();
stream = netstream;
}
stream.ReadTimeout = 60000;
sreader = new StreamReader(stream, Encoding.Default);
swriter = new StreamWriter(stream);
string result = sreader.ReadLine(); //验证用户和密码 swriter.WriteLine("USER " + UserName);
swriter.Flush(); result = sreader.ReadLine();
if (result.Substring(0, 3) == "-ER")
{
throw new Exception("用户名错误");//没有该用户名;
} swriter.WriteLine("PASS " + UserPassword);
swriter.Flush(); try
{
result = sreader.ReadLine();
}
catch (IOException)
{
throw new Exception("密码错误");//未能读取行
} if (result.Substring(0, 3) == "-ER")
{
throw new Exception("密码错误"); //无法登录,可能使用户名密码错误!
}
return true; }
catch (Exception err)
{
throw;
}
} public string OperaStream( string strCmd)
{
swriter.WriteLine(strCmd);
swriter.Flush();
string result = sreader.ReadLine();
return result;
} static void Main(string[] args)
{
Program p = new Program();
p.MyTcpClient = new TcpClient();
if (p.Connect("pop.gmail.com", 995, "[email protected]", "15972572759", true) == true)
{
Console.WriteLine("Connect Is OK!");
string Cmd = Console.ReadLine();
while (Cmd.ToLower() != "quit")
{
string temp = p.OperaStream( Cmd);
Console.WriteLine(temp);
Cmd = Console.ReadLine(); }
}
}
}
}
这是根据U的帖子修改的。拷贝到VS里面可以直接编译的。共同学习一下!