当前位置:宁波企业邮 » 企业目录 » 金华 » 文章详细

python使用腾讯企业邮箱与个人邮箱的SMTP服务的用法区别
  1. 使用个人邮箱

    使用个人邮箱的时候需要申请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'
    #将example替换成你的QQ号码
    msg['From'] = 'example@qq.com'
    msg['To'] = stu.email
    #将example替换成你的QQ号码,并填写password,此处的password不是QQ密码,而是腾讯授权码
    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
  1. 使用企业邮箱
    如果使用的是企业邮箱,那么需要将smtpObj = smtplib.SMTP('smtp.qq.com', 587)替换为smtpObj = smtplib.SMTP_SSL('smtp.exmail.qq.com', 465)
    此外,需要将如下两行代码注释掉:
# smtpObj.ehlo()
# smtpObj.starttls()
  • 1
  • 2

例子如下:(注意此处的企业邮箱密码并非授权码,是登录密码)

smtpObj = smtplib.SMTP_SSL('smtp.exmail.qq.com', 465)
# smtpObj.ehlo()
# smtpObj.starttls()

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()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

参考链接

<>


阅读:729
日期:2021-04-29

打印 】 【 关闭 】  【 字体: 】 
上一篇: SpringBoot+JavaMailSender+ 腾讯企业邮箱配置
下一篇: 2021年什么企业邮箱更值得入手,这个企业邮箱不可错过
  >> 相关文章
 

服务热线

0574-55011290

微信二维码