Skip to main content
 首页 » 编程设计

python之当我使用 Mime 从 Python 发送时,Outlook 和 Thunderbird 收不到附件

2025年02月15日11飞鱼

我试图在 python 中使用 MimeBase 发送带附件的电子邮件。当我不使用 Thunderbird 或 Outlook 并在浏览器中打开邮箱时,我什至可以发送电子邮件和接收 pdf 附件。

mensagem = MIMEMultipart('alternative') 
mensagem['Subject'] = Header(sTituloEmail.encode('utf-8'), 'UTF-8').encode() 
mensagem['To'] = Header(sEmailTo.encode('utf-8'), 'UTF-8').encode() 
mensagem['CC'] = Header(sEmailCC.encode('utf-8'), 'UTF-8').encode() 
mensagem['From'] = Header(sEmailFrom.encode('utf-8'), 'UTF-8').encode() 
 
# Corpo da mensagem 
mensagem.attach(MIMEText(sTextEmail.encode('utf-8'), 'html', 'UTF-8')) 
 
## Arquivos anexos. 
mime = MIMEBase('application', 'x-pdf') #I've used pdf too and did the same thing 
mime.set_payload(open('out.pdf', 'rb').read()) 
encoders.encode_base64(mime) 
mime.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename('out.pdf')) 
mensagem.attach(mime) 

请您参考如下方法:

确保您的电子邮件是 mensagem = MIMEMultipart('mixed')
并尝试 mime = MIMEBase('application', "octet-stream")。

如果这没有帮助,请与您分享完整的代码。