使用的资料

计算机网络自顶向下方法 第7版

参考前人第六版资料

操作系统:Windows 7

我的笔记

实验步骤

Skeleton Python Code for the Mail Client Your task is to develop a simple mail client that sends email to any recipient. Your client will need to connect to a mail server, dialogue with the mail server using the SMTP protocol, and send an email message to the mail server. Python provides a module, called smtplib, which has built in methods to send mail using SMTP protocol. However, we will not be using this module in this lab, because it hide the details of SMTP and socket programming.

整体流程是根据第七版书,2.3.1节的过程写的,上面发送的指令,就使用socket.sent()以字符串编码的形式发过去。

服务器的回复,就使用socket.recv()来进行接收,再解码。

给的代码框架有些古老了,并不适合Python3.x。

  1. from socket import *
  2. # Choose a mail server (e.g. Google mail server) and call it mailserver
  3. mailserver = "smtp.qq.com"
  4. mailport = 25 # smtp port
  5. # Create socket called clientSocket and establish a TCP connection with mailserver
  6. print("-- creat socket test --")
  7. clientSocket = socket(AF_INET, SOCK_STREAM)
  8. clientSocket.connect((mailserver, mailport))
  9. recv = clientSocket.recv(1024).decode()
  10. print(recv)
  11. if recv[:3] != '220':
  12. print('220 reply not received from server.')
  13. # Send HELO command and print server response.
  14. print("-- send HELO command test --")
  15. heloCommand = 'HELO Alice\r\n'
  16. clientSocket.send(heloCommand.encode())
  17. recv1 = clientSocket.recv(1024).decode()
  18. print(recv1)
  19. if recv1[:3] != '250':
  20. print('250 reply not received from server.')
  21. # Send MAIL FROM command and print server response.
  22. print("-- send MAIL FROM command test --")
  23. mailFromCommand = "MAIL FROM: <xyz@abc.com>\r\n" #发送邮箱地址
  24. clientSocket.send(mailFromCommand.encode())
  25. recv2 = clientSocket.recv(1024).decode()
  26. print(recv2)
  27. if recv2[:3] != "250":
  28. print('250 reply not received from server.')
  29. # Send RCPT TO command and print server response.
  30. print("-- send RCPT TO command test --")
  31. rcptToCommand = 'RCPT TO: <xxxx@qq.com>\r\n' #接收邮箱地址
  32. clientSocket.send(rcptToCommand.encode())
  33. recv3 = clientSocket.recv(1024).decode()
  34. print(recv3)
  35. if recv3[:3] != "250":
  36. print('250 reply not received from server.')
  37. # Send DATA command and print server response.
  38. print("-- send DATA command test --")
  39. dataCommand = "DATA\r\n"
  40. clientSocket.send(dataCommand.encode())
  41. recv4 = clientSocket.recv(1024).decode()
  42. print(recv4)
  43. if recv4[:3] != "354":
  44. print('354 reply not received from server.')
  45. # Send message data.
  46. print("-- send message test --")
  47. msg = "\r\n I love computer networks!"
  48. clientSocket.send(msg.encode())
  49. # Message ends with a single period.
  50. print("-- end message test --")
  51. endmsg = "\r\n.\r\n"
  52. clientSocket.send(endmsg.encode())
  53. # Send QUIT command and get server response.
  54. print("-- send QUIT command test --")
  55. quitCommand = 'QUIT\r\n'
  56. clientSocket.send(quitCommand.encode())
  57. recv5 = clientSocket.recv(1024).decode()
  58. print(recv5)
  59. if recv5 != '221':
  60. print("221 reply not received from server.")

测试结果

总体流程是走过了一遍,但是给的代码框架可能有些过时?没有考虑这个AUTH登陆验证的部分,或许需要自己设计。课本没有细说这个流程,我也没再细究,具体可以看看参考的文档。

image-20210119140542392.png

关于扩展练习

  1. Mail servers like Google mail (address: smtp.gmail.com, port: 587) requires your client to add a Transport Layer Security (TLS) or Secure Sockets Layer (SSL) for authentication and security reasons, before you send MAIL FROM command. Add TLS/SSL commands to your existing ones and implement your client using Google mail server at above address and port.
  2. Your current SMTP mail client only handles sending text messages in the email body. Modify your client such that it can send emails with both text and images.

扩展练习都暂且保留。

总结

一开始选择的学校的邮箱,但是不知道为什么出现Error ConnectionAbortedError: WinError 10053](https://stackoverflow.com/questions/65782242/python-error-connectionabortederror-winerror-10053),之后捣鼓了很久,换了QQ邮箱测试就没再出现问题。我的一个猜测是,学校的邮箱也是基于网易的,可能当时邮件服务器恰好是同一个?外加没有`AUTH`,当时也没注意看到这一条。可能早就出现了。