Advertisement
Guest User

Untitled

a guest
Mar 8th, 2018
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.77 KB | None | 0 0
  1. from socket import *
  2. import ssl
  3. import base64
  4. import argparse as ap
  5. import getpass as gp
  6.  
  7. parser = ap.ArgumentParser(description='A test SMTP client with TLS')
  8. parser.add_argument('-u', '--username', dest='username', required=True, metavar='<username>')
  9. parser.add_argument('-f', '--from', dest='fromMail', required=True, metavar='<sender_email>')
  10. parser.add_argument('-t', '--to', dest='toMail', required=True, metavar='<recipient_email>')
  11.  
  12. args = parser.parse_args()
  13.  
  14. # Message to send
  15. msg = '\nI love computer networks!\n'
  16. endmsg = '\n.\n'
  17.  
  18. #Credentials
  19. # Remember to DELETE THE PASSWORD before you hand-in the code!!!
  20. username = args.username
  21. password = gp.getpass(prompt='Password: ')
  22.  
  23. # Choose a mail server (Office365 for your student accounts or NTNU's student SMTP) and call it mailserver
  24. # You'll have to search the web for the address
  25. #mailserver = 'smtp.office365.com'
  26. mailserver = 'smtp.stud.ntnu.no'
  27. # Create socket called clientSocket and establish a TCP connection with mailserver
  28. clientSocket = socket(AF_INET, SOCK_STREAM)
  29.  
  30. # Port number may change according to the mail server
  31. # You'll have to search the web to find the corresponding port. Remember that we are using TLS.
  32. clientSocket.connect((mailserver, 587))
  33. #clientSocket.connect((mailserver, 2500))
  34. recv = clientSocket.recv(1024)
  35. recv = recv.decode()
  36. print(recv)
  37. if recv[:3] != '220':
  38. print('220 reply not received from server.')
  39.  
  40. # Send HELO command and print(server response.)
  41. # Can use EHLO instead since HELO is obsolete, but can still be used
  42. heloCommand = 'HELO {}\n'.format(clientSocket.getsockname()[0])
  43. print(clientSocket.getsockname()[0])
  44. clientSocket.send(heloCommand.encode())
  45. recv1 = clientSocket.recv(1024)
  46. recv1 = recv1.decode()
  47. print(recv1)
  48. if recv1[:3] != '250':
  49. print('250 reply not received from server.')
  50.  
  51. # Send STARTTLS command to start a secure connection and get server response.
  52. quitcommand = 'STARTTLS\n'
  53. clientSocket.send(quitcommand.encode())
  54. recv2 = clientSocket.recv(1024)
  55. recv2 = recv2.decode()
  56. print(recv2)
  57. if recv2[:3] != '220':
  58. print('220 reply not received from server.')
  59.  
  60. # Wrap the clientSocket with a TLS secure socket
  61. clientSocketSSL = ssl.wrap_socket(clientSocket, ssl_version=ssl.PROTOCOL_SSLv23)
  62.  
  63. # Send HELO command and print(server response.)
  64. # Can use EHLO instead since HELO is obsolete, but the latter can still be used
  65. heloCommand = 'EHLO Hey\n'
  66. clientSocketSSL.send(heloCommand.encode())
  67. recv3 = clientSocketSSL.recv(1024)
  68. recv3 = recv3.decode()
  69. print(recv3)
  70. if recv3[:3] != '250':
  71. print('250 reply not received from server.')
  72.  
  73. # Send AUTH command and print(server response.)
  74. authCommand = 'AUTH LOGIN\n'
  75. clientSocketSSL.send(authCommand.encode())
  76. recv4 = clientSocketSSL.recv(1024)
  77. recv4 = recv4.decode()
  78. print(recv4)
  79. # The code in this response is in plain text (should be 334), but
  80. # the message is in base64. Have to decode it before printing.
  81. print(recv4.split()[0] + ' ' + base64.b64decode(recv4.split()[1]).decode() + '\n')
  82. if recv4[:3] != '334':
  83. print('334 reply not received from server.')
  84.  
  85. # Send username in base64 command and print(server response.)
  86. # Remember to add \n after the username
  87. loginCommand = base64.b64encode(username.encode()).decode() + '\n'
  88. clientSocketSSL.send(loginCommand.encode())
  89. # In bytes, have to decode to string
  90. recv5 = clientSocketSSL.recv(1024)
  91. recv5 = recv5.decode()
  92. # The code in this response is in plain text (should be 334), but
  93. # the message is in base64. Have to decode it before printing.
  94. print(recv5.split()[0] + ' ' + base64.b64decode(recv5.split()[1]).decode() + '\n')
  95. if recv5[:3] != '334':
  96. print('334 reply not received from server.')
  97.  
  98. # Send your password in base64 command and print(server response.)
  99. # Remember to add \n after the password
  100. passCommand = base64.b64encode(password.encode()).decode() + '\n'
  101. clientSocketSSL.send(passCommand.encode())
  102. recv6 = clientSocketSSL.recv(1024)
  103. recv6 = recv6.decode()
  104. print(recv6)
  105. if recv6[:3] != '235':
  106. print('235 reply not received from server.')
  107.  
  108. # Send MAIL FROM command and print(server response.)
  109. mailfrom = 'MAIL FROM: <{}>\n'.format(args.fromMail)
  110. clientSocketSSL.send(mailfrom.encode())
  111. recv7 = clientSocketSSL.recv(1024)
  112. recv7 = recv7.decode()
  113. print(recv7)
  114. if recv7[:3] != '250':
  115. print('250 reply not received from server.')
  116.  
  117. # Send RCPT TO command and print(server response.)
  118. rcptto = 'RCPT TO: <{}>\n'.format(args.toMail)
  119. clientSocketSSL.send(rcptto.encode())
  120. recv8 = clientSocketSSL.recv(1024)
  121. recv8 = recv8.decode()
  122. print(recv8)
  123. if recv8[:3] != '250':
  124. print('250 reply not received from server.')
  125.  
  126. # Send DATA command and print(server response.)
  127. data = 'DATA\n'
  128. clientSocketSSL.send(data.encode())
  129. recv9 = clientSocketSSL.recv(1024)
  130. recv9 = recv9.decode()
  131. print(recv9)
  132. if recv9[:3] != '354':
  133. print('354 reply not received from server.')
  134.  
  135. # Send message data.
  136. clientSocketSSL.send('Subject:Greeting To you!\n'.encode())
  137. clientSocketSSL.send('\ntest again\n'.encode())
  138. clientSocketSSL.send(msg.encode())
  139.  
  140. # Message ends with a single period.
  141. clientSocketSSL.send(endmsg.encode())
  142. recv10 = clientSocketSSL.recv(1024)
  143. recv10 = recv10.decode()
  144. print(recv10)
  145. if recv10[:3] != '250':
  146. print('250 reply not received from server.')
  147.  
  148. # If you get here and receive the proper code response from the server,
  149. # the code is correct. The mailserver has commited to deliver the mail.
  150. # Keep in mind that the mail may still be blocked at the receiver side (spam)
  151. # but that does not mean that your code is wrong.
  152.  
  153. # Send QUIT command and get server response.
  154. quitcommand = 'QUIT\n'
  155. clientSocketSSL.send(quitcommand.encode())
  156. recv11 = clientSocketSSL.recv(1024)
  157. recv11 = recv11.decode()
  158. print(recv11)
  159. if recv11[:3] != '221':
  160. print('221 reply not received from server.')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement