使用excel xlsx附件通过python发送电子邮件(gmail)时出错

时间:2017-05-31 12:22:35

标签: python excel email 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')

3 个答案:

答案 0 :(得分:1)

更改后尝试

npm

devServer: {
  watchOptions: {
    ignored: /node_modules/
  }
}

答案 1 :(得分:0)

您是否成功发送附有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格式文件!!

总会收到未知的格式文件!!!!

答案 2 :(得分:0)

我解决了更正MIME类型的相同问题:

'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'