Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from socket import *
- import argparse as ap
- import getpass as gp
- #Get sender_email and recipient_email as arguments to the program
- parser = ap.ArgumentParser(description='A test SMTP client without authentication')
- parser.add_argument('-f', '--from', dest='fromMail', required=True, metavar='<sender_email>')
- parser.add_argument('-t', '--to', dest='toMail', required=True, metavar='<recipient_email>')
- #If using the authentication of the SMTP server, also get a valid username (optional exercise)
- #parser.add_argument('-u', '--username', dest='username', required=True, metavar='<username>')
- args = parser.parse_args()
- fromMail = args.fromMail #Sender's email address
- toMail = args.toMail #Recipient's email address
- #username = args.username #SMTP username in case you are implementing the optional exercise
- #If using the authentication of the SMTP server, ask for a valid password (optional exercise)
- #password = gp.getpass(prompt='Password: ')
- # Message to send
- msg = "\r\n I love computer networks!"
- endmsg = "\r\n.\r\n"
- # Our mail server is smtp.stud.ntnu.no but it allows only authenticated communications. (optional exercise)
- #mailserver = 'smtp.stud.ntnu.no'
- # You can run a local simple SMTP server such as "Fake SMTP Server" and communicate with it without authentication.
- mailserver = 'localhost'
- # Create socket called clientSocket and establish a TCP connection
- # (use the appropriate port) with mailserver
- #Fill in start
- serverName = 'mailServer'
- serverPort = 2525
- clientSocket = socket(AF_INET, SOCK_STREAM) #SOCK_STREAM: TCP socket, AF_INET: IPv4
- clientSocket.connect((mailserver,serverPort))
- #Fill in end
- recv = clientSocket.recv(1024)
- 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 the latter can still be used
- heloCommand = 'EHLO Hey\r\n'
- clientSocket.send(heloCommand.encode())
- recv1 = clientSocket.recv(1024)
- print (recv1)
- if recv1[:3] != '250':
- print ('250 reply not received from server.')
- # Send MAIL FROM command and print server response.
- mailFromCommand = 'MAIL FROM: <{}> \r\n'.format(fromMail)
- clientSocket.send(mailFromCommand.encode()) #Python 3
- recv2 = clientSocket.recv(1024)
- print('Mail from: ', recv2)
- if recv2[:3] != '250':
- print ('250 reply not received from server.')
- # Send RCPT TO command and print server response.
- RCPTtoCommand = 'RCPT TO: <{}> \r\n'.format(toMail)
- clientSocket.send(RCPTtoCommand.encode())
- recv3 = clientSocket.recv(1024)
- print('RCPT to: ', recv3)
- if recv3[:3] != '250':
- print ('250 reply not received from server.')
- # Send DATA command and print server response.
- DataCommand = 'DATA \r\n'
- clientSocket.send(DataCommand.encode())
- recv4 = clientSocket.recv(1024)
- print('Data: ', recv4)
- if recv4[:3] != '250':
- print ('250 feilreply not received from server.')
- # Send message data.
- clientSocket.send(msg.encode())
- # Message ends with a single period.
- dot = '.'
- clientSocket.send(dot.encode())
- # Send QUIT command and get server response.
- quitCommand = 'QUIT \r\n'
- clientSocket.send(quitCommand.encode())
- recv4 = clientSocket.recv(1024)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement