python - 凭据有问题(google smtp)
问题描述
我是一个非常新的Python 编码器,所以请不要对我太苛刻,谢谢。
我正在尝试使用电子邮件发送器smtplib
,但在将用户凭据交给 Google 时遇到了麻烦。
完整代码:
mailcheck = input("This mailer is only for gmail, want to continue? (yes or no)")
# GMAIL SMTP INFORMATION
if mailcheck == "yes" or mailcheck == "Yes" or mailcheck == "y":
smtp_server = 'smtp.gmail.com'
port = 587
set_server = "gmail"
else:
print("Hey, sorry this program is only for Gmail")
#USER CREDENTIALS + RECEIVER
username = input("Google Email?")
password = input("Password?")
receiver = input("Who to send it to?")
subject = input("Subject of the email?")
econtents = input("What to put in the email?")
amount = input("Amount of emails to send?")
credentials = username + password
global d1
global d2
try:
server = smtplib.SMTP(smtp_server, port)
server.ehlo()
server.starttls()
server.login(username, password)
print("Sending" + amount, "to", receiver)
for i in range(1, int(amount) + 1):
message = "From" + credentials[0] + '\nSubject' + subject + '\n' + econtents
time.sleep(random.uniform(d1, d2))
server.sendmail(credentials[0], receiver, message)
print("\nEmail Sent!")
else:
print("Finished sending emails")
except smtplib.SMTPRecipientsRefused:
print("Recipient refused. Invalid email address?")
except smtplib.SMTPAuthenticationError:
print("Unable to authenticate with server. Ensure the details are correct.")
except smtplib.SMTPServerDisconnected:
print("Server disconnected for an unknown reason.")
except smtplib.SMTPConnectError:
print("Unable to connect to server.")
错误 :
Unable to authenticate with server. Ensure the details are correct.
这意味着登录过程出错了。这部分的某个地方应该出错了:
#USER CREDENTIALS + RECEIVER
username = input("Google Email?")
password = input("Password?")
receiver = input("Who to send it to?")
subject = input("Subject of the email?")
econtents = input("What to put in the email?")
amount = input("Amount of emails to send?")
credentials = username + password
global d1
global d2
try:
server = smtplib.SMTP(smtp_server, port)
server.ehlo()
server.starttls()
server.login(username, password)
print("Sending" + amount, "to", receiver)
for i in range(1, int(amount) + 1):
message = "From" + credentials[0] + '\nSubject' + subject + '\n' + econtents
time.sleep(random.uniform(d1, d2))
server.sendmail(credentials[0], receiver, message)
print("\nEmail Sent!")
我认为这是因为credentials = username + password
which 不起作用,但我不知道如何解决它。
如果有人知道我必须改变什么来解决这个问题,那就太好了!
解决方案
不是添加这两个字符串,而是将它们放在一个数组中。在 Python 中,要么
credentials = [username, password]
或者
credentials = list(username, password)
But that doesn't seem to be your issue. Your issue is related to the login() function as you get the SMTPAuthenticationError exception. The smtplib documentation says that after running .starttls(), you should run .ehlo() again. Try to run that before logging in. Additionally, you could try to generate an SSL instance on port 465.
(Ctrl+F for .starttls())
推荐阅读
- visual-studio-code - 如何清除 Plugin svelte: A11y: on:blur error/warning?
- windows - How can I rename file extensions to another extension type, on another directory?
- javascript - 数组在本地机器上是可迭代的,但在 Leetcode 上不行?
- python - 如何将字典列表转换为元组列表?
- r - Linear regression: calculate confidence and prediction intervals with the standard errors of the fitted parameters the correlation coefficient
- python - 是否可以按宽度扩展冻结神经网络模型的节点?
- authentication - POST authentication on upstream server in NGINX
- c# - Unity地铁冲浪者碰撞
- c++ - C++ linker error while using static member of a class
- excel - Setting DataBodyRange2 = DataBodyRange1