smtplib.SMTPAuthenticationError: (534, b'5.7.9 需要应用程序专用密码。了解更多信息,请访问\n5.7.9 https://support.google.com/mail/?p=InvalidSecondFactor h22sm15927247pfv.25 - gsmtp')
环境变量已设置但不起作用(即使它是确切的值)。
export EMAIL_USER='da24@gmail.com'os.environ.get('EMAIL_USER')邮件值字符串是否为True我试图将其导入配置文件中,当我使用编码值时,它工作正常,但使用 I use 时os.environ.get('EMAIL_USER'),它会停止工作并引发此错误。
我想通过代理发送电子邮件.
我目前的实施如下:
我通过身份验证连接到smtp服务器.我成功登录后,发送电子邮件.它工作正常,但当我看到电子邮件标题时,我可以看到我的主机名.我想通过代理隧道它.
任何帮助将受到高度赞赏.
我正在尝试使用Python smtplib将电子邮件的优先级设置为高.我已成功使用此库发送电子邮件,但我不确定如何使优先级正常工作.
import smtplib
from smtplib import SMTP
Run Code Online (Sandbox Code Playgroud)
我的第一次尝试是通过研究如何设置优先级来使用它:
smtp.sendmail(from_addr, to_addr, msg, priority ="high")
Run Code Online (Sandbox Code Playgroud)
但是我收到了一个错误: keyword priority is not recognized.
我也试过用:
msg['X-MSMail-Priority'] = 'High'
Run Code Online (Sandbox Code Playgroud)
但是我得到另一个错误.有没有办法只使用smtplib设置优先级?
我正在使用smtplib,我正在从我的应用程序发送通知电子邮件.但是我有时会注意到(特别是当邮件发送之间有很多空闲时间时)我收到SMTPServerDisconnected错误.
我想这有两个解决方案(不知道它们都没有)
我认为第二种解决方案似乎更优雅.但是我该怎么做呢?
编辑:我正在添加代码
from smtplib import SMTP
smtp = SMTP()
smtp.connect('smtp.server.com')
smtp.login('username','password')
def notifyUser():
smtp.sendmail(from_email, to_email, msg.as_string())
Run Code Online (Sandbox Code Playgroud) 我想知道我是否可以通过smtplib发送matplotlib pyplot.我的意思是,在我绘制这个数据帧之后:
In [3]: dfa
Out[3]:
day imps clicks
70 2013-09-09 90739468 74609
69 2013-09-08 90945581 72529
68 2013-09-07 91861855 70869
In [6]: dfa.plot()
Out[6]: <matplotlib.axes.AxesSubplot at 0x3f24da0>
Run Code Online (Sandbox Code Playgroud)
我知道我可以看到情节使用
plt.show()
Run Code Online (Sandbox Code Playgroud)
但是对象本身存储在哪里?或者我误解了关于matplotlib的一些事情?有没有办法将它转换为python中的图片或html所以我可以通过smtplib发送它?谢谢!
我只想在python中发送带附件的电子邮件
import smtplib, os
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email.Utils import COMMASPACE, formatdate
from email import Encoders
def send_mail(send_from, send_to, subject, text, files=[], server="localhost"):
assert type(send_to)==list
assert type(files)==list
msg = MIMEMultipart()
msg['From'] = send_from
msg['To'] = COMMASPACE.join(send_to)
msg['Date'] = formatdate(localtime=True)
msg['Subject'] = subject
msg.attach( MIMEText(text) )
for f in files:
part = MIMEBase('application', "octet-stream")
part.set_payload( open(f,"rb").read() )
Encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(f))
msg.attach(part)
smtp = smtplib.SMTP('smtp.gmail.com:587')
smtp.login('fu@gmail.com','fu')
smtp.sendmail(send_from, send_to, msg.as_string())
smtp.close() …Run Code Online (Sandbox Code Playgroud) 我有一个需要无限期运行的脚本。该脚本设置为通过电子邮件向我发送每天已完成的某些步骤的确认信息。我正在尝试为此使用 smtplib。
初始连接已设置,以便我将在脚本启动时使用 getpass 输入我的登录名(写入脚本)和密码。我不想将我的密码写入脚本中,甚至不想让脚本在配置文件中引用。因此,我想在启动时输入密码并保留 smtp 连接。
按照脚本中的要求重新连接到 smtp 连接将无法完全脱离脚本并让它无限期运行。
我目前正在使用的示例代码如下所示:
import smtplib
import getpass
smtpObj = smtplib.SMTP('smtp.gmail.com',587)
smtpObj.ehlo()
smtpObj.starttls()
smtpObj.login('myemail@gmail.com',password = getpass.getpass('Enter Password: '))
Run Code Online (Sandbox Code Playgroud)
然后我输入密码,输出是:
(235, b'2.7.0 Accepted')
Run Code Online (Sandbox Code Playgroud)
所以这一切正常。
问题是脚本需要根据时间暂停几分钟到几天不等。这是使用带有时间条件的 while 循环来实现的,直到调用发送函数的某个时间:
smtpObj.sendmail('myemail@gmail.com','recipient@gmail.com','This is a test')
Run Code Online (Sandbox Code Playgroud)
然而,经过大约 20/30 分钟的时间后,似乎(即,如果暂停就足够了)。然后 smptObj.sendmail 调用将由于超时错误而失败。
具体错误如下:
SMTPSenderRefused: (451, b'4.4.2 Timeout - closing connection. l22sm2469172wre.52 - gsmtp', 'myemail@gmail.com')
Run Code Online (Sandbox Code Playgroud)
到目前为止,我已经尝试了以下方法:
使用以下超时参数化实例化连接对象:
smtpObj = smtplib.SMTP('smtp.gmail.com',587,timeout=None)
smtpObj = smtplib.SMTP('smtp.gmail.com',587,timeout=86400)
Run Code Online (Sandbox Code Playgroud)
这些似乎都没有抑制连接的“超时”(即同样的问题仍然存在)。
我也尝试过这篇文章中建议的这种解决方案:
如何使用 smtplib 和 Python 打开 SMTP 连接?
然而,这也没有奏效!
我确实想尝试避免每次发送电子邮件时都必须重新连接的解决方案,因为我只想手动输入一次连接密码,而不是将其写入直接或间接编写脚本。
肯定有办法处理超时问题!如果有人可以在这里提供帮助,请告诉我!不过,如果您认为在脚本需要发送电子邮件之前重新连接的更“明显”解决方案是更好的方法,那么请告诉我。
谢谢!...
我有一个脚本,它会定期向收件人列表发送报告。一切正常,直到今天凌晨 4 点,当我检查我的收件箱时,报告没有出现。
通过调试代码:
import smtplib
username="my.user.account@gmail.com"
password="my.correct.password"
server=smtplib.SMTP('smtp.gmail.com',587)
server.ehlo()
server.starttls()
server.ehlo()
server.login(username,password)
#if login worked, it should send a message, but it is not working, so I will suppress this part
server.quit()
Run Code Online (Sandbox Code Playgroud)
我收到以下(旧的)结果:
(250, b'smtp.gmail.com 为您服务, [SERVERIP]\nSIZE 35882577\n8BITMIME\nSTARTTLS\nENHANCEDSTATUSCODES\nPIPELINING\nCHUNKING\nSMTPUTF8') (220, b'2.0.0 准备启动 TLS') ( , b'smtp.gmail.com 为您服务,[SERVERIP]\nSIZE 35882577\n8BITMIME\nAUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH\nENHANCEDSTATUSCODES\nPIPELINING 最近调用\nSMTPCHUNKFile\nSMTPCHUNKFile\n8BITMIME\n <pyshell#52>", line 6, in server.login(username,password) File "C:\Python\Python36\lib\smtplib.py", line 729, in login raise last_exception File "C:\Python\Python36 \lib\smtplib.py",第 720 行,在登录名 initial_response_ok=initial_response_ok) 文件“C:\Python\Python36\lib\smtplib.py”,第 641 行,在 auth 中引发 SMTPAuthenticationError(code, …
我正在尝试通过 python 3.6 中的一个简单函数将 csv 文件作为附件发送。
from email.message import Message
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
def email():
msg = MIMEMultipart()
msg['Subject'] = 'test'
msg['From'] = 'test@gmail.com'
msg['To'] = 'testee@gmail.com'
msg.preamble = 'preamble'
with open("test.csv") as fp:
record = MIMEText(fp.read())
msg.attach(record)
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.login("test@gmail.com", "password")
server.sendmail("test@gmail.com", "testee@gmail.com", msg)
server.quit()
Run Code Online (Sandbox Code Playgroud)
调用email()产生错误expected string or bytes-like object。重新定义server.sendmail("test@gmail.com", "testee@gmail.com", msg)为server.sendmail("atest@gmail.com", "testee@gmail.com", msg.as_string()) 会导致发送电子邮件,但会在电子邮件正文中发送 csv 文件,而不是作为附件发送。谁能给我一些关于如何将 csv 文件作为附件发送的提示?
我使用:Python 2.7.15,OpenSSL 1.1.0h(2018年3月27日),MS Exchange 2007.
我的MS交换允许仅在STARTTLS之后发送登录/通过.
在python中我尝试连接到服务器,如:
from stmplib import SMTP
conn = SMTP(server,port)
conn.set_debuglevel(1)
conn.starttls()
conn.login(username, password)
conn.quit()
Run Code Online (Sandbox Code Playgroud)
最后我得到错误starttls:
/python2.7/ssl.py",第847行,在do_handshake self._sslobj.do_handshake()中
问题是遵循python尝试与TLS v1.2建立连接,但Exchange仅支持TLS v.1.0.我尝试了端口25和587.
当我尝试通过控制台openssl应用程序连接并登录到服务器时,对于具有TLS v.1.0的两个端口都可以正常工作:
openssl s_client -connect sever:587 -starttls smtp -no_tls1_2 -no_tls1_1 -crlf
服务器答案:
SSL handshake has read 1481 bytes and written 530 bytes
--- New, TLSv1/SSLv3, Cipher is RC4-MD5 Server public key is 2048 bit Secure Renegotiation IS supported Compression: NONE Expansion: NONE No ALPN negotiated SSL-Session:
Protocol : TLSv1
Cipher : …Run Code Online (Sandbox Code Playgroud) smtplib ×10
python ×9
email ×3
gmail ×2
python-3.x ×2
smtp ×2
csv ×1
matplotlib ×1
openssl ×1
proxy ×1
python-2.7 ×1
starttls ×1
timeout ×1