解决python发邮件报错(554, 'DT:SPM 163 smtp11,D8CowA..

博客提及报错信息,指出将发送人邮箱加入收件人地址可解决报错,还展示了更改前后的代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

报错信息如下:
报错信息
将发送人邮箱也加入收件人地址中即可解决报错。

更改之前代码:

import smtplib
from email.mime.text import MIMEText
from email.utils import formataddr
from email.mime.application import MIMEApplication
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart

sender = 'XXX@163.com' # 发件人地址
receive = 'XXX@qq.com' # 收件人地址
passwd = 'XXX' # 授权码
mailserver = 'smtp.163.com'
port = '465'
sub = 'TEST RESULT' # 邮件主题

try:
    msg = MIMEMultipart('related')
    msg['From'] = formataddr(["ME", sender])
    msg['To'] = formataddr(["YOU", receive])
    msg['Subject'] = sub

    txt = MIMEText('This is your test result!', 'plain', 'utf-8')
    msg.attach(txt)

# 添加附件,以txt为例,可以改成其他文件格式
    attach = MIMEApplication(open("D:\xx\hi.txt").read())
    attach.add_header('Content-Disposition', 'attachment', filename='hi.txt')
    msg.attach(attach)

    server = smtplib.SMTP_SSL(mailserver, port)
    server.login(sender, passwd)
    server.sendmail(sender, receive, msg.as_string())
    server.quit()
   

更改后:

import smtplib
from email.mime.text import MIMEText
from email.utils import formataddr
from email.mime.application import MIMEApplication
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart

sender = 'XXX@163.com' # 发件人地址
receive = 'XXX@qq.com,XXX@163.com' # 收件人地址 
passwd = 'XXX' # 授权码
mailserver = 'smtp.163.com'
port = '465'
sub = 'TEST RESULT' # 邮件主题

try:
    msg = MIMEMultipart('related')
    msg['From'] = formataddr(["ME", sender])
    msg['To'] = formataddr(["YOU,ME", receive])
    msg['Subject'] = sub

    txt = MIMEText('This is your test result!', 'plain', 'utf-8')
    msg.attach(txt)

# 添加附件,以txt为例,可以改成其他文件格式
    attach = MIMEApplication(open("D:\xx\hi.txt").read())
    attach.add_header('Content-Disposition', 'attachment', filename='hi.txt')
    msg.attach(attach)

    server = smtplib.SMTP_SSL(mailserver, port)
    server.login(sender, passwd)
    server.sendmail(sender, receive, msg.as_string())
    server.quit()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值