搜尋

首頁  >  問答  >  主體

python3 使用smtplib发送邮件错误554

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')

在网上找了很多方法,把防火墙也关了,修改了发件人收件人名称,但还是没有效果,真的不知道是什么原因。。

大家讲道理大家讲道理2808 天前937

全部回覆(4)我來回復

  • 大家讲道理

    大家讲道理2017-04-17 17:53:33

    最後終於還是找到解決辦法了:
    郵件主題為‘test’的時候就會出現錯誤,換成其他詞就好了。 。我也不知道這是什麼奇葩的原因

    回覆
    0
  • 大家讲道理

    大家讲道理2017-04-17 17:53:33

    Error報的不是很清楚了嗎:
    從error裡的連結: http://help.163.com/09/1224/17/5RAJ4LMH00753VB8.html

    554 DT:SPM 發送的郵件內容包含了未被許可的訊息,或被系統識別為垃圾郵件。請檢查是否有使用者發送病毒或垃圾郵件;

    你需要的是檢查自己發的東西內容.

    回覆
    0
  • ringa_lee

    ringa_lee2017-04-17 17:53:33

    1) 檢查下垃圾箱,確認郵件沒有丟到垃圾箱裡
    2) from和To要跟內容一致,不要填不一致的,否則會被郵件伺服器過濾。
    下面程式碼親測可以用163信箱寄email給QQ信箱。

    def 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()
        
        
        

    回覆
    0
  • PHPz

    PHPz2017-04-17 17:53:33

    確實是這樣,發送方和接收方必須是163的伺服器,才能成功發送。不然就會報錯!

    回覆
    0
  • 取消回覆