Python常规MIMEText电子邮件有效,但MIMEMultiPart不起作用

问题描述

我有以下适用于Python的代码,可以在公司网络内发送电子邮件

import smtplib
from email.mime.text import MIMEText

#Define the message
msg = MIMEText("Email Body")
msg['Subject'] = "Subject Line"
msg['From'] = "[email protected]"
msg['To'] = "[email protected]"

#Connect and send the email
server = smtplib.SMTP('blahblahblah.company.com',25)
server.ehlo()
server.starttls()
server.sendmail("[email protected]","[email protected]",msg.as_string())
server.quit()    

现在,我要向其中添加PDF附件。我查看了MIMEMultiPart电子邮件上用于发送附件的许多线程,因此我模拟了该版本:

import smtplib
import os
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart

#Define the message
pdf_output_path = os.path.join(os.getcwd(),"Form.pdf")
msg = MIMEMultipart()
msg['Subject'] = "Subject Line"
msg['From'] = '[email protected]'
msg['To'] = "[email protected]"
msg.attach(MIMEText("Email Body"))
with open(pdf_output_path,"rb") as fil:
    part = MIMEApplication(
        fil.read(),Name=os.path.basename(pdf_output_path)
    )
part['Content-disposition'] = 'attachment; filename="%s"' % os.path.basename(pdf_output_path)
msg.attach(part)

#Connect and send the email
server = smtplib.SMTP('blahblahblah.company.com',msg.as_string())
server.quit()    

但是,这根本不发送电子邮件。它不会崩溃或留下任何输出错误消息,但不会发送电子邮件。当我注释掉添加附件的代码部分时,我得到相同的结果。

有人对发生的事情有想法吗?通常,我只是查找错误,但是由于没有任何反馈,我不仅不知所措,而且还很好奇它为什么能够成功运行但什么也没做。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)