python - 使用 Gmail Python 发送电子邮件

我正在尝试发送电子邮件,但遇到了这个错误: smtplib.SMTPAuthenticationError: (534, b'5.7.9 需要特定于应用程序的密码。了解更多信息\n5.7.9 https://support.google.com/mail/?p=InvalidSecondFactor d2sm13023190qkl.98 - gsmtp' )

在网址中我没有看到任何 super 有用的东西,有人有什么提示吗?出于 SO 目的,我将电子邮件帐户密码保留为 test 而不是共享我的个人信息..

import smtplib
import ssl


# User configuration
sender_email = 'test@gmail.com'
receiver_email = 'test@gmail.com'
password = 'test'



# Email text
email_body = '''
    This is a test email sent by Python. Isn't that cool?
'''

# Creating a SMTP session | use 587 with TLS, 465 SSL and 25
server = smtplib.SMTP('smtp.gmail.com', 587)
# Encrypts the email
context = ssl.create_default_context()
server.starttls(context=context)
# We log in into our Google account
server.login(sender_email, password)
# Sending email from sender, to receiver with the email body
server.sendmail(sender_email, receiver_email, email_body)
print('Email sent!')

print('Closing the server...')
server.quit()

最佳答案

我尽力了...我认为这应该可行!

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

email = "test@gmail.com" # the email where you sent the email
password = "yourPassword"
send_to_email = "yourEmail@gmail.com" # for whom
subject = "Gmail"
message = "This is a test email sent by Python. Isn't that cool?!"

msg = MIMEMultipart()
msg["From"] = email
msg["To"] = send_to_email
msg["Subject"] = subject

msg.attach(MIMEText(message, 'plain'))

server = smtplib.SMTP("smtp.gmail.com", 587)
server.starttls()
server.login(email, password)
text = msg.as_string()
server.sendmail(email, send_to_email, text)
server.quit()

https://stackoverflow.com/questions/60956725/

相关文章:

firefox - 如何删除 Firefox 中的 X 按钮?

scala - 为什么zio的putStrLn没有输出

node.js - 如何与 nx 并行运行 express 和 angular?

spring-boot - Spring WebFlux Reactive 和 Kotlin Cor

regex - PowerShell 从脚本中删除所有注释

jenkins - 在airflow中的特定时间运行任务

prometheus - 如何在普罗米修斯中获取速率的分位数

java - 从 JSONObject 中删除除一个之外的所有键

sql - Postgres : How do I extract year and month f

angular - 使用 ngrx 和选择器等待资源加载到守卫中