【问题标题】:Google SMTP less secure applicationsGoogle SMTP 不太安全的应用程序
【发布时间】:2018-05-07 04:57:27
【问题描述】:

我正在尝试使用以下简单的 python 和邮件客户端通过 Google SMTP 服务器发送电子邮件。

我对 Google 将此脚本标记为不安全并要求我允许不太安全的应用程序访问发件人的 gmail 帐户的部分感到有些困惑。

有什么方法可以解决这个问题,而不必让不太安全的应用程序访问我的 gmail 帐户。

#Make necessary imports

import mailclient

#Craft the message
msg = mailclient.Message("This will be the subject", "This will be the body content", 'sender@gmail.com', 'recipient@domain.com')

#Create server object with gmail

s = mailclient.Server('smtp.gmail.com', '587', 'sender@gmail.com', 'senderpassword', True)

#Send email

s.send(msg)

【问题讨论】:

  • 它与代码无关。您需要在 google 帐户中启用它。参考 - support.google.com/accounts/answer/6010255?hl=en
  • 我认为可以使用其他邮件客户端,而无需在帐户上启用不太安全的应用程序。我应该在代码中包含哪些内容才能不被标记?
  • 在代码级别对 Google 无能为力。尝试使用 Outlook 或业务 smtp 服务器凭据。

标签: python email gmail smtplib


【解决方案1】:

很难说,因为谷歌对于他们所谓的不安全的应用程序并不是很明确,但我猜它们是使用端口 25 或 587 的应用程序。在这些端口上,最初建立了连接在未加密的通道上,并且仅在(并且如果)发出STARTTLS 命令时才被加密。

所以我想你应该尝试直接在端口 465 上通过 SSL 建立连接。我不知道是否可以使用 mailclient 但使用标准库模块,它应该很简单:

import smtplib
from email.message import EmailMessage

msg = EmailMessage()
msg['Subject'] = "This will be the subject"
msg['From'] = 'sender@gmail.com'
msg['To'] = [ 'recipient@domain.com' ]
msg.set_content("This will be the body content")

server = smtplib.SMTP_SSL('smtp.gmail.com')
server.login('sender@gmail.com', 'senderpassword')
server.send_message(msg)
server.quit()

【讨论】:

    猜你喜欢
    • 2020-08-18
    • 1970-01-01
    • 2014-09-29
    • 2023-01-17
    • 1970-01-01
    • 2020-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多