
第四步、点击开启然后点击重置授权码。

第五步、按照如下操作编辑短信发送、然后就可以
重置授权码了、授权码和密码不要设置为一样的注意点:授权码和密码不一致、用代码连接163邮件服务器就需要用授权码而不是密码。

第六步、然后把 init 构造函数里面的psw默认参数的值改为你自己设置的授权码
就可以用如下的代码发送邮件了。
1#coding=utf‐8 2import smtplib 3from email.mime.text import MIMEText 4from email.mime.text import MIMEText 5from email.mime.multipart import MIMEMultipart 6from email.header import Header 7import os 8 9class SendMail: 10def init (self, send_msg, 11 smtpserver="smtp.163.com", sender="18617162994@163.com", 12psw="xxxxxx", receiver="18617162994@163.com", #psw这个为授权码 13port=25, attachment=None): 14# self.reportfile = reportfile 15self.send_msg = send_msg 16self.smtpserver = smtpserver 17self.sender = sender 18self.psw = psw 19self.receiver = receiver 20self.port = port 21self.attachment = attachment 22 23def send_mail(self): 24"""发送最新的测试报告内容"""
25 #打开测试报告,读取测试报告内容
26 with open(self.send_msg, "rb") as f:
27 mail_boday = f.read()
28 #定义邮件
29 msg = MIMEMultipart()
30 msg['subject'] = Header(u"自动化测试报告", 'utf‐8')
31 msg['From'] = Header(self.sender, 'utf‐8')
32 msg['To'] = self.receiver
33
34 #添加附件
35 if self.attachment != None:
36 file_name = self.attachment.split(os.path.sep)[‐1]
37 att = MIMEText(open(self.attachment, "rb").read(),
38 "base64", "utf‐8")
39 att["Content‐Type"] = "application/octet‐stream"
40 att.add_header('Content‐Disposition', 'attachment',
41 filename = file_name)
42 msg.attach(att)
43
44 body = MIMEText(mail_boday, _subtype="html", _charset='utf‐8')
45 msg.attach(body)
46 smtp = smtplib.SMTP()
47 smtp.set_debuglevel(1)
48 smtp.connect(self.smtpserver, self.port)
49 smtp.starttls()
50 #用户登录并发送邮件
51 smtp.login(self.sender, self.psw)
52 smtp.sendmail(self.sender, self.receiver, msg.as_string())
53 smtp.quit()
python3源码
#coding=utf-8
import smtplib
from email.mime.text import MIMEText
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header
import os
class SendMail:
def __init__(self, send_msg,
smtpserver="smtp.163.com", sender="15949613302@163.com", #15949613302@163.com
psw="VOQGWBZYNBOAVZGE", receiver="15949613302@163.com", #psw这个为授权码
port=25, attachment=None):
# self.reportfile = reportfile
self.send_msg = send_msg
self.smtpserver = smtpserver
self.sender = sender
self.psw = psw
self.receiver = receiver
self.port = port
self.attachment = attachment
def send_mail(self):
"""发送最新的测试报告内容"""
#打开测试报告,读取测试报告内容
with open(self.send_msg, "rb") as f:
mail_boday = f.read()
#定义邮件
msg = MIMEMultipart()
msg['subject'] = Header(u"自动化测试报告", 'utf-8')
msg['From'] = Header(self.sender, 'utf-8')
msg['To'] = self.receiver
#添加附件
if self.attachment != None:
file_name = self.attachment.split(os.path.sep)[-1]
att = MIMEText(open(self.attachment, "rb").read(), "base64", "utf-8")
att["Content-Type"] = "application/octet-stream"
att.add_header('Content-Disposition', 'attachment', filename = file_name)
msg.attach(att)
body = MIMEText(mail_boday, _subtype="html", _charset='utf-8')
msg.attach(body)
smtp = smtplib.SMTP()
smtp.set_debuglevel(1)
smtp.connect(self.smtpserver, self.port)
#用户登录并发送邮件
smtp.login(self.sender, self.psw)
smtp.sendmail(self.sender, self.receiver, msg.as_string())
smtp.quit()
python2源码
#coding=utf-8
import smtplib
from email.mime.text import MIMEText
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header
import os
class SendMail:
def __init__(self, send_msg,
smtpserver="smtp.163.com", sender="15949613302163.com", #15949613302@163.com
psw="xlh.1314", receiver="15949613302@163.com", #psw这个为授权码
port=25, attachment=None):
# self.reportfile = reportfile
self.send_msg = send_msg
self.smtpserver = smtpserver
self.sender = sender
self.psw = psw
self.receiver = receiver
self.port = port
self.attachment = attachment
def send_mail(self):
"""发送最新的测试报告内容"""
#打开测试报告,读取测试报告内容
with open(self.send_msg, "rb") as f:
mail_boday = f.read()
#定义邮件
msg = MIMEMultipart()
msg['subject'] = Header("自动化测试报告", 'utf-8')
msg['From'] = Header(self.sender, 'utf-8')
msg['To'] = self.receiver
#添加附件
if self.attachment != None:
file_name = self.attachment.split(os.path.sep)[-1]
att = MIMEText(open(self.attachment, "rb").read(), "base64", "utf-8")
att["Content-Type"] = "application/octet-stream"
att.add_header('Content-Disposition', 'attachment', filename = file_name)
msg.attach(att)
body = MIMEText(mail_boday, _subtype="html", _charset='utf-8')
msg.attach(body)
smtp = smtplib.SMTP()
smtp.set_debuglevel(1)
smtp.connect(self.smtpserver, self.port)
smtp.starttls()
#用户登录并发送邮件
smtp.login(self.sender, self.psw)
smtp.sendmail(self.sender, self.receiver, msg.as_string())
smtp.quit()
