用smtp协议给QQ邮箱发送邮件(计算机网络习题)

from socket import *
import base64
# Choose a mail server
mailserver = 'smtp.163.com'
# Create socket called clientSocket and establish a TCP connection with mailserver
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.')
# Send HELO command and print server response.
heloCommand = 'HELO hi\r\n'
clientSocket.send(heloCommand.encode())
recv1 = clientSocket.recv(1024).decode()
print(recv1)
if recv1[:3] != '250':
    print('250 reply not received from server.')
# Send MAIL FROM command and print server response.
clientSocket.send('AUTH LOGIN\r\n'.encode())
recv2 = clientSocket.recv(1024).decode()
print(recv2)
if recv2[:3] != '334':
    print('334 reply not received from server.')

user = base64.b64encode(b'*****@163.com').decode()+'\r\n'   #输入邮箱
password = base64.b64encode(b'**********').decode()+'\r\n'     #**输入授权密码
clientSocket.send(user.encode())
recv3 = clientSocket.recv(1024).decode()
print(recv3)

clientSocket.send(password.encode())
recv4 = clientSocket.recv(1024).decode()
print(recv4)

clientSocket.send('MAIL FROM: <*****@163.com> \r\n'.encode())
recv5 = clientSocket.recv(1024)
print('mail from: ', recv5)

# Send RCPT TO command and print server response.
clientSocket.send('RCPT TO: <******@qq.com> \r\n'.encode())
recv6 =clientSocket.recv(1024)
print('rcpt to: ', recv6)

# Send DATA command and print server response.
data = b'DATA\r\n'
clientSocket.send(data)
recv7 = clientSocket.recv(1024)
print('data: ', recv7)

# Send message data. 注意格式From:nickname<邮箱地址>,否则会被qq邮箱拒收
header = "From:boss<****@163.com>\r\nTo:*********@qq.com\r\nSubject:a letter\r\n" 

msg = 'hello,world'
clientSocket.send((header+msg).encode())
clientSocket.send('/r/n./r/n'.encode())  # end message
recv8 = clientSocket.recv(1024)
print(recv8)

# Send QUIT command and get server response.
clientSocket.send('QUIT\r\n'.encode())
clientSocket.close()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值