通过python与excel xlsx附件发送电子邮件(gmail)时出错
我试图通过python作为主机发送电子邮件,如下所示:
# 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
我已经closures了gmail的安全设置,我认为它会通过,但是这也不起作用。 我正试图发送一个excel(xlsx)附件给我自己的电子邮件。 不知道什么是错的。
编辑:以下build议后出现新错误:
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)
你发送电子邮件,附有一个Excel文件的成功?
通过使用你的方法:
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)
我试图按照你的方式,但我可以收到的.xlsx格式的文件!
总是收到未知的格式文件!