python3使用smtplib和MIME发送邮件失败
代码:
from 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@163.com>'
msg['To'] = "xxxxx@126.com"
email_client.sendmail(from_addr, to_addrs, msg.as_string())
email_client.quit()
if __name__ == "__main__":
send_email("smtp.163.com","xxxxx@163.com","password","xxxxx@126.com","test","hellow")
运行错误:
Traceback (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--.20172S2 1465018537,please see http://mail.163.com/help/help_spam_16.htm?ip=219.143.13.117&hostid=smtp8&time=1465018537')
在网上找了很多方法,把防火墙也关了,修改了发件人收件人名称,但还是没有效果,真的不知道是什么原因。。
Finally I found a solution:
An error will occur when the email subject is ‘test’, just change it to another word. . I don’t know what the weird reason is
Isn’t the Error report very clear:
The link from the error: http://help.163.com/09/1224/17/5RAJ4LMH00753VB8.html
What you need is to check the content of what you posted.
1) Check the trash can to make sure the email is not thrown into the trash can
2) From and To must be consistent with the content, do not fill in inconsistent fields, otherwise it will be filtered by the mail server.
If you test the code below, you can use 163 mailbox to send emails to QQ mailbox.
This is indeed the case, the sender and receiver must be 163 servers to successfully send. Otherwise, an error will be reported!