from socket import *
import ssl
import base64
msg = "rnI love computer networks!"
endmsg = "rn.rn"
# Choose a mail server (e.g. Google mail server) and call it mailserver
mailserver = ('smtp.gmail.com', 587)
# Create socket called clientSocket and establish a TCP connection with mailserver
clientSocket = socket(AF_INET, SOCK_STREAM)
clientSocket.connect(mailserver)
recv = clientSocket.recv(1024).decode()
print(recv)
if recv[:3] != '220':
print('220 reply not received from server.')
# Send EHLO command and print server response.
ehloCommand = 'EHLO Alicern'
clientSocket.send(ehloCommand.encode())
recv1 = clientSocket.recv(1024).decode()
print(recv1)
if recv1[:3] != '250':
print('250 reply not received from server.')
# Send STARTTLS command and print server response.
starttlsCommand = 'STARTTLSrn'
clientSocket.send(starttlsCommand.encode())
recv_tls = clientSocket.recv(1024).decode()
print(recv_tls)
# Upgrade to a secure connection using SSL
context = ssl.create_default_context()
clientSocket = context.wrap_socket(clientSocket, server_hostname='smtp.gmail.com')
# Send EHLO command again after STARTTLS
clientSocket.send(ehloCommand.encode())
recv_helo = clientSocket.recv(1024).decode()
print(recv_helo)
if recv_helo[:3] != '250':
print('250 reply not received from server after TLS.')
# Provide credentials for authentication
email_address = "myEMail"
password = "MyPassword" # Replace with your application-specific password
# Send AUTH LOGIN command and print server response.
auth_command = 'AUTH LOGINrn'
clientSocket.send(auth_command.encode())
recv_auth = clientSocket.recv(1024).decode()
print(recv_auth)
# Send base64 encoded email address for authentication
clientSocket.send(base64.b64encode(email_address.encode()) + b'rn')
recv_user = clientSocket.recv(1024).decode()
print(recv_user)
# Send base64 encoded password for authentication
clientSocket.send(base64.b64encode(password.encode()) + b'rn')
recv_password = clientSocket.recv(1024).decode()
print(recv_password)
# Handle authentication failure
if recv_password[:3] != '235':
print("Authentication failed. Please check your credentials.")
clientSocket.close()
exit()
# Send MAIL FROM command and print server response.
mailFromCommand = f'MAIL FROM: <{email_address}>rn'
clientSocket.send(mailFromCommand.encode())
recv2 = clientSocket.recv(1024).decode()
print(recv2)
# Send RCPT TO command and print server response.
rcptToCommand = 'RCPT TO: <destinationMail>rn' # Fill in your recipient email address
clientSocket.send(rcptToCommand.encode())
recv3 = clientSocket.recv(1024).decode()
print(recv3)
# Send DATA command and print server response.
dataCommand = 'DATArn'
clientSocket.send(dataCommand.encode())
recv4 = clientSocket.recv(1024).decode()
print(recv4)
# Send message data.
clientSocket.send(msg.encode())
# Message ends with a single period.
clientSocket.send(endmsg.encode())
# Send QUIT command and get server response.
quitCommand = 'QUITrn'
clientSocket.send(quitCommand.encode())
recv5 = clientSocket.recv(1024).decode()
print(recv5)
clientSocket.close()
Here I aimed to send to gmail with socket programming, without using the smtplib library. But when I run the code
535-5.7.8 Username and Password not accepted. For more information, go to
535 5.7.8 https://support.google.com/mail/?p=BadCredentials j3-20020a05600c1c0300b004186f979543sm11130252wms.33 – gsmtp
The above problem occurred.
Note:
2-step verification is turned off.