积极答复者
测试SMTP服务器是否可连接

问题
-
请问SmtpClient中只提供邮箱地址,邮箱密码,和SMTP服务器地址,怎么测试邮箱地址和密码是否正确?(就类似邮件客户端的测试连接功能)
fang- 已移动 Sheng Jiang 蒋晟Moderator 2009年5月23日 1:50 类库问题 ([Loc]From:Visual C#)
答案
-
一般邮件客户段的测试连接功能是给自己发一封邮件 然后用pop3协议接收
紫柔版主的头像真叫萌得一个不行啊。。。。- 已标记为答案 韦恩卑鄙 waywaModerator 2009年5月26日 4:48
-
测试发送一下,如果密码不正确会抛出异常,捕获一下看看邮件服务器返回的信息就可以了
@韦恩卑鄙
Hello~在这也碰到你,希望以后多多帮助
James.Ying- 已标记为答案 韦恩卑鄙 waywaModerator 2009年5月26日 4:47
-
请问SmtpClient中只提供邮箱地址,邮箱密码,和SMTP服务器地址,怎么测试邮箱地址和密码是否正确?(就类似邮件客户端的测试连接功能)
fang
try{
SmtpClient client = new SmtpClient();
client.Credentials = new System.Net.NetworkCredential("#######@163.com", "*******");
//注册的邮箱和密码
client.Host = "smtp.163.com";
}
catch(exception e)
{
throw e;
}
可以使用try catch捕获异常,邮件发送会失败。信息里应该包括用户名和密码错误的消息.
you have a dream,you gonna protect it! http://www.cnblogs.com/frank_xl- 已标记为答案 韦恩卑鄙 waywaModerator 2009年5月26日 4:47
-
private bool CheckSmtp(string smtpServer, int port,string username,string password)
{
TcpClient tcpClient = new TcpClient(smtpServer, port);
NetworkStream stream = tcpClient.GetStream();
if(!WaiteFor(stream,"220")) return false ;SendCommand(stream, "AUTH LOGIN\r\n");
if (!WaiteFor(stream, "334")) return false;SendCommand(stream,Base64Encode(username)+ "\r\n");
if (!WaiteFor(stream, "334")) return false;SendCommand(stream, Base64Encode(password) + "\r\n");
if (!WaiteFor(stream, "235")) return false;
return true;}
private bool WaiteFor(NetworkStream stream, string strCode)
{
int StreamSize;
byte[] ReadBuffer = new byte[1024];
StreamSize = stream.Read(ReadBuffer, 0, ReadBuffer.Length);
string Returnvalue = Encoding.Default.GetString(ReadBuffer).Substring(0, StreamSize);Console.WriteLine(Returnvalue);
return Returnvalue.Substring(0, 3).Equals(strCode);
}
private void SendCommand(NetworkStream stream,string strCmd)
{
byte[] WriteBuffer;WriteBuffer = Encoding.Default.GetBytes(strCmd);
stream.Write(WriteBuffer, 0, WriteBuffer.Length);
}private string Base64Encode(string str)
{
byte[] barray;
barray = Encoding.Default.GetBytes(str);
return Convert.ToBase64String(barray);
}
http://feiyun0112.cnblogs.com/- 已标记为答案 韦恩卑鄙 waywaModerator 2009年5月26日 4:47
-
全部回复
-
一般邮件客户段的测试连接功能是给自己发一封邮件 然后用pop3协议接收
紫柔版主的头像真叫萌得一个不行啊。。。。- 已标记为答案 韦恩卑鄙 waywaModerator 2009年5月26日 4:48
-
测试发送一下,如果密码不正确会抛出异常,捕获一下看看邮件服务器返回的信息就可以了
@韦恩卑鄙
Hello~在这也碰到你,希望以后多多帮助
James.Ying- 已标记为答案 韦恩卑鄙 waywaModerator 2009年5月26日 4:47
-
请问SmtpClient中只提供邮箱地址,邮箱密码,和SMTP服务器地址,怎么测试邮箱地址和密码是否正确?(就类似邮件客户端的测试连接功能)
fang
try{
SmtpClient client = new SmtpClient();
client.Credentials = new System.Net.NetworkCredential("#######@163.com", "*******");
//注册的邮箱和密码
client.Host = "smtp.163.com";
}
catch(exception e)
{
throw e;
}
可以使用try catch捕获异常,邮件发送会失败。信息里应该包括用户名和密码错误的消息.
you have a dream,you gonna protect it! http://www.cnblogs.com/frank_xl- 已标记为答案 韦恩卑鄙 waywaModerator 2009年5月26日 4:47
-
private bool CheckSmtp(string smtpServer, int port,string username,string password)
{
TcpClient tcpClient = new TcpClient(smtpServer, port);
NetworkStream stream = tcpClient.GetStream();
if(!WaiteFor(stream,"220")) return false ;SendCommand(stream, "AUTH LOGIN\r\n");
if (!WaiteFor(stream, "334")) return false;SendCommand(stream,Base64Encode(username)+ "\r\n");
if (!WaiteFor(stream, "334")) return false;SendCommand(stream, Base64Encode(password) + "\r\n");
if (!WaiteFor(stream, "235")) return false;
return true;}
private bool WaiteFor(NetworkStream stream, string strCode)
{
int StreamSize;
byte[] ReadBuffer = new byte[1024];
StreamSize = stream.Read(ReadBuffer, 0, ReadBuffer.Length);
string Returnvalue = Encoding.Default.GetString(ReadBuffer).Substring(0, StreamSize);Console.WriteLine(Returnvalue);
return Returnvalue.Substring(0, 3).Equals(strCode);
}
private void SendCommand(NetworkStream stream,string strCmd)
{
byte[] WriteBuffer;WriteBuffer = Encoding.Default.GetBytes(strCmd);
stream.Write(WriteBuffer, 0, WriteBuffer.Length);
}private string Base64Encode(string str)
{
byte[] barray;
barray = Encoding.Default.GetBytes(str);
return Convert.ToBase64String(barray);
}
http://feiyun0112.cnblogs.com/- 已标记为答案 韦恩卑鄙 waywaModerator 2009年5月26日 4:47
-