SMTP客户端python实现

import base64
import re
import socket

msg = "\r\n I love computer networks!"
endmsg = "\r\n.\r\n"
# TAG: 服务器地址
# Choose a mail server (e.g. Google mail server) and call it mailserver
mailserver = 'smtp.163.com'  # Fill in start   #Fill in end
port = 25
ssl_port = 994
# mailserver_ip = socket.gethostbyname(mailserver)
from_man = 'xxx@163.com'
from_man_pwd = 'xxx'  # 授权码,不是密码
recv_man = 'xxx@qq.com'

login_id = base64.b64encode(from_man.encode()).decode() + '\r\n'
login_pwd = base64.b64encode(from_man_pwd.encode()).decode() + '\r\n'

# TAG: 建立连接
# Create socket called clientSocket and establish a TCP connection with mailserver
#Fill in start
clientSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
clientSocket.connect((mailserver, port))
print('connect成功')

#Fill in end
recv0 = clientSocket.recv(1024).decode()
print(recv0)
if recv0[:3] != '220':
    print('[1]220 reply not received from server.')

# TAG: 1.HELLO
# 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.')

# TAG: 登录
log_command = 'AUTH LOGIN\r\n'
clientSocket.send(log_command.encode())
recv = clientSocket.recv(1024).decode()
print(recv)
if (recv[:3] != '334'):
    print('334 login wrong')

clientSocket.send(login_id.encode())
recv = clientSocket.recv(1024).decode()
print(recv)
if recv[:3] == '535':
    print('Login ID wrong')

clientSocket.send(login_pwd.encode())
recv = clientSocket.recv(1024).decode()
print(recv)
if recv[:3] == '535':
    print('Password wrong')

# # TAG: TLS加密
# temp='STARTTLS\r\n'
# print(temp)
# clientSocket.send(temp.encode())
# recv = clientSocket.recv(1024).decode()
# print(recv)

# TAG: 2.MAIL FROM
# Send MAIL FROM command and print server response.
# Fill in start
mailfrom = 'MAIL FROM: <' + from_man + '>\r\n'
clientSocket.send(mailfrom.encode())
recv2 = clientSocket.recv(1024).decode()
print(recv2)
if recv2[:3] != '250':
    print('250 reply not received from server.')
# Fill in end

# TAG: 3.RCPT TO
# Send RCPT TO command and print server response.
# Fill in start
rcptto = 'RCPT TO: <' + recv_man + '>\r\n'
clientSocket.send(rcptto.encode())
recv3 = clientSocket.recv(1024).decode()
print(recv3)
if recv3[:3] != '250':
    print('250 reply not received from server.')
# Fill in end

# TAG: 4.DATA
# Send DATA command and print server response.
# Fill in start
data = 'DATA\r\n'
clientSocket.send(data.encode())
recv4 = clientSocket.recv(1024).decode()
print(recv4)
if recv4[:3] != '354':
    print('354 reply not received from server.')
# Fill in end

# TAG: 5.Send message
# Send message data.
# Fill in start
send = "From: " + from_man + '\r\n'
send += "To: " + recv_man + '\r\n'
send += "Subject: " + "First Web Mail Test From Love6" + '\r\n'
send += msg
clientSocket.send(send.encode())
# Fill in end

# Message ends with a single period.
# Fill in start
clientSocket.send(endmsg.encode())
recv5 = clientSocket.recv(1024).decode()
print(recv5)
if recv5[:3] != '250':
    print('250 reply not received from server.')
# Fill in end

# TAG: 6.QUIT
# Send QUIT command and get server response.
# Fill in start
quit = 'QUIT\r\n'
clientSocket.send(quit.encode())
recv6 = clientSocket.recv(1024).decode()
print(recv6)
if recv6[:3] != '221':
    print('221 reply not received from server.')
# Fill in end

clientSocket.close()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值