V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
linhanqiu
V2EX  ›  Python

Python smtplib 的发送间断性失败

  •  
  •   linhanqiu · 2017-11-09 15:35:05 +08:00 · 1989 次点击
    这是一个创建于 2576 天前的主题,其中的信息可能已经有所发展或是发生改变。
    使用 163、outlook 总是显示 554, b'DT:SPM 163 smtp9,说是系统判断我是垃圾邮件,可是我只是发了'hello'而已

    #!/usr/bin/env python3
    #coding: utf-8
    # coding: utf-8
    import smtplib
    import mimetypes
    from email.mime.text import MIMEText
    from email.mime.image import MIMEImage
    from email.mime.multipart import MIMEMultipart
    from email.header import Header
    import email


    #列表类
    class LS:
    mail_address=[
    "[email protected]";
    ]
    #配置类
    class Cfg:
    mail_config = {
    "sender":"[email protected]",
    "receiver":["[email protected]"],
    "username":"[email protected]",
    "password":"linhanqiu1123",
    #smtp 服务器
    "smtpserver":"smtp.163.com"
    }
    @staticmethod
    def insert_md():
    global mail_config
    for i in LS.mail_address:
    mail_config["receiver"].append(i)

    sys_config = {
    #图片地址
    "path": "/home/linhanqiu/img/1.jpg"
    }

    #内容类
    class Content:
    def __init__(self,id=None,word="hello",subject=u"hello"):
    self.id = id
    self.word = word
    self.subject = subject
    self.html = "<p1></p1>"
    #内容格式,其他格式还在添加
    def text(self):
    msgRoot = MIMEMultipart("related")
    msgRoot['Subject'] = Header(self.subject, 'utf-8')
    msgRoot['From'] = Header('爬虫任务情况<[email protected]>','utf-8')
    msgRoot['To'] = "[email protected]";
    msgRoot.preamble = 'This is a multi-part message in MIME format.'
    #设置转化部分
    # msgAlternative = MIMEMultipart("alternative")
    # msgRoot.attach(msgAlternative)
    # 添加纯文本信息,(添加模板)
    # msgText = MIMEText(self.word,'plain','utf-8')
    # msgAlternative.attach(msgText)
    # 设定 html 信息,(添加模板)
    # msgHtml = MIMEText(self.html,'html','utf-8')
    # msgRoot.attach(msgHtml)
    # 设置图片信息,( matplotlib 画图)
    # fp = open(Cfg.sys_config["path"], 'rb')
    # msgImage = MIMEImage(fp.read())
    # fp.close()
    # msgImage.add_header('Content-ID', '<image1>')
    # msgRoot.attach(msgImage)
    return msgRoot

    #邮件基类
    class Mail:
    def __init__(self):
    self.cfg = Cfg.mail_config
    self.server = self.cfg["smtpserver"]
    self.sender = self.cfg["sender"]
    self.receiver = self.cfg["receiver"]
    self.username = self.cfg["username"]
    self.password = self.cfg["password"]
    def login(self):
    s = smtplib.SMTP()
    s.connect(self.server)
    s.login(self.username,self.password)
    #调式过程
    s.set_debuglevel(1)

    s.ehlo("begin connect")

    # s.starttls()
    return s
    def send(self):
    s = self.login()
    #创建内容
    m = Content().text()
    try:
    s.sendmail(self.sender,self.receiver,m.as_string())
    print("已发送")
    except smtplib.SMTPRecipientsRefused:
    print('Recipient refused')
    except smtplib.SMTPAuthenticati onError:
    print('Auth error')
    except smtplib.SMTPSenderRefused:
    print('Sender refused')
    # except smtplib.SMTPException as e:
    # print(e.message)
    s.quit()
    def __call__(self, *args, **kwargs):
    return self.send()

    #商道邮件子类
    #娱道邮件子类
    if __name__=="__main__":
    mail = Mail()
    mail()
    2 条回复    2017-11-14 08:44:07 +08:00
    katios
        1
    katios  
       2017-11-11 19:10:40 +08:00
    这个问题比较复杂,我遇到过。但是不是代码的问题,经过反复的测试。
    可能是如下的情况
    http://blog.csdn.net/sinat_21302587/article/details/69388526
    希望能帮到你
    zhusimaji
        2
    zhusimaji  
       2017-11-14 08:44:07 +08:00 via iPhone
    最好邮件内容齐全比如主题之类,现在反垃圾邮件越来越厉害了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1135 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 22:46 · PVG 06:46 · LAX 14:46 · JFK 17:46
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.