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 something123@gmail.com \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.
You should probably just use System.Net.Mail.SMTPClient as this is what it is designed to be used with