-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmtpClient.py
133 lines (114 loc) · 4.27 KB
/
smtpClient.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# Name: Eren Kılıç
# ID: 64150238706
# Python version: 3.6
######################################################################################
# In this assignment I created a SMTP client and with this program, we are able #
# to send e-mails. As an optional exercise I used Google mail server and handled #
# its requirements by using TLS. In order to test it I created a gmail account and #
# allowed login request from 3rd party. Here is the login information of the account:#
# mail: [email protected] / password: tedu1234 #
# With this, I am able to send an e-mail from gmail account to any mail account. #
######################################################################################
from socket import *
import ssl
import base64
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.gmail.com' #Fill in start #Fill in end
port = 587
# Create socket called clientSocket and establish a TCP connection with mailserver
#Fill in start
clientSocket = socket(AF_INET, SOCK_STREAM)
clientSocket.connect((mailserver, port))
#Fill in end
recv = clientSocket.recv(1024).decode()
print("recv: ", recv)
if recv[:3] != '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).decode()
print("recv1: ", recv1)
if recv1[:3] != '250':
print('250 reply not received from server.')
# OPTIONAL: Establish secure connection TLS
startTLSCommand = "STARTTLS\r\n"
clientSocket.send(startTLSCommand.encode())
recv2 = clientSocket.recv(1024).decode()
print("recv2: ", recv2)
if recv2[:3] != '220':
print('220 reply not received from server.')
tlsClientSocket = ssl.wrap_socket(clientSocket)
tlsClientSocket.send(heloCommand.encode())
recv3 = tlsClientSocket.recv(1024).decode()
print("recv3: ", recv3)
if recv3[:3] != '250':
print('250 reply not received from server.')
authLoginCommand = 'AUTH LOGIN\r\n'
tlsClientSocket.send(authLoginCommand.encode())
recv4 = tlsClientSocket.recv(1024).decode()
print("recv4: ", recv4)
if recv4[:3] != '334':
print('334 reply not received from server.')
tlsClientSocket.send(base64.b64encode(("[email protected]").encode()))
tlsClientSocket.send(('\r\n').encode())
recv5 = tlsClientSocket.recv(1024).decode()
print("recv5: ", recv5)
if recv5[:3] != "334":
print('334 reply not received from server.')
tlsClientSocket.send(base64.b64encode(("tedu1234").encode()))
tlsClientSocket.send(('\r\n').encode())
recv6 = tlsClientSocket.recv(1024).decode()
print("recv6: ", recv6)
if recv6[:3] != "235":
print('235 reply not received from server.')
# Send MAIL FROM command and print server response.
# Fill in start
mailFromCommand = 'MAIL FROM:<[email protected]>\r\n'
tlsClientSocket.send(mailFromCommand.encode())
recv7 = tlsClientSocket.recv(1024).decode()
print("recv7: ", recv7)
if recv7[:3] != '250':
print('250 reply not received from server.')
# Fill in end
# Send RCPT TO command and print server response.
# Fill in start
rcptToCommand = 'RCPT TO:<[email protected]>\r\n'
tlsClientSocket.send(rcptToCommand.encode())
recv8 = tlsClientSocket.recv(1024).decode()
print("recv8: ", recv8)
if recv8[:3] != '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'
tlsClientSocket.send(dataCommand.encode())
recv9 = tlsClientSocket.recv(1024).decode()
print("recv9: ", recv9)
if recv9[:3] != '354':
print('354 reply not received from server.')
# Fill in end
# Send message data.
# Fill in start
tlsClientSocket.send(msg.encode())
# Fill in end
# Message ends with a single period.
# Fill in start
tlsClientSocket.send(endmsg.encode())
recv10 = tlsClientSocket.recv(1024).decode()
print("recv10: ", recv10)
if recv10[:3] != "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'
tlsClientSocket.send(quitCommand.encode())
recv11 = tlsClientSocket.recv(1024).decode()
print("recv11: ", recv11)
if recv11[:3] != '221':
print('221 reply not received from server.')
# Fill in end