使用 TcpClient 连接并向 smtp.gmail.com 发送命令
我无法通过 smtp.gmail.com 发送任何命令。
当我使用 465 作为端口号连接时,我可以看到一些混合的字符。
如果我使用任何其他端口号(例如 25 或 587),则会失败并显示“由于意外的数据包格式,握手失败”。
这是我的代码..
public delegate void StatusUpdateEvents(object o, clsStatusEventHandler e);
public event StatusUpdateEvents statusupdate;
clsStatusEventHandler objStatus = new clsStatusEventHandler();
public bool EnableSSL { get; set; }
public string strIpAddress { get; set; }
public int portNo { get; set; }
static public string EncodeTo64(string toEncode)
{
byte[] toEncodeAsBytes
= System.Text.ASCIIEncoding.ASCII.GetBytes(toEncode);
string returnValue
= System.Convert.ToBase64String(toEncodeAsBytes);
return returnValue;
}
public TcpClient GetTcpClient()
{
if(smtpTcpClient==null) smtpTcpClient = new TcpClient();
return smtpTcpClient;
}
public TcpClient ConnectTcpClient()
{
if (smtpTcpClient.Connected) smtpTcpClient.Close();
smtpTcpClient.Connect(strIpAddress, portNo);
return smtpTcpClient;
}
public void UpdateStatus(string strStatus)
{
objStatus.strStatus = strStatus;
if (statusupdate!=null) statusupdate(this, objStatus);
}
bool CertificateValidationCallback(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
return true;
}
public StreamReader GetReadStream()
{
if (EnableSSL == true)
{
smtpNetworkStream = smtpTcpClient.GetStream();
if (smtpStreamReader == null) { smtpStreamReader = new StreamReader(GetSslStream()); }
return smtpStreamReader;
}
else
{
smtpNetworkStream = smtpTcpClient.GetStream();
if (smtpStreamReader == null) { smtpStreamReader = new StreamReader(smtpNetworkStream); }
return smtpStreamReader;
}
}
public StreamWriter GetWriteStream()
{
if (EnableSSL == true)
{
smtpNetworkStream = smtpTcpClient.GetStream();
// readerSmtp = new StreamReader(new System.Net.Security.SslStream(clientSmtp.GetStream(), true, CertificateValidationCallback));
if (smtpStreamWriter == null) { smtpStreamWriter = new StreamWriter(GetSslStream()); }
return smtpStreamWriter;
}
else
{
smtpNetworkStream = smtpTcpClient.GetStream();
if (smtpStreamWriter == null) { smtpStreamWriter = new StreamWriter(smtpNetworkStream); }
return smtpStreamWriter;
}
}
SslStream GetSslStream()
{
if (sslstream == null) {
sslstream = new SslStream(smtpNetworkStream);
sslstream.AuthenticateAsClient(strIpAddress, null, System.Security.Authentication.SslProtocols.Tls, false);
}
return sslstream;
}
public void DoConnect(string strIpAddress, int portno,bool EnableSSL)
{
this.strIpAddress = strIpAddress;
this.portNo = portno;
try
{
UpdateStatus("trying to connect to " + strIpAddress + ":" + portno.ToString());
smtpTcpClient = GetTcpClient();
smtpTcpClient.Connect(strIpAddress, portno);
if(EnableSSL==true)
{
this.EnableSSL = EnableSSL;
smtpNetworkStream = smtpTcpClient.GetStream();
//sslstream.Write(System.Text.ASCIIEncoding.ASCII.GetBytes("EHLO [email protected] \n"));
//sslstream.Write(System.Text.ASCIIEncoding.ASCII.GetBytes("STARTTLS \n"));
smtpStreamReader = GetReadStream();//new StreamReader(new System.Net.Security.SslStream(smtpTcpClient.GetStream(), true,CertificateValidationCallback));
//readerSmtp = new StreamReader(sslstream);
}
else
{
EnableSSL = false;
smtpNetworkStream = smtpTcpClient.GetStream();
smtpStreamReader = GetReadStream();
}
UpdateStatus(readResponse());
}
catch (Exception e)
{
//MessageBox.Show(e.Message);
UpdateStatus("****CONNECTION ERROR****" + "\n" + e.Message);
smtpTcpClient.Close();
}
}
请帮助我.. 非常感谢。.
编辑:我了解 System.net.mail.smtpclient 和其他可用的开源 smtp 库。但是,我无法使用它来手动发送 smtp 命令。这不是为了发送电子邮件。这是为了了解电子邮件协议通过 SSL 逐步工作。
I am unable to send any command through smtp.gmail.com.
I can see some mixed up characters when I connect with 465 as the port number.
If I use any other port number like 25 or 587, it fails with "The handshake failed due to an unexpected packet format."
Here's my code..
public delegate void StatusUpdateEvents(object o, clsStatusEventHandler e);
public event StatusUpdateEvents statusupdate;
clsStatusEventHandler objStatus = new clsStatusEventHandler();
public bool EnableSSL { get; set; }
public string strIpAddress { get; set; }
public int portNo { get; set; }
static public string EncodeTo64(string toEncode)
{
byte[] toEncodeAsBytes
= System.Text.ASCIIEncoding.ASCII.GetBytes(toEncode);
string returnValue
= System.Convert.ToBase64String(toEncodeAsBytes);
return returnValue;
}
public TcpClient GetTcpClient()
{
if(smtpTcpClient==null) smtpTcpClient = new TcpClient();
return smtpTcpClient;
}
public TcpClient ConnectTcpClient()
{
if (smtpTcpClient.Connected) smtpTcpClient.Close();
smtpTcpClient.Connect(strIpAddress, portNo);
return smtpTcpClient;
}
public void UpdateStatus(string strStatus)
{
objStatus.strStatus = strStatus;
if (statusupdate!=null) statusupdate(this, objStatus);
}
bool CertificateValidationCallback(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
return true;
}
public StreamReader GetReadStream()
{
if (EnableSSL == true)
{
smtpNetworkStream = smtpTcpClient.GetStream();
if (smtpStreamReader == null) { smtpStreamReader = new StreamReader(GetSslStream()); }
return smtpStreamReader;
}
else
{
smtpNetworkStream = smtpTcpClient.GetStream();
if (smtpStreamReader == null) { smtpStreamReader = new StreamReader(smtpNetworkStream); }
return smtpStreamReader;
}
}
public StreamWriter GetWriteStream()
{
if (EnableSSL == true)
{
smtpNetworkStream = smtpTcpClient.GetStream();
// readerSmtp = new StreamReader(new System.Net.Security.SslStream(clientSmtp.GetStream(), true, CertificateValidationCallback));
if (smtpStreamWriter == null) { smtpStreamWriter = new StreamWriter(GetSslStream()); }
return smtpStreamWriter;
}
else
{
smtpNetworkStream = smtpTcpClient.GetStream();
if (smtpStreamWriter == null) { smtpStreamWriter = new StreamWriter(smtpNetworkStream); }
return smtpStreamWriter;
}
}
SslStream GetSslStream()
{
if (sslstream == null) {
sslstream = new SslStream(smtpNetworkStream);
sslstream.AuthenticateAsClient(strIpAddress, null, System.Security.Authentication.SslProtocols.Tls, false);
}
return sslstream;
}
public void DoConnect(string strIpAddress, int portno,bool EnableSSL)
{
this.strIpAddress = strIpAddress;
this.portNo = portno;
try
{
UpdateStatus("trying to connect to " + strIpAddress + ":" + portno.ToString());
smtpTcpClient = GetTcpClient();
smtpTcpClient.Connect(strIpAddress, portno);
if(EnableSSL==true)
{
this.EnableSSL = EnableSSL;
smtpNetworkStream = smtpTcpClient.GetStream();
//sslstream.Write(System.Text.ASCIIEncoding.ASCII.GetBytes("EHLO [email protected] \n"));
//sslstream.Write(System.Text.ASCIIEncoding.ASCII.GetBytes("STARTTLS \n"));
smtpStreamReader = GetReadStream();//new StreamReader(new System.Net.Security.SslStream(smtpTcpClient.GetStream(), true,CertificateValidationCallback));
//readerSmtp = new StreamReader(sslstream);
}
else
{
EnableSSL = false;
smtpNetworkStream = smtpTcpClient.GetStream();
smtpStreamReader = GetReadStream();
}
UpdateStatus(readResponse());
}
catch (Exception e)
{
//MessageBox.Show(e.Message);
UpdateStatus("****CONNECTION ERROR****" + "\n" + e.Message);
smtpTcpClient.Close();
}
}
Please help me..
Thanks so much in advance..
Edit: I know about the System.net.mail.smtpclient and other opensource smtp libraries available. But, I cannot use that to manually send smtp commands. This is not for sending e-mails. This is for seeing the email protocol work step by step through SSL.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能应该只使用 System.Net.Mail.SMTPClient< /a> 因为这就是它的设计用途
You should probably just use System.Net.Mail.SMTPClient as this is what it is designed to be used with
要与 TcpClient 连接,您必须先扫描 DNS,smtp.gmail.com 不是正确的服务器,请尝试以下操作:
To connect with TcpClient you have to scan DNS before, smtp.gmail.com is not the right server, try this: