?最近在學計算機網絡看到套接字的練習
于是應用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()