美文网首页
python SMTP邮件服务

python SMTP邮件服务

作者: YYL07 | 来源:发表于2018-11-01 10:17 被阅读0次

两种邮件服务器及端口:

邮箱 SMTP服务器 SSL协议端口 非SSL协议端口
163 smtp.163.com 465 25
qq smtp.qq.com 465 25
# -*- coding=utf-8 -*-

import smtplib
from email.mime.text import MIMEText

# 设置服务器邮箱(我用的gmail,你们用的什么就换成什么,不过要在设置里面修改支持smtp)
mail_host = "smtp.163.com"
mail_port = 465
sender = 'username@163.com'   # 服务器邮箱
password = "password"  # 邮箱密码/授权码(现在很多邮箱使用的是授权码)
receivers = ['to_user@qq.com']  # 接收邮件,可设置为你的QQ邮箱或者其他邮箱


if __name__ == '__main__':
    try:
        # 第一个为文本内容,第二个 plain 设置文本格式,第三个 utf-8 设置编码
        message = MIMEText("邮件正文", 'plain', 'utf-8')
        message['from'] = 'sender'      # 邮件上显示的发件人
        message['to'] = 'receiver'      # 163有个坑,会对这个字段进行检测,会比对收件箱,如果和收件箱不一致,会被认为是垃圾邮件,发送不出去,所以这就会导致群发邮件时出问题
        message['subject'] = 'header'   # 邮件上标题

        smtp = smtplib.SMTP_SSL(mail_host, mail_port)
        smtp.set_debuglevel(1)
        smtp.ehlo(mail_host)
        smtp.login(sender, password)
        smtp.sendmail(sender, receivers, message.as_string())
    except smtplib.SMTPException:
        print("Error: 无法发送邮件")

当message['to']和实际收件邮箱不一致时会报以下错误:

554 DT:SPM 发送的邮件内容包含了未被许可的信息,或被系统识别为垃圾邮件。请检查是否有用户发送病毒或者垃圾邮件;

所以群发邮件时,考虑使用其他邮箱。

相关文章

网友评论

      本文标题:python SMTP邮件服务

      本文链接:https://www.haomeiwen.com/subject/sagatqtx.html