Python smtplib 的发送间断性失败

2017-11-09 15:35:05 +08:00
 linhanqiu
使用 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=[
"linhanqiu1123@163.com"
]
#配置类
class Cfg:
mail_config = {
"sender":"workinform@163.com",
"receiver":["linhanqiu1123@163.com"],
"username":"workinform@163.com",
"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('爬虫任务情况<workinform@163.com>','utf-8')
msgRoot['To'] = "linhanqiu1123@163.com"
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.SMTPAuthenticationError:
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()
1986 次点击
所在节点    Python
2 条回复
katios
2017-11-11 19:10:40 +08:00
这个问题比较复杂,我遇到过。但是不是代码的问题,经过反复的测试。
可能是如下的情况
http://blog.csdn.net/sinat_21302587/article/details/69388526
希望能帮到你
zhusimaji
2017-11-14 08:44:07 +08:00
最好邮件内容齐全比如主题之类,现在反垃圾邮件越来越厉害了

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/404976

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX