繁体   English   中英

使用 Gmail Python 发送电子邮件

[英]send email with Gmail Python

我正在尝试发送电子邮件,但遇到此错误: smtplib.SMTPAuthenticationError: (534, b'5.7.9 Application-specific password required. Learn more at\\n5.7.9 https://support.google.com/mail/?p=InvalidSecondFactor d2sm13023190qkl.98 - gsmtp')

在网址中,我没有看到任何非常有用的东西,有人有任何提示吗? 出于 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()

您必须在 Google 配置中允许“安全性较低的应用”。

这是关于它的另一个线程的链接: 链接

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM