Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from socket import *
- import ssl
- import base64
- import argparse as ap
- import getpass as gp
- parser = ap.ArgumentParser(description='A test SMTP client with TLS')
- parser.add_argument('-u', '--username', dest='username', required=True, metavar='<username>')
- parser.add_argument('-f', '--from', dest='fromMail', required=True, metavar='<sender_email>')
- parser.add_argument('-t', '--to', dest='toMail', required=True, metavar='<recipient_email>')
- args = parser.parse_args()
- # Message to send
- msg = '\nI love computer networks!\n'
- endmsg = '\n.\n'
- #Credentials
- # Remember to DELETE THE PASSWORD before you hand-in the code!!!
- username = args.username
- password = gp.getpass(prompt='Password: ')
- # Choose a mail server (Office365 for your student accounts or NTNU's student SMTP) and call it mailserver
- # You'll have to search the web for the address
- #mailserver = 'smtp.office365.com'
- mailserver = 'smtp.stud.ntnu.no'
- # Create socket called clientSocket and establish a TCP connection with mailserver
- clientSocket = socket(AF_INET, SOCK_STREAM)
- # Port number may change according to the mail server
- # You'll have to search the web to find the corresponding port. Remember that we are using TLS.
- clientSocket.connect((mailserver, 587))
- #clientSocket.connect((mailserver, 2500))
- 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.)
- # Can use EHLO instead since HELO is obsolete, but can still be used
- heloCommand = 'HELO {}\n'.format(clientSocket.getsockname()[0])
- print(clientSocket.getsockname()[0])
- 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 STARTTLS command to start a secure connection and get server response.
- quitcommand = 'STARTTLS\n'
- clientSocket.send(quitcommand.encode())
- recv2 = clientSocket.recv(1024)
- recv2 = recv2.decode()
- print(recv2)
- if recv2[:3] != '220':
- print('220 reply not received from server.')
- # Wrap the clientSocket with a TLS secure socket
- clientSocketSSL = ssl.wrap_socket(clientSocket, ssl_version=ssl.PROTOCOL_SSLv23)
- # Send HELO command and print(server response.)
- # Can use EHLO instead since HELO is obsolete, but the latter can still be used
- heloCommand = 'EHLO Hey\n'
- clientSocketSSL.send(heloCommand.encode())
- recv3 = clientSocketSSL.recv(1024)
- recv3 = recv3.decode()
- print(recv3)
- if recv3[:3] != '250':
- print('250 reply not received from server.')
- # Send AUTH command and print(server response.)
- authCommand = 'AUTH LOGIN\n'
- clientSocketSSL.send(authCommand.encode())
- recv4 = clientSocketSSL.recv(1024)
- recv4 = recv4.decode()
- print(recv4)
- # The code in this response is in plain text (should be 334), but
- # the message is in base64. Have to decode it before printing.
- print(recv4.split()[0] + ' ' + base64.b64decode(recv4.split()[1]).decode() + '\n')
- if recv4[:3] != '334':
- print('334 reply not received from server.')
- # Send username in base64 command and print(server response.)
- # Remember to add \n after the username
- loginCommand = base64.b64encode(username.encode()).decode() + '\n'
- clientSocketSSL.send(loginCommand.encode())
- # In bytes, have to decode to string
- recv5 = clientSocketSSL.recv(1024)
- recv5 = recv5.decode()
- # The code in this response is in plain text (should be 334), but
- # the message is in base64. Have to decode it before printing.
- print(recv5.split()[0] + ' ' + base64.b64decode(recv5.split()[1]).decode() + '\n')
- if recv5[:3] != '334':
- print('334 reply not received from server.')
- # Send your password in base64 command and print(server response.)
- # Remember to add \n after the password
- passCommand = base64.b64encode(password.encode()).decode() + '\n'
- clientSocketSSL.send(passCommand.encode())
- recv6 = clientSocketSSL.recv(1024)
- recv6 = recv6.decode()
- print(recv6)
- if recv6[:3] != '235':
- print('235 reply not received from server.')
- # Send MAIL FROM command and print(server response.)
- mailfrom = 'MAIL FROM: <{}>\n'.format(args.fromMail)
- clientSocketSSL.send(mailfrom.encode())
- recv7 = clientSocketSSL.recv(1024)
- recv7 = recv7.decode()
- print(recv7)
- if recv7[:3] != '250':
- print('250 reply not received from server.')
- # Send RCPT TO command and print(server response.)
- rcptto = 'RCPT TO: <{}>\n'.format(args.toMail)
- clientSocketSSL.send(rcptto.encode())
- recv8 = clientSocketSSL.recv(1024)
- recv8 = recv8.decode()
- print(recv8)
- if recv8[:3] != '250':
- print('250 reply not received from server.')
- # Send DATA command and print(server response.)
- data = 'DATA\n'
- clientSocketSSL.send(data.encode())
- recv9 = clientSocketSSL.recv(1024)
- recv9 = recv9.decode()
- print(recv9)
- if recv9[:3] != '354':
- print('354 reply not received from server.')
- # Send message data.
- clientSocketSSL.send('Subject:Greeting To you!\n'.encode())
- clientSocketSSL.send('\ntest again\n'.encode())
- clientSocketSSL.send(msg.encode())
- # Message ends with a single period.
- clientSocketSSL.send(endmsg.encode())
- recv10 = clientSocketSSL.recv(1024)
- recv10 = recv10.decode()
- print(recv10)
- if recv10[:3] != '250':
- print('250 reply not received from server.')
- # If you get here and receive the proper code response from the server,
- # the code is correct. The mailserver has commited to deliver the mail.
- # Keep in mind that the mail may still be blocked at the receiver side (spam)
- # but that does not mean that your code is wrong.
- # Send QUIT command and get server response.
- quitcommand = 'QUIT\n'
- clientSocketSSL.send(quitcommand.encode())
- recv11 = clientSocketSSL.recv(1024)
- recv11 = recv11.decode()
- print(recv11)
- if recv11[:3] != '221':
- print('221 reply not received from server.')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement