我试图通过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


我已经关闭了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)

关于python - 通过带有excel xlsx附件的python发送电子邮件(gmail)时出错,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44284713/

10-10 18:03
查看更多