Python-通过SMTP发送邮件而不使用库 - 必须首先发出STARTTLS命令错误消息

时间:2018-04-11 07:39:06

标签: python email smtp

我正在尝试使用python上的SMTP发送电子邮件 我收到错误“必须先发出STARTTLS命令”和 网站上的每个答案都说“使用.starttls()” 但是,我没有使用smtp库。 我该如何解决这个问题?我找了 我在网站上的其他问题和我一样 了解SMTP如何工作(+ SSL / TSL问题)

from socket import *
import base64

heloCommand = 'EHLO Alice\r\n'
msg = "\r\n I love computer networks!"
endmsg = "\r\n.\r\n"

# Choose a mail server && Establish TCP connection
mailserver = 'smtp.gmail.com'
clientSocket = socket(AF_INET, SOCK_STREAM)
clientSocket.connect((mailserver, 587))
recv = clientSocket.recv(1024)
recv = recv.decode()
print(recv)
if recv[:3] != '220':
    print('220 reply not received from server.')

# Send HELO command and print server response
print("Sending First HELO")
clientSocket.send(heloCommand.encode())
recv1 = clientSocket.recv(1024)
recv1 = recv1.decode()
print(recv1)
if recv1[:3] != '250':
    print('250 reply not received from server.')

###
# Send AUTH command and print server response.
print("Sending AUTH Command")
authMsg = "AUTH PLAIN\r\n"
clientSocket.send(authMsg.encode())
recv_auth = clientSocket.recv(1024)
print(recv_auth.decode())
if recv_auth[:3] != '250':
    print('250 reply not received from server.')

#Info for username and password
username = "xxxx"
password = "xxxx"

# AUTH with base64 encoded user name password
auth = username+"\0"+password
base64_str = ('%s\0%s' % (username,password)).encode()
auth = base64.b64encode(base64_str)
clientSocket.send(auth)
recv_user = clientSocket.recv(1024)
print(recv_user.decode())
if recv_user[:3] != '250':
    print('250 reply not received from server.')
###

# Send MAIL FROM command and print server response.  2
print("Sending MAIL FROM Command")
clientSocket.send("MAIL FROM: xxxx\r\n".encode())
recv2 = clientSocket.recv(1024)
print(recv2.decode())
if recv2[:3] != '250':
    print('250 reply not received from server.')

# Send RCPT TO command and print server response.  3
print("Sending RCPT TO Command")
clientSocket.send("RCPT TO: xxxx\r\n".encode())
recv2 = clientSocket.recv(1024)
print(recv2.decode())
if recv2[:3] != '250':
    print('250 reply not received from server.')

# Send DATA command and print server response.  4
print("Sending DATA Command")
clientSocket.send("DATA\r\n".encode())
recv2 = clientSocket.recv(1024)
print(recv2.decode())
if recv2[:3] != '250':
    print('250 reply not received from server.')

# Send message data and print server response.  5
print("Sending message data")
clientSocket.send("SUBJECT: SMTP Mail Client\nIs it working??            
\n.\n\r\n".encode())
recv2 = clientSocket.recv(1024)
print(recv2.decode())
if recv2[:3] != '250':
    print('250 reply not received from server.')

# Message ends with a single period.
print("Message ends with a single period")
clientSocket.send(".\r\n".encode())
recv2 = clientSocket.recv(1024)
print(recv2.decode())
if recv2[:3] != '250':
    print('250 reply not received from server.')

# Send QUIT and print server response.
print("Sending QUIT")
clientSocket.send("QUIT\r\n".encode())
recv2 = clientSocket.recv(1024)
print(recv2.decode())
if recv2[:3] != '250':
    print('250 reply not received from server.')

print("Mail Sent")

clientSocket.close()

1 个答案:

答案 0 :(得分:0)

tlsCommand = 'STARTTLS \r\n'
clientSocket.send(tlsCommand.encode())
recv2 = clientSocket.recv(1024)
recv2 = recv2.decode()
print("Message after STARTTLS command:" + recv2)
if recv2[:3] != '220':
   print('220 reply not received from server.')