通过 python 发送带有 excel xlsx 附件的电子邮件 (gmail) 时出错
Error sending email (gmail) via python with excel xlsx attachment
我正在尝试通过 python 以 gmail 作为主机发送电子邮件,如下所示:
# Import smtplib for the actual sending function
import smtplib
# For guessing MIME type
import mimetypes
# Import the email modules we'll need
import email
import email.mime.application
# Create a text/plain message
msg = email.mime.Multipart.MIMEMultipart()
msg['Subject'] = 'Greetings'
msg['From'] = 'x@gmail.com'
msg['To'] = 'x@gmail.com'
# The main body is just another attachment
body = email.mime.Text.MIMEText("""Hello, how are you? I am fine.
This is a rather nice letter, don't you think?""")
msg.attach(body)
# PDF attachment
filename=r'C:\Users\cost9\OneDrive\Documents\PYTHON\TEST0530\testingemail.xlsx'
fp=open(filename,'rb')
att = email.mime.application.MIMEApplication(fp.read(),_subtype="xlsx")
fp.close()
att.add_header('Content-Disposition','attachment',filename=filename)
msg.attach(att)
# send via Gmail server
# NOTE: my ISP, Centurylink, seems to be automatically rewriting
# port 25 packets to be port 587 and it is trashing port 587 packets.
# So, I use the default port 25, but I authenticate.
s = smtplib.SMTP("smtp.gmail.com")
s.starttls()
s.login('x@gmail.com','password')
s.sendmail('x@gmail.com',['x@gmail.com'], msg.as_string())
s.quit()
不幸的是,我在计算了大约 20 秒后得到了错误:
error: [Errno 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
我认为我已经关闭了 gmail 的安全设置,这样它就会通过,但这也不起作用。我正在尝试将带有电子邮件的 excel (xlsx) 附件发送给自己。不知道怎么了。
编辑:以下建议后的新错误:
SMTPAuthenticationError: (534, '5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbud\n5.7.14 Ptkk7FoIWgMYWUepxcLLdzPhOBbY5hJDbghJm9AqpU61dWd3pVjuxVaNzkiIyiT0gpijJl\n5.7.14 PQU08uhdkGEPOoo2LatNeL-W0_IiJi3GzdhBBd57yr77BxAYfdY6qMF4CtxW1UmbborYgl\n5.7.14 6Az7m2-ULUcjo96qXX31S2wKGN-XWcmd3F-SagzxJax-8v-KoloZlN1BBQM4ATPikNnlB9\n5.7.14 UpPRBSGiG8fE_mi4d-y345I8EJEJU> Please log in via your web browser and\n5.7.14 then try again.\n5.7.14 Learn more at\n5.7.14 https://support.google.com/mail/answer/78754 93sm7663257iod.17 - gsmtp')
改后试试
s = smtplib.SMTP("smtp.gmail.com")
至
s = smtplib.SMTP("smtp.gmail.com", 587)
我修复了更正 MIME 类型的相同问题:
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
我正在尝试通过 python 以 gmail 作为主机发送电子邮件,如下所示:
# Import smtplib for the actual sending function
import smtplib
# For guessing MIME type
import mimetypes
# Import the email modules we'll need
import email
import email.mime.application
# Create a text/plain message
msg = email.mime.Multipart.MIMEMultipart()
msg['Subject'] = 'Greetings'
msg['From'] = 'x@gmail.com'
msg['To'] = 'x@gmail.com'
# The main body is just another attachment
body = email.mime.Text.MIMEText("""Hello, how are you? I am fine.
This is a rather nice letter, don't you think?""")
msg.attach(body)
# PDF attachment
filename=r'C:\Users\cost9\OneDrive\Documents\PYTHON\TEST0530\testingemail.xlsx'
fp=open(filename,'rb')
att = email.mime.application.MIMEApplication(fp.read(),_subtype="xlsx")
fp.close()
att.add_header('Content-Disposition','attachment',filename=filename)
msg.attach(att)
# send via Gmail server
# NOTE: my ISP, Centurylink, seems to be automatically rewriting
# port 25 packets to be port 587 and it is trashing port 587 packets.
# So, I use the default port 25, but I authenticate.
s = smtplib.SMTP("smtp.gmail.com")
s.starttls()
s.login('x@gmail.com','password')
s.sendmail('x@gmail.com',['x@gmail.com'], msg.as_string())
s.quit()
不幸的是,我在计算了大约 20 秒后得到了错误:
error: [Errno 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
我认为我已经关闭了 gmail 的安全设置,这样它就会通过,但这也不起作用。我正在尝试将带有电子邮件的 excel (xlsx) 附件发送给自己。不知道怎么了。
编辑:以下建议后的新错误:
SMTPAuthenticationError: (534, '5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbud\n5.7.14 Ptkk7FoIWgMYWUepxcLLdzPhOBbY5hJDbghJm9AqpU61dWd3pVjuxVaNzkiIyiT0gpijJl\n5.7.14 PQU08uhdkGEPOoo2LatNeL-W0_IiJi3GzdhBBd57yr77BxAYfdY6qMF4CtxW1UmbborYgl\n5.7.14 6Az7m2-ULUcjo96qXX31S2wKGN-XWcmd3F-SagzxJax-8v-KoloZlN1BBQM4ATPikNnlB9\n5.7.14 UpPRBSGiG8fE_mi4d-y345I8EJEJU> Please log in via your web browser and\n5.7.14 then try again.\n5.7.14 Learn more at\n5.7.14 https://support.google.com/mail/answer/78754 93sm7663257iod.17 - gsmtp')
改后试试
s = smtplib.SMTP("smtp.gmail.com")
至
s = smtplib.SMTP("smtp.gmail.com", 587)
我修复了更正 MIME 类型的相同问题:
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'