-
Notifications
You must be signed in to change notification settings - Fork 221
/
MailTextPicClient.py
148 lines (132 loc) · 4.12 KB
/
MailTextPicClient.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#改为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 = 'jz163wy@163.com'
mailPassWord = '******'
mailToAddress = 'ecnujz@qq.com'
# transfer image and html
with open("timg.jpg","rb") as f:
image_data = base64.b64encode(f.read())
with open("hello.html","rb") as f:
html_data = base64.b64encode(f.read())
# 构造邮件正文
msg = 'FROM: ' + mailFromAddress + '\r\n'
msg += 'TO: ' + mailToAddress + '\r\n'
msg += 'Subject: ' + 'text and picture' + '\r\n'
msg += 'Content-Type:multipart/related; boundary="----=_NextPart_000_0012345JZ"\r\n'
msg += 'MIME-Version: 1.0\r\n'
msg += '\r\n'
msg = msg.encode()
msg += '------=_NextPart_000_0012345JZ\r\n'.encode()
msg += 'Content-Type: text/html; charset=UTF-8\r\n'.encode()
msg += 'Content-Transfer-Encoding: base64\r\n'.encode()
msg += '\r\n'.encode()
msg += html_data
msg += '\r\n'.encode()
msg += '\r\n'.encode()
msg += '------=_NextPart_000_0012345JZ\r\n'.encode()
msg += 'Content-Type: image/jpeg; name="timg.jpg"\r\n'.encode()
msg += 'Content-Transfer-Encoding: base64\r\n'.encode()
msg += 'Content-ID: JZJZJZJZJZJZJZJZ'.encode()
msg += '\r\n'.encode()
msg += image_data + "\r\n".encode()
msg += '\r\n'.encode()
msg += '------=_NextPart_000_0012345JZ--\r\n'.encode()
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)
# 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()