使用 Gmail Python 发送电子邮件

2023-12-29

我正在尝试发送电子邮件,但遇到此错误: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')

在网址中我没有看到任何超级有用的东西,有人有任何提示吗?出于这样的目的,我将电子邮件帐户密码保留为test与分享我的个人信息相比..

import smtplib
import ssl


# User configuration
sender_email = '[email protected] /cdn-cgi/l/email-protection'
receiver_email = '[email protected] /cdn-cgi/l/email-protection'
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 = "[email protected] /cdn-cgi/l/email-protection" # the email where you sent the email
password = "yourPassword"
send_to_email = "[email protected] /cdn-cgi/l/email-protection" # 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()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 Gmail Python 发送电子邮件 的相关文章

随机推荐

  • 替换除
     标记之间的换行符之外的换行符                
                

    我正在寻找替换 删除给定字符串中的所有换行符 嵌套在 a 中的换行符除外 pre 标签 因此对于以下字符串 var text Some contents which is formatted over multiple lines but
  • 将 3d 4x4 旋转矩阵转换为 2d

    假设我们有一个 4x4 矩阵 其索引如下 00 01 02 03 10 11 12 13 20 21 22 23 30 31 32 33 如何将该矩阵中包含的旋转数据 忽略 z 轴 如果有帮助的话 转换为单个 2d 旋转角度 以弧度为单位
  • VSTS 缺少某些构建标志:env:BUILD_SOURCEVERSIONMESSAGE

    在我的 Visual Studio Team Services 构建中 我从 bitbucket 存储库中提取 我正在尝试获取提交消息并在 powershell 脚本中使用它 在我的 powershell 脚本中 我有以下代码 param
  • 不明白这个 TypeError: 'list' object is not callable

    我有一个名为im py其中包含几个函数和几个类 第一个函数和类失败了 TypeError list object is not callable 问题似乎是在函数中创建的列表的第一个元素 然后将其传递给类 列表的大部分可以由类处理 但第一个
  • 如何在 C# 中使用管道和 let 参数进行 $lookup (MongoDB.Driver 2.7.2)

    我需要在我的 C 代码中在 MongoDB 中运行以下查询 我使用 MongoDB Driver 2 7 2 和 MongoDB 4 0 我想使用 lookup 而不是 project unwind 以防止命名集合的每个可能字段 db ge
  • std::initializer_list 的实现

    我一直在研究如何initializer list已实现 所以我找到了标准的第 18 9 节 并找到了一个看起来足够简单的界面 我认为制作我自己的版本 我命名为 会很有启发性MyNamespace InitializerList和一个用例 t
  • 在mfc中如何将控件置于前面

    如何更改 MFC 中控件的 Z 顺序在设计时 即我无法使用 SetWindowPos 或在运行时执行此操作 我想在设计器中查看更改后的 z 顺序 即使我必须诉诸直接编辑 rc 代码 我有一个 MFC 对话框 要向其中添加控件 如果控件的边缘
  • 在 tvOS 上对 Dropbox 进行身份验证

    我在我的 ios 移动应用程序中使用 dropbox sdk 它使用 dropbox 身份验证从我的应用程序中的 dropbox 中获取用户文件 它在我的 ios 应用程序上完美运行 并上传到苹果商店 我想让它也适用于 tvos 苹果商店
  • EC2 上的轻度、中度或重度利用率预留实例之间有什么区别?

    他们讨论了不同的利用率实例 但除了价格之外 我找不到任何一个地方可以实际说明轻利用率大型实例和重利用率大型实例之间的区别 有人可以告诉我有什么区别吗 实例是相同的 这只是价格差异 因此如果您知道自己将大量使用该实例 则可以通过支付预付费用来
  • Mandelbrot 集渲染的平滑频谱

    我目前正在编写一个程序来生成非常巨大的 65536x65536 像素及以上 Mandelbrot 图像 我想设计一个光谱和着色方案来使它们公正 这维基百科精选曼德尔布罗图像 http en wikipedia org wiki File M
  • 最有效的多重纹理方法 - iOS、OpenGL ES2、优化

    我正在尝试找到在 iOS 上处理 OpenGL ES2 中多重纹理的最有效方法 我所说的 高效 是指即使在较旧的 iOS 设备 iPhone 4 及更高版本 上也能实现最快的渲染 但同时还要平衡便利性 我考虑过 并尝试过 几种不同的方法 但
  • 单个 Logger 的每个附加程序的日志级别

    是否可以根据appender为单个Logger配置不同的日志级别 我意识到这与此类似question https stackoverflow com questions 1839647 how to configure log4j to l
  • 何时在 Spring 中使用自动装配

    我正在看书专业春季3 https rads stackoverflow com amzn click com 1430241071 其中有一段确实让我很困惑 该段落是关于 Spring 中的自动装配 以下是摘录 在大多数情况下 是否应该使用
  • 合成器外观和感觉中的默认按钮输入映射?

    我正在尝试使用 UIManager 获取并清除一些默认键绑定 以便空格键不会激活我的 JButtons 如所解释的here https stackoverflow com questions 12133795 removing defaul
  • strtok()函数的实现

    我需要编写我的函数 strtok 下面是我的代码 问题是 我无法显示结果字符串 在我使用的代码中strcpy 然后显示新数组 是否可以仅使用指针显示字符串 str include
  • 使用 Spring DispatcherServlet 自定义 404

    我已设置 web xml 如下 我还有一个基于注释的控制器 它接受任何 URL 模式 然后转到相应的 jsp 我已在 servlet xml 中进行了设置 但是 如果我转到以 html 结尾的页面 并且其 jsp 不存在 我不会看到自定义
  • Selenium 访问框架内的元素时出现问题

    我在验证由框架集和框架组成的页面中的元素时遇到问题 我正在使用代码 selenium selectFrame relative up selenium selectFrame topFrame 但它失败并出现错误 未找到元素 topFram
  • J2ME 支持 HTTP PUT 吗?

    我刚刚注意到 MIDP 2 0 API 中的一个奇怪的事情 HttpConnection 类 apidocs 明确引用了方法 GET POST 和 HEAD 但没有其他方法 这是否意味着它们不受支持 http java sun com ja
  • 如何在 powershell 中列出所有已安装、可运行的 cmdlet?

    我想列出 powershell 中所有已安装 可运行的 cmdlet 和函数 但是Get Command正在列出以某种方式 存在 但未加载且不可运行的 cmdlet 举个例子 Get Command lists New IseSnippet
  • 使用 Gmail Python 发送电子邮件

    我正在尝试发送电子邮件 但遇到此错误 smtplib SMTPAuthenticationError 534 b 5 7 9 Application specific password required Learn more at n5 7