计算机网络(2)——手动SMTP

#file name:PA01_3.py
#writer:Zhang_Jingtun(Ordinary_Crazy)
#initial date:20180310
from socket import *
msg = "\r\n I love computer networks!"
endmsg = "\r\n.\r\n"

# Choose a mail server (e.g. Google mail server) and call it mailserver
mailserver =('smtp.163.com', 25)

# Create socket called clientSocket and establish a TCP connection with mailserver
#Fill in start
clientSocket = socket(AF_INET, SOCK_STREAM)
clientSocket.connect(mailserver)
#Fill in end

recv = clientSocket.recv(1024)
print (recv)
if(str(recv[:3],"utf-8") != "220"):
	print('220 reply not received from server.')


# Send HELO command and print server response.
heloCommand = 'HELO Alice\r\n'
clientSocket.send(heloCommand.encode())
recv1 = clientSocket.recv(1024)
print (recv1)
if(str(recv1[:3],"utf-8") != '250'):
	print ('250 reply not received from server.')
	
loginCommand = 'AUTH LOGIN\r\n'
clientSocket.send(loginCommand.encode())
recv7 = clientSocket.recv(1024)
print (recv7)
if str(recv7[:3],"utf-8") != '334':
	print ('334 reply not received from server.')
	
	
clientSocket.send("用户名base64码\r\n".encode())
recv9 = clientSocket.recv(1024)
print (recv9)
clientSocket.send("密码base64码\r\n".encode())
recv8 = clientSocket.recv(1024)
print (recv8)
# Send MAIL FROM command and print server response.
# Fill in start

mail_fromCommand = 'MAIL FROM:<发送邮箱全称>\r\n'
clientSocket.send(mail_fromCommand.encode())
recv2 = clientSocket.recv(1024)
print (recv2)
if str(recv2[:3],"utf-8") != "250":
	print ('250 reply not received from server.')
# Fill in end

# Send RCPT TO command and print server response.
# Fill in start
rcpt_toCommand = 'RCPT TO:<接收邮箱quan chen>\r\n'
clientSocket.send(rcpt_toCommand.encode())
recv3 = clientSocket.recv(1024)
print (recv3)
if str(recv3[:3],"utf-8") != '250':
	print ('250 reply not received from server.')
# Fill in end

# Send DATA command and print server response.
# Fill in start
dataCommand = 'DATA\r\n'
clientSocket.send(dataCommand.encode())
recv4 = clientSocket.recv(1024)
print (recv4)
if str(recv4[:3],"utf-8") != '354':
	print ('354 reply not received from server.')
# Fill in end

# Send message data.
# Fill in start
message = "testmail\r\n"
clientSocket.send("from:发送邮箱全称\r\n".encode())
clientSocket.send("to:接收邮箱全称\r\n".encode())
clientSocket.send("subject:test\r\n".encode())
clientSocket.send("\r\n".encode())
clientSocket.send(message.encode())
# Fill in end

# Message ends with a single period.
# Fill in start
clientSocket.send(".\r\n".encode())
recv5 = clientSocket.recv(1024)
print (recv5)
if str(recv5[:3],"utf-8") != '250':
	print ('250 reply not received from server.')
# Fill in end

# Send QUIT command and get server response.
# Fill in start
quitCommand = 'QUIT\r\n'
clientSocket.send(quitCommand.encode())
recv6 = clientSocket.recv(1024)
print (recv6)
if str(recv6[:3],"utf-8") != '221':
	print ('221 reply not received from server.')
# Fill in end



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值