title: ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host (SMTP)
body: <p>I am trying to send an test email from one gmail account to another using SMTP socket. I have allowed unsecure access from my client gmail account. </p>
  
  <p>During the login phase the code breaks here:</p>
  
  <pre><code>#Send username as Base64 encoded string
  username = (username + '\r\n').encode()
  command = base64.b64encode(username)
  clientSocket.send(command)
  resp = clientSocket.recv(1024)
  print('S: ' + resp.decode())
  </code></pre>
  
  <p>I am getting the 10054 error message and have tried using wireshark to decompose whats happening, but I am not really understanding whats happening. It seems to be an server side encryption based off google smtp server. I am new to socket programming, thanks for your patience!</p>
  
  <p>The rest of the code:</p>
  
  <pre><code>from socket import *
  import time
  import base64
  
  print("")
  
  username = input("Enter your mail.com username : ")
  password = input("Enter your mail.com password : ")
  rec = input("\nEnter the recipient's email address : ")
  name = input("\nEnter your name : ")
  subject = input("\nEnter the subject for your mail : \n")
  
  message = ["SMTP client is awesome \r\n", "123456789 \r\n","Networking is cool. \r\n"]
  
  print("")
  
  endmsg = "\r\n.\r\n"
  
  # Choose a mail server (e.g. Google mail server) and call it mailserver
  mailserver = "smtp.gmail.com"
  mailserverPort = 587
  
  # Create socket called clientSocket and establish a TCP connection with mailserver
  clientSocket = socket(AF_INET, SOCK_STREAM)
  clientSocket.connect((mailserver, mailserverPort))
  
  resp = clientSocket.recv(1024)
  print('S: ' + resp.decode())
  
  # Send EHLO (for Extended SMTP) command and print server response.
  heloCommand = 'EHLO 127.0.0.1 \r\n'
  print('C: ' + heloCommand)
  clientSocket.send(heloCommand.encode())
  resp = clientSocket.recv(1024)
  print('S: ' + resp.decode())
  
  # Authentication start mail server client
  command = 'STARTTLS \r\n'
  print('C: ' + command)
  clientSocket.send(command.encode())
  resp = clientSocket.recv(1024)
  print('S: ' + resp.decode())
  
  # Authentication required for the mail.com server I'm using
  command = 'AUTH LOGIN \r\n'
  print('C: ' + command)
  clientSocket.send(command.encode())
  resp = clientSocket.recv(1024)
  print('S: ' + resp.decode())
  
  # Send username as Base64 encoded string
  username = (username + '\r\n').encode()
  command = base64.b64encode(username)
  #print('C: ' + command.decode())
  clientSocket.send(command)
  resp = clientSocket.recv(1024)
  print('S: ' + resp.decode())
  
  # Send password as Base64 encoded string
  password = (password + '\r\n').encode()
  command = base64.b64encode(password)
  print('C: ' + command)
  clientSocket.send(command)
  resp = clientSocket.recv(1024)
  print('S: ' + resp.decode())
  
  # Send MAIL FROM command and print server response.
  command = "MAIL FROM: &lt;" + username + "&gt;\r\n"
  print('C: ' + command)
  clientSocket.send(command)
  resp = clientSocket.recv(1024)
  print('S: ' + resp.decode())
  
  # Send RCPT TO command and print(server resp.decode()onse.
  command = "RCPT TO: &lt;" + rec + "&gt;\r\n"
  print('C: ' + command)
  clientSocket.send(command)
  resp = clientSocket.recv(1024)
  print('S: ' + resp.decode())
  
  # Send DATA command and print server response.
  command = "DATA\r\n"
  print('C: ' + command)
  clientSocket.send(command)
  resp = clientSocket.recv(1024)
  print('S: ' + resp.decode())
  
  # Send message headers
  # Gmail does not accept mail without headers to prevent spam
  command = 'From: ' + name + ' &lt;' + username + '&gt;' + '\r\n'
  print('C: ' + command)
  clientSocket.send(command)
  command = 'To: ' + rec + '\r\n'
  print('C: ' + command)
  clientSocket.send(command)
  command = 'Date: ' + time.asctime( time.localtime(time.time()) ) + '\r\n'
  print('C: ' + command)
  clientSocket.send(command)
  command = 'Subject: ' + subject + '\r\n'
  print('C: ' + command)
  clientSocket.send(command)
  
  # Send message data.
  clientSocket.send('\r\n')
  for msg in message:
      print('C: ' + msg)
      clientSocket.send(msg)
  
  # Message ends with a single period.
  print('C: ' + endmsg)
  clientSocket.send(endmsg)
  
  # Send QUIT command and get server resp.decode()onse.
  command = "QUIT\r\n"
  print('C: ' + command)
  clientSocket.send(command)
  resp = clientSocket.recv(1024)
  print('S: ' + resp.decode())
  </code></pre>