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

题目要求:

  • 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连接。

前提准备:

hostport
smtp.qq.com25
smtp.163.com25
  • 开启发送方邮件的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: <xxxxxxxxx@qq.com>\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 = ['xxxxxxxx@qq.com']
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这个在作者配套的代码文件里没有。
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(可能会被判垃圾邮件?)

结果:

这里写图片描述
这里写图片描述

使用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 = 'xxxxxxxx@qq.com'

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("邮件发送失败!")  

  • 10
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值