2

I am writting my own SMTP client in c# (to be used in compact framework). trying to use smtp.gmail.com server

After succesful STARTTLS command if smtp client sends EHLO Command response from server isempty`.

message = "EHLO 10.192.4.223\r\n";
      Write(message);
      response = Response();
      if (response.Substring(0, 3) != "250")
      {
        throw new SmtpException(response);
      }
      message = "STARTTLS\r\n";
      Write(message);
      response = Response();
      if (response.Substring(0, 3) != "220")
      {
        throw new SmtpException(response);
      }

      message = "EHLO 10.192.4.223\r\n";
      Write(message);
      response = Response();
      if (response.Substring(0, 3) != "250") //empty response
      {
        throw new SmtpException(response);
      }
Toon Krijthe
  • 52,876
  • 38
  • 145
  • 202
Anant
  • 342
  • 6
  • 14

1 Answers1

2

Because after STARTTLS you are no longer supposed to send plain text commands. Instead, you are supposed to switch to TLS-secured communication on the same existing TCP socket/connection.

See what RFC 2487 - SMTP Service Extension for Secure SMTP over TLS says:

After receiving a 220 response to a STARTTLS command, the client
SHOULD start the TLS negotiation before giving any other SMTP
commands.

and

5.2 Result of the STARTTLS Command

Upon completion of the TLS handshake, the SMTP protocol is reset to the initial state (the state in SMTP after a server issues a 220
service ready greeting). The server MUST discard any knowledge
obtained from the client, such as the argument to the EHLO command,
which was not obtained from the TLS negotiation itself. The client
MUST discard any knowledge obtained from the server, such as the list of SMTP service extensions, which was not obtained from the TLS
negotiation itself. The client SHOULD send an EHLO command as the
first command after a successful TLS negotiation.

Roman R.
  • 68,205
  • 6
  • 94
  • 158
  • Thanks for your reply.. how can I switch to TLS-secured communication? can it be done by sending command? – Anant Oct 15 '12 at 07:50
  • You send STARTLS as you do, you receive response in plain text as you are already doing. Next to that, you should start sending TLS handshake in binary. Typically you need a third party library or API for that (SChannel in Win API). Or instead, you can just use C# `SmtpClient` which already encapsulates this, see http://stackoverflow.com/questions/32260/sending-email-in-net-through-gmail – Roman R. Oct 15 '12 at 07:54