python3使用smtplib和MIME发送邮件失败
代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18from smtplib import SMTP
from email.mime.text import MIMEText
from email.header import Header
def send_email(SMTP_host, from_addr, password, to_addrs, subject, content):
email_client = SMTP(SMTP_host)
email_client.login(from_addr, password)
# create msg
msg = MIMEText(content,'plain','utf-8')
msg['Subject'] = Header(subject, 'utf-8')#subject
msg['From'] = 'main<xxxxx@>'
msg['To'] = "xxxxx@"
email_client.sendmail(from_addr, to_addrs, msg.as_string())
email_client.quit()
if __name__ == "__main__":
send_email("","xxxxx@","password","xxxxx@","test","hellow")
运行错误:
1
2
3
4
5
6
7Traceback (most recent call last):
...
File "D:/bioinformatics/python脚本/mai.py", line 14, in send_email
email_client.sendmail(from_addr, to_addrs, msg.as_string())
File "D:\软件\python\lib\smtplib.py", line 799, in sendmail
raise SMTPDataError(code, resp)
smtplib.SMTPDataError: (554, b'DT:SPM 163 smtp8,DMCowACX58anaFJX9+uwAA--.2S2 1465018537,please see /help/help_spam_16.htm?ip=219.143.13.117&hostid=smtp8&time=1465018537')
在网上找了很多方法,把防火墙也关了,修改了发件人收件人名称,但还是没有效果,真的不知道是什么原因。。
最后终于还是找到解决办法了:
邮件主题为‘test’的时候就会出现错误,换成其他词就好了。。我也不知道这是什么奇葩的原因
Error报的不是很清楚了吗:
从error里的链接: /09/1224/17/5RAJ4LMH00753VB8.html
554 DT:SPM 发送的邮件内容包含了未被许可的信息,或被系统识别为垃圾邮件。请检查是否有用户发送病毒或者垃圾邮件;
你需要的是检查自己发的东西内容.
1) 检查下垃圾箱,确认邮件没有扔到垃圾箱里
2) from和To要跟内容一致,不要填不一致的,否则会被邮件服务器过滤。
下面代码亲测可以用163邮箱给QQ邮箱发邮件。
1
2
3
4
5
6
7
8
9
10
11def send_email(SMTP_host, from_account, from_passwd, to_account, subject, content):
email_client = SMTP(SMTP_host)
email_client.login(from_account, from_passwd)
# create msg
msg = MIMEText(content, 'plain', 'utf-8')
msg['Subject'] = Header(subject, 'utf-8') # subject
msg['From'] = from_account
msg['To'] = to_account
email_client.sendmail(from_account, to_account, msg.as_string())
email_client.quit()