python發送郵件554DT:SPM已解決


  說明:本例使用163郵箱

一、報錯信息

  使用SMTP發送郵件遇到以下報錯:

  554, b'DT:SPM 163 smtp10,DsCowACXeOtmjRRdsY8aCw--.21947S2 1561628007,please see http://mail.163.com/help/help_spam_16.htm?ip=36.110.94.251&hostid=smtp10&time=1561628007'

二、排查原因

  1.檢查163郵箱是否設置授權碼,授權碼對不對。不對會報錯:535, b'Error: authentication failed'

  2.檢查代碼郵件格式規范

    下面貼出菜鳥教程部分代碼使用header。看已發送郵件的確是顯示了發件人別名(顯示代發),收件人看到的發件人也是自己起的別名。如下圖:

# -*- coding: utf-8 -*-
# Nola

import smtplib
from email.mime.text import MIMEText
from email.header import Header
import time

mail_server = "smtp.163.com"
mail_port = 25
sender = "xxxxx@163.com"
sender_password = "xxxxxx"  # 授權碼
receivers = "wewewwe@163.com"


message = MIMEText('Python郵件發送測試...', 'plain', 'utf-8')
# message['From'] = sender
# message['To'] = receivers
message['From'] = Header("菜鳥教程", 'utf-8')
message['To'] = Header("測試", 'utf-8')

send_time = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
subject = '郵件測試' + send_time
message['Subject'] = subject


try:
    smtp_obj = smtplib.SMTP()
    smtp_obj.connect(mail_server, mail_port)
    smtp_obj.login(sender, sender_password)
    smtp_obj.sendmail(sender, [receivers], message.as_string())
    print('success!')
except smtplib.SMTPException as e:
    print('failure!')
    print(e)

 

  重點是:收到幾封郵件后,收不到了,換收件人也收不到,猜測是163禁發了。點上圖中幫助可以看到下圖,所以善意的偽裝也會產生誤會。

三、解決方法

  From和To使用發件人和收件人真實郵箱地址,發送即可成功。

  

# -*- coding: utf-8 -*-
# Nola

import smtplib
from email.mime.text import MIMEText
import time

mail_server = "smtp.163.com"
mail_port = 25
sender = "xxxxx@163.com"
sender_password = "xxxxxx"  # 授權碼
receivers = "wewewwe@163.com"


message = MIMEText('Python郵件發送測試...', 'plain', 'utf-8')
message['From'] = sender
message['To'] = receivers

send_time = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
subject = '郵件測試' + send_time
message['Subject'] = subject


try:
    smtp_obj = smtplib.SMTP()
    smtp_obj.connect(mail_server, mail_port)
    smtp_obj.login(sender, sender_password)
    smtp_obj.sendmail(sender, [receivers], message.as_string())
    print('success!')
except smtplib.SMTPException as e:
    print('failure!')
    print(e)

 

 

 

  

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM