我正在尝试使用Python将电子邮件发送到收件人列表,但被告知先连接。
import smtplib
try:
s = smtplib.SMTP('smtp.xxx.com', 587)
s.starttls()
s.login('[email protected]', 'xxx')
message = '''
...message text...
'''
s.connect()
with open('players.txt') as f:
email = f.readlines()
email = [e.strip() for e in email]
for person in range(len(email)):
print('Sending email to {}'.format(email[person]))
s.sendmail('barbaramilleraz', email[person], message)
except(smtplib.SMTPSenderRefused):
pass
输出为:
C:\Users\BMiller>python mailing.py
Sending email [email protected]
Sending email to [email protected]
Traceback (most recent call last):
File "mailing.py", line 25, in <module>
s.sendmail('barbaramilleraz', email[person], message)
File "C:\Users\BMiller\AppData\Local\Programs\Python\Python37\lib\smtplib.py", line 852, in sendmail
self.ehlo_or_helo_if_needed()
File "C:\Users\BMiller\AppData\Local\Programs\Python\Python37\lib\smtplib.py", line 600, in ehlo_or_helo_if_needed
if not (200 <= self.ehlo()[0] <= 299):
File "C:\Users\BMiller\AppData\Local\Programs\Python\Python37\lib\smtplib.py", line 440, in ehlo
self.putcmd(self.ehlo_msg, name or self.local_hostname)
File "C:\Users\BMiller\AppData\Local\Programs\Python\Python37\lib\smtplib.py", line 367, in putcmd
self.send(str)
File "C:\Users\BMiller\AppData\Local\Programs\Python\Python37\lib\smtplib.py", line 359, in send
raise SMTPServerDisconnected('please run connect() first')
smtplib.SMTPServerDisconnected: please run connect() first
我是Python的新手,所以不确定如何继续。
谢谢。
参考方案
如dcg在评论中所述,您必须在发送电子邮件之前呼叫s.connect()
建立连接。
您提到在执行此操作时它仅发送一次:这是因为在发送每条消息后要调用s.quit()
。完成此操作后,s
对于所有意图和目的都是无效的:如果要再次使用它,则必须重新启动配置。
因此,在发送所有消息之前,请先致电s.connect()
,在完全完成s.quit()
之前,请勿致电s
。
我需要计算Lindeman-Merenda-Gold(LMG)分数,以进行回归分析。我发现R语言的relaimpo包下有该文件。不幸的是,我对R没有任何经验。我检查了互联网,但找不到。这个程序包有python端口吗?如果不存在,是否可以通过python使用该包? python参考方案 最近,我遇到了pingouin库。
用大写字母拆分字符串,但忽略AAA Python Regex - python我的正则表达式:vendor = "MyNameIsJoe. I'mWorkerInAAAinc." ven = re.split(r'(?<=[a-z])[A-Z]|[A-Z](?=[a-z])', vendor) 以大写字母分割字符串,例如:'我的名字是乔。 I'mWorkerInAAAinc”变成…
Python:传递记录器是个好主意吗? - python我的Web服务器的API日志如下:started started succeeded failed 那是同时收到的两个请求。很难说哪一个成功或失败。为了彼此分离请求,我为每个请求创建了一个随机数,并将其用作记录器的名称logger = logging.getLogger(random_number) 日志变成[111] started [222] start…
Python-Excel导出 - python我有以下代码:import pandas as pd import requests from bs4 import BeautifulSoup res = requests.get("https://www.bankier.pl/gielda/notowania/akcje") soup = BeautifulSoup(res.cont…
Matplotlib'粗体'字体 - python跟随this example:import numpy as np import matplotlib.pyplot as plt fig = plt.figure() for i, label in enumerate(('A', 'B', 'C', 'D')): ax = f…