-
使用个人邮箱
使用个人邮箱的时候需要申请QQ授权码:
首先登陆个人邮箱,在邮箱设置下方找到账户一栏。

接着点击下图中的生成授权码:

例子如下:
for stu in student:
smtpObj = smtplib.SMTP('smtp.qq.com', 587)
smtpObj.ehlo()
smtpObj.starttls()
scoresend = str(int(exam) * int(exam_proportion) / 100 + int(dailyscore) * int(
dailyscore_proportion) / 100 + int(homework) * int(homework_proportion) / 100 + int(
checkscore) * int(checkscore_proportion) / 100 + int(experiment) * int(
experiment_proportion) / 100)
text = 'Subject: ' + courseTitle + ' score is: ' + scoresend
msg = MIMEText(text)
if float(scoresend) >= 60:
msg['Subject'] = u'Score Indicate'
else:
msg['Subject'] = u'WARNING'
msg['From'] = 'example@qq.com'
msg['To'] = stu.email
smtpObj.login('example@qq.com', '授权码')
smtpObj.sendmail(msg['From'], msg['To'], msg.as_string())
smtpObj.quit()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 使用企业邮箱
如果使用的是企业邮箱,那么需要将smtpObj = smtplib.SMTP('smtp.qq.com', 587)
替换为smtpObj = smtplib.SMTP_SSL('smtp.exmail.qq.com', 465)
。
此外,需要将如下两行代码注释掉:
例子如下:(注意此处的企业邮箱密码并非授权码,是登录密码)
smtpObj = smtplib.SMTP_SSL('smtp.exmail.qq.com', 465)
context['site'] = site_url()
context['site_name'] = config.site_name
message = render_to_string(template_name, context)
subject = ''.join(subject.splitlines())
msg = MIMEText(message)
msg['Subject'] = subject
msg['From'] = '企业邮箱账号'
msg['To'] = to[0]
smtpObj.login('企业邮箱账号', '企业邮箱密码')
smtpObj.sendmail(msg['From'], msg['To'], msg.as_string())
smtpObj.quit()
参考链接
<>