最近在学计算机网络看到套接字的练习
于是应用SMTP协议写了个发送邮箱的玩玩
可以发一大堆垃圾邮件给对方
其中参考了
关于发邮件报错535 Error:authentication failed解决方法http://t.csdnimg.cn/Bc0Dq
已经查询如何获取网易邮箱客户端授权码
base64编码
import base64
import time
from socket import *subject = "heng hong jun shi sha bi"#邮箱标题
contenttype = "text/plain"
msg = "heng hong jun shi sha bi"#内容
endmsg = "\r\n.\r\n"mailserver = "smtp.163.com"fromaddress = "@163.com"#输入你的163.com邮箱
toaddress = "@qq.com"#输入对方邮箱username = "@163.com"#输入你的邮箱
password = "******"#输入客户端授权码,参考:http://t.csdnimg.cn/Bc0Dqusername_base64 = base64.b64encode(username.encode()).decode()
password_base64 = base64.b64encode(password.encode()).decode()clientSocket = socket(AF_INET, SOCK_STREAM)
clientSocket.connect((mailserver, 25))recv = clientSocket.recv(1024).decode()
print(recv)
if recv[:3] != '220':print('220 reply not received from server.')exit()# Send HELO command and print server response.
heloCommand = 'HELO Alice\r\n'
clientSocket.send(heloCommand.encode())
recv1 = clientSocket.recv(1024).decode()
print(recv1)
if recv1[:3] != '250':print('250 reply not received from server.')exit()# Auth
clientSocket.sendall('AUTH LOGIN\r\n'.encode())
recv = clientSocket.recv(1024).decode()
print(recv)
if (recv[:3] != '334'):print('334 reply not received from server')exit()clientSocket.sendall((username_base64 + '\r\n').encode())
recv = clientSocket.recv(1024).decode()
print(recv)
if (recv[:3] != '334'):print('334 reply not received from server')exit()clientSocket.sendall((password_base64 + '\r\n').encode())
recv = clientSocket.recv(1024).decode()
print(recv)
if (recv[:3] != '235'):print('235 reply not received from server')exit()cnt=int(input())#输入发送次数while(cnt>0):cnt-=1# Send MAIL FROM command and print server response.clientSocket.sendall(('MAIL FROM: <' + fromaddress + '>\r\n').encode())recv = clientSocket.recv(1024).decode()print(recv)if (recv[:3] != '250'):print('250 reply not received from server')exit()# Send RCPT TO command and print server response.clientSocket.sendall(('RCPT TO: <' + toaddress + '>\r\n').encode())recv = clientSocket.recv(1024).decode()print(recv)if (recv[:3] != '250'):print('250 reply not received from server')exit()# Send DATA command and print server response.clientSocket.send('DATA\r\n'.encode())recv = clientSocket.recv(1024).decode()print(recv)if (recv[:3] != '354'):print('354 reply not received from server')exit()# Send message data.message = 'from:' + fromaddress + '\r\n'message += 'to:' + toaddress + '\r\n'message += 'subject:' + subject + '\r\n'message += 'Content-Type:' + contenttype + '\t\n'message += '\r\n' + msgclientSocket.sendall(message.encode())# Message ends with a single period.clientSocket.sendall(endmsg.encode())recv = clientSocket.recv(1024).decode()print(recv)if (recv[:3] != '250'):print('250 reply not received from server')exit()# Send QUIT command and get server response.
clientSocket.sendall('QUIT\r\n'.encode())# Close connection
clientSocket.close()