-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMailSSLClient.py
123 lines (108 loc) · 3.21 KB
/
MailSSLClient.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
#改为Python3格式
from socket import *
import base64
import ssl
# Choose a mail server (e.g. Google mail server) and call it mailserver
mailserver = "smtp.163.com"
mailUser = 'jz163wy'
mailFromAddress = '[email protected]'
mailPassWord = '******'
mailToAddress = '[email protected]'
msg = 'FROM: ' + mailFromAddress + '\r\n'
msg += 'TO: ' + mailToAddress + '\r\n'
msg += 'Subject: ' + 'test' + '\r\n'
msg += "\r\n I love computer networks!"
endmsg = "\r\n.\r\n"
# Create socket called clientSocket and establish a TCP connection with mailserver
context = ssl.create_default_context()
clientSocket = socket(AF_INET, SOCK_STREAM)
clientSocket.connect((mailserver, 465))
clientSocketSSL = context.wrap_socket(clientSocket, server_hostname=mailserver)
recv = clientSocketSSL.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.
heloCommand = 'HELO mailserver\r\n'
while True:
clientSocketSSL.send(heloCommand.encode())
recv = clientSocketSSL.recv(1024)
recv = recv.decode()
print(recv)
if recv[:3] == '250':
break
# 登录过程
loginCommand = 'auth login\r\n'
while True:
clientSocketSSL.send(loginCommand.encode())
recv = clientSocketSSL.recv(1024)
recv = recv.decode()
print(recv)
if recv[:3] == '334':
break
# 邮箱账户经过base64编码
userCommand = base64.b64encode(mailUser.encode()) + b'\r\n'
while True:
clientSocketSSL.send(userCommand)
recv = clientSocketSSL.recv(1024)
recv = recv.decode()
print(recv)
if recv[:3] == '334':
break
# 邮箱密码经过base64编码 这里不展示密码了
passCommand = base64.b64encode(mailPassWord.encode()) + b'\r\n'
while True:
clientSocketSSL.send(passCommand)
recv = clientSocketSSL.recv(1024)
recv = recv.decode()
print(recv)
if recv[:3] == '235':
break
# Send MAIL FROM command and print server response.
MFCommand = 'MAIL FROM: <'+ mailFromAddress + '>\r\n'
while True:
clientSocketSSL.send(MFCommand.encode())
recv = clientSocketSSL.recv(1024)
recv = recv.decode()
print(recv)
if recv[:3] == '250':
break
# Send RCPT TO command and print server response.
RTCommand = 'RCPT TO: <'+ mailToAddress + '>\r\n'
while True:
clientSocketSSL.send(RTCommand.encode())
recv = clientSocketSSL.recv(1024)
recv = recv.decode()
print(recv)
if recv[:3] == '250':
break
# Send DATA command and print server response.
DATACommand = 'DATA\r\n'
while True:
clientSocketSSL.send(DATACommand.encode())
recv = clientSocketSSL.recv(1024)
recv = recv.decode()
print(recv)
if recv[:3] == '354':
break
# Send message data.
clientSocketSSL.send(msg.encode())
# Message ends with a single period.
while True:
clientSocketSSL.send(endmsg.encode())
recv = clientSocketSSL.recv(1024)
recv = recv.decode()
print(recv)
if recv[:3] == '250':
break
# Send QUIT command and get server response.
QUITCommand = 'QUIT\r\n'
while True:
clientSocketSSL.send(QUITCommand.encode())
recv = clientSocketSSL.recv(1024)
recv = recv.decode()
print(recv)
if recv[:3] == '221':
break
clientSocketSSL.close()