|
|
|
@ -0,0 +1,102 @@
|
|
|
|
|
import base64
|
|
|
|
|
import sys
|
|
|
|
|
from socket import *
|
|
|
|
|
|
|
|
|
|
toemail = sys.argv[1]
|
|
|
|
|
print(toemail)
|
|
|
|
|
msg = "\r\n 您的国防科大请销假系统注册成功。"
|
|
|
|
|
endmsg = "\r\n.\r\n"
|
|
|
|
|
# Choose a mail server (e.g. Google mail server) and call it mailserver
|
|
|
|
|
mailserver = "smtp.qq.com" # 网上查询到qq邮箱的SMTP服务器
|
|
|
|
|
# Create socket called clientSocket and establish a TCP connection with mailserver
|
|
|
|
|
# Fill in start
|
|
|
|
|
ID = base64.b64encode(b"750975972@qq.com").decode() + '\r\n' # 账户名,登录用,需要利用库base64转换
|
|
|
|
|
password = base64.b64encode(b"semjskwgqfwkbcfd").decode() + '\r\n' # 授权码,登录时代替密码,需要利用库base64转换
|
|
|
|
|
clientSocket = socket(AF_INET, SOCK_STREAM) # 与之前webserver实验相同,仍然是创建一个IPv4,SOCK_STREAM类型的客户套接字
|
|
|
|
|
serverPort = 25
|
|
|
|
|
clientSocket.connect((mailserver, serverPort)) # 客户套接字尝试创建与SMTP服务器指定端口的连接
|
|
|
|
|
# Fill in end
|
|
|
|
|
recv = clientSocket.recv(1024).decode()
|
|
|
|
|
print(recv)
|
|
|
|
|
if recv[:3] != '220':
|
|
|
|
|
print('!220 reply not received from server.')
|
|
|
|
|
# Send HELO command and print server response.
|
|
|
|
|
|
|
|
|
|
heloCommand = 'HELO Alice\r\n' # HELO握手,确认双方在线
|
|
|
|
|
clientSocket.send(heloCommand.encode())
|
|
|
|
|
recv1 = clientSocket.recv(1024).decode()
|
|
|
|
|
print(recv1)
|
|
|
|
|
if recv1[:3] != '250':
|
|
|
|
|
print('!250 reply not received from server.')
|
|
|
|
|
|
|
|
|
|
mailCommand = "AUTH LOGIN\r\n" # AUTH登录请求,qq邮箱需要登录才能进行收发邮件功能
|
|
|
|
|
clientSocket.send(mailCommand.encode())
|
|
|
|
|
recv1 = clientSocket.recv(1024).decode()
|
|
|
|
|
print(recv1)
|
|
|
|
|
if recv1[:3] != '334':
|
|
|
|
|
print('!334 need login')
|
|
|
|
|
|
|
|
|
|
clientSocket.send(ID.encode()) # 发送账户名
|
|
|
|
|
recv1 = clientSocket.recv(1024).decode()
|
|
|
|
|
print(recv1)
|
|
|
|
|
if recv1[:3] != '334':
|
|
|
|
|
print('!334 need login')
|
|
|
|
|
|
|
|
|
|
clientSocket.send(password.encode()) # 发送授权码
|
|
|
|
|
recv1 = clientSocket.recv(1024).decode()
|
|
|
|
|
print(recv1)
|
|
|
|
|
if recv1[:3] != '235':
|
|
|
|
|
print('!235 password error')
|
|
|
|
|
|
|
|
|
|
# Send MAIL FROM command and print server response.
|
|
|
|
|
# Fill in start
|
|
|
|
|
mailCommand = "MAIL FROM: <750975972@qq.com>\r\n" # 正式进入发件环节,MAIL FROM填写发件人
|
|
|
|
|
clientSocket.send(mailCommand.encode())
|
|
|
|
|
recv1 = clientSocket.recv(1024).decode()
|
|
|
|
|
print(recv1)
|
|
|
|
|
if recv1[:3] != '250':
|
|
|
|
|
print('!250 reply not received from server.')
|
|
|
|
|
# Fill in end
|
|
|
|
|
# Send RCPT TO command and print server response.
|
|
|
|
|
# Fill in start
|
|
|
|
|
mailCommand = "RCPT TO: <" + toemail + ">\r\n" # RCPT TO填写收件人
|
|
|
|
|
clientSocket.send(mailCommand.encode())
|
|
|
|
|
recv1 = clientSocket.recv(1024).decode()
|
|
|
|
|
print(recv1)
|
|
|
|
|
if recv1[:3] != '250':
|
|
|
|
|
print('!250 reply not received from server.')
|
|
|
|
|
# Fill in end
|
|
|
|
|
# Send DATA command and print server response.
|
|
|
|
|
# Fill in start
|
|
|
|
|
mailCommand = "DATA\r\n" # 准备发送正文,告知服务器要发送一连串的正文
|
|
|
|
|
clientSocket.send(mailCommand.encode())
|
|
|
|
|
recv1 = clientSocket.recv(1024).decode()
|
|
|
|
|
print(recv1)
|
|
|
|
|
if recv1[:3] != '354':
|
|
|
|
|
print('!354 DATA error')
|
|
|
|
|
# Fill in end
|
|
|
|
|
# Send message data.
|
|
|
|
|
# Fill in start
|
|
|
|
|
clientSocket.send(
|
|
|
|
|
('From: 750975972@qq.com\r\nTo: ' + toemail + ' \r\nSubject: welcome!\r\n' + msg).encode()) # 告知服务器正文,附带收发信息和标题
|
|
|
|
|
# Fill in end
|
|
|
|
|
# Message ends with a single period.
|
|
|
|
|
# Fill in start
|
|
|
|
|
clientSocket.send(endmsg.encode()) # 告知服务器DATA部分结束
|
|
|
|
|
recv1 = clientSocket.recv(1024).decode()
|
|
|
|
|
print(recv1)
|
|
|
|
|
if recv1[:3] != '250':
|
|
|
|
|
print('!250 reply not received from server.')
|
|
|
|
|
# Fill in end
|
|
|
|
|
# Send QUIT command and get server response.
|
|
|
|
|
# Fill in start
|
|
|
|
|
# Fill in start
|
|
|
|
|
mailCommand = "QUIT\r\n" # 退出SMTP
|
|
|
|
|
clientSocket.send(mailCommand.encode())
|
|
|
|
|
recv1 = clientSocket.recv(1024).decode()
|
|
|
|
|
print(recv1)
|
|
|
|
|
if recv1[:3] != '221':
|
|
|
|
|
print('!221 quit error')
|
|
|
|
|
clientSocket.close() # 关闭TCP
|
|
|
|
|
sys.exit()
|
|
|
|
|
# Fill in end
|