[英]Sending mail through python by using SMTP
為了重現我的問題,這是我的代碼:
import smtplib
from email.message import EmailMessage
email=EmailMessage()
email['from']='Pratik Mishra'
email['to']='pratikmishra79@gmail.com'
email['subject']='Hi there! congratulations you won $10,000'
email.set_content("i am a python master ")
with smtplib.SMTP(host='smtp.gmail.com', port=587) as smtp:
smtp.ehlo()
smtp.starttls()
smtp.login('pratik.jgd17@gmail.com','password')# can't write password here
smtp.send_message(email)
print("hello this was a joke")
我從命令提示符處收到此 output 消息:
C:\Users\prati\Documents>python email_sender.py
Traceback (most recent call last):
File "email_sender.py", line 11, in <module>
smtp.login('pratik.jgd17@gmail.com','password')
File "C:\Users\prati\AppData\Local\Programs\Python\Python38-32\lib\smtplib.py", line 734, in login
raise last_exception
File "C:\Users\prati\AppData\Local\Programs\Python\Python38-32\lib\smtplib.py", line 723, in login
(code, resp) = self.auth(
File "C:\Users\prati\AppData\Local\Programs\Python\Python38-32\lib\smtplib.py", line 646, in auth
raise SMTPAuthenticationError(code, resp)
smtplib.SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8 https://support.google.com/mail/?p=BadCredentials a23sm994647pfo.145 - gsmtp')
根據我的經驗,這意味着三件事之一。 1,您的用戶名和密碼不正確,2,您沒有在您的 Google 帳戶上啟用允許安全性較低的應用程序,或者 3,您啟用了兩步驗證。 您可以按照以下步驟啟用安全性較低的應用程序:
在“允許不太安全的應用程序:關閉”旁邊,select 將撥動開關打開。
如果您想將它與兩步驗證一起使用,您需要為您的谷歌帳戶生成一個個人訪問令牌並將其用作您的密碼, 這里
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.