计算机网络套接字编程作业三: 邮件客户

题目要求:

  • Your task is to develop a simple mail client that sends email to any recipient. Your client will need to connect to a mail server, dialogue with the mail server using the SMTP protocol, and send an email message to the mail server. Python provides a module, called smtplib, which has built in methods to send mail using SMTP protocol. However, we will not be using this module in this lab, because it hide the details of SMTP and socket programming.
  • 创建一个向任何接收方发送电子邮件的简单邮件客户。客户端必须与邮件服务器创建一个TCP连接,使用SMTP协议与邮件服务器进行交谈,经该邮件服务器向某接收方发送一个电子邮件报文,最后关闭邮件服务器的TCP连接。

前提准备:

  • 查询发送方邮件的smtp服务器。百度经验
host port
smtp.qq.com 25
smtp.163.com 25
  • 开启发送方邮件的SMTP服务。一般在邮箱的设置选项里,开启服务时有一个授权码。这个授权码会代替真正的邮箱密码进行登陆。(即填在我代码的password那里)

使用socket发邮件作业代码:

from socket import *
import base64

HOST = 'smtp.163.com'
PORT = 25
BUFSIZE = 1024
ADDR = (HOST, PORT)
user = base64.b64encode(b'******@163.com').decode() + '\r\n'
password = base64.b64encode(b'......').decode() + '\r\n'

tcpCliSock = socket(AF_INET, SOCK_STREAM)
tcpCliSock.connect(ADDR)
recv = tcpCliSock.recv(BUFSIZE)
print(recv)
if recv[:3] != b'220':
	print('220 reply not received from server.')

heloCommand = 'HELO Alice\r\n'
tcpCliSock.send(heloCommand.encode())
recv = tcpCliSock.recv(BUFSIZE)
print('hello: ', recv)
if recv[:3] != b'250':
	print('250 reply not received from server.')

login = 'AUTH LOGIN\r\n'
tcpCliSock.send(login.encode())
recv = tcpCliSock.recv(BUFSIZE)
print('login:', recv)
tcpCliSock.send(user.encode())
recv = tcpCliSock.recv(BUFSIZE)
print('user:', recv)
tcpCliSock.send(password.encode())
recv = tcpCliSock.recv(BUFSIZE)
print('password: ', recv)

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

reptTo = 'RCPT TO: \r\n'
tcpCliSock.send(reptTo.encode())
recv = tcpCliSock.recv(BUFSIZE)
print('rcpt to: ', recv)

data = b'DATA\r\n'
tcpCliSock.send(data)
recv = tcpCliSock.recv(BUFSIZE)
print('data: ', recv)

who = '******@163.com'
from_ = who
to = ['[email protected]']
headers = [
    'From: %s' % from_,
    'To: %s' % ','.join(to),
    'Subject: send SMTP',
]

body = [
    'Hello',
    'World!',
]
msg = '\r\n\r\n'.join(('\r\n'.join(headers), '\r\n'.join(body)))
tcpCliSock.send(msg.encode())
endmsg = b'\r\n.\r\n'
tcpCliSock.send(endmsg)
recv = tcpCliSock.recv(BUFSIZE)
print('msg: ', recv)

quit = 'QUIT\r\n'
tcpCliSock.send(quit.encode())
recv = tcpCliSock.recv(BUFSIZE)
print('quit: ', recv)

tcpCliSock.close()

难点:

问题1:AUTH LOGIN这个在作者配套的代码文件里没有。
  • SMTP验证方式
C:auth login ----------------------------------------- 进行用户身份认证 
S:334 VXNlcm5hbWU6 ----------------------------------- BASE64编码“Username:”
C:用户名,使用BASE64编码
S:334 UGFzc3dvcmQ6 ----------------------------------- BASE64编码"Password:"
C:密码,使用BASE64编码
S:235 auth successfully ------------------------------ 身份认证成功
问题2:原作者写的msg会被163判定为垃圾邮件,发不成功
  • 哇,这里我真的百度了很久。后来看了《python核心编程》的示例代码。它没有直接用MIME格式发啊,而是以字符串的方式。竟然用到这个代码也可以!!开心!!
问题3:写subject的时候不要写test(可能会被判垃圾邮件?)

结果:

计算机网络套接字编程作业三: 邮件客户_第1张图片
计算机网络套接字编程作业三: 邮件客户_第2张图片

使用SMTP发邮件

from smtplib import SMTP
from email.mime.text import MIMEText 
from email.header import Header

mail_server = 'smtp.163.com'
port = '25'

sender = '******@163.com'
sender_pass = '......'
receiver = '[email protected]'

mail_msg = 'this is a test'
msg = MIMEText(mail_msg, 'plain', 'utf-8')
msg['From'] = sender
msg['To'] = receiver
msg['Subject'] = Header('testone', 'utf-8')

try:
	mail = SMTP(mail_server, port)
	mail.login(sender, sender_pass)
	mail.sendmail(sender, (receiver), msg.as_string() )
	mail.quit()
	print("邮件发送成功!")
except:
	mail.quit()
	print("邮件发送失败!")  

你可能感兴趣的:(计算机网络)