Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4065316
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T15:58:04+00:00 2026-05-20T15:58:04+00:00

I am unable to send any command through smtp.gmail.com. I can see some mixed

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T15:58:04+00:00Added an answer on May 20, 2026 at 3:58 pm

    You should probably just use System.Net.Mail.SMTPClient as this is what it is designed to be used with

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.