【发布时间】:2017-06-16 13:45:50
【问题描述】:
在运行以下 python 脚本以使用 SMTP 发送 pdf 附件时,遇到 SMTPDataError 异常。我可以使用以下代码将文本或图像文件发送给同一发件人和同一收件人。 pdf 文件大小几乎没有 1 MB。
import smtplib
import email
import email.mime
import email.mime.application
from_email = "XXXX@gmail.com"
from_passwd = ""
to_email = "abc@gmail.com"
message = email.mime.Multipart.MIMEMultipart('mixed')
message['Subject'] = 'Test_run'
message['From'] = from_email
message['To'] = to_email
text_part = email.mime.Text.MIMEText("""This is an e-mail message to be sent in HTML format
<b>This is HTML message.</b>
<h1>This is headline.</h1>
""",'html')
message.attach(text_part)
filename1 = "some_doc.pdf"
fp = open(filename1 , 'rb')
attach_part = email.mime.application.MIMEApplication(fp.read(),"pdf")
fp.close()
attach_part.add_header('Content-Disposition','attachment',filename = "some_doc.pdf")
message.attach(attach_part)
server = smtplib.SMTP("smtp.gmail.com",587)
server.starttls()
server.login(from_email,from_passwd)
server.sendmail(from_email,to_email,message.as_string())
server.close()
遇到的错误信息
File "/usr/lib/python2.7/smtplib.py", line 746, in sendmail
raise SMTPDataError(code, resp)
smtplib.SMTPDataError: (550, '5.7.1 The user or domain that you are sending to (or from) has a policy that\n5.7.1 prohibited the mail that you sent. Please contact your domain\n5.7.1 administrator for further details. For more information, please visit\n5.7.1 https://support.google.com/a/answer/172179 66sm37804549pfx.29 - gsmtp')
非常感谢任何帮助。
【问题讨论】:
-
您是否尝试过阅读错误消息并按照其说明操作?