python - 如何使用 python 通过我的 hotmail.com 帐户自动发送电子邮件?

标签 python microsoft-graph-api

因此,我一直在尝试使用我的 @hotmail.com 帐户以编程方式发送电子邮件。

到目前为止我已经成功做到了:

  1. 在此处创建应用程序 https://apps.dev.microsoft.com/#/appList并有:
    1. 应用程序 ID
    2. “应用程序 secret ”下的密码
    3. 在平台下,我添加了一个网络和一个 native 应用程序
    4. 在图表权限下,我拥有委派权限“Mail.Send”和“User.Read”
  2. 我已成功运行 this通过浏览器发送电子邮件的代码。
    1. 但是,作为身份验证过程的一部分,我被要求登录。

问题如下:

如何通过 python 并使用我的 @hotmail.com 帐户发送电子邮件,而无需每次都提供任何登录凭据?

理想的解决方案是一个简单的 sendmail(to, subject, body) (或类似的)和一个带有应用程序特定密码的配置文件(我想我已经有了)。

最初,我希望查看上面链接中的示例 python 代码(重复 here )并尝试对其进行调整,但这不是正确的方法,因为示例代码需要通过浏览器登录。

最佳答案

from email import encoders
from email.message import Message
from email.mime.audio import MIMEAudio
from email.mime.base import MIMEBase
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

def SendEmail():
    s = smtplib.SMTP('smtp.gmail.com', 587) #Change smtp for Outlook
    s.starttls()
    s.login(EmailGoesHere, PASSWORDHere)

    msg = MIMEMultipart()       # create a message

    # add in the actual person name to the message template
    message = '''
        Message Goes here
    '''

    # Prints out the message body for our sake
    # print(message)

    # setup the parameters of the message
    msg['From']=MY_ADDRESS
    msg['To']='ecesisproduction@gmail.com'
    msg['Subject']="Marketplace order Accepted!"

    # add in the message body
    msg.attach(MIMEText(message, 'plain'))

    # send the message via the server set up earlier.
    s.send_message(msg)

    # Terminate the SMTP session and close the connection
    s.quit()

关于python - 如何使用 python 通过我的 hotmail.com 帐户自动发送电子邮件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52907945/

相关文章:

python - CSV 模块 - 'for row in reader' - 在 Mac 和 Windows 之间

javascript - 我可以使用 python3 从 https ://www. rt.com/提取任何页面的评论吗?

python - 作为类属性的实例列表

python - 无法导入 numpy : AttributeError: type object 'numpy.ndarray' has no attribute '__array_function__'

python - Pandas :数据文件中没有列名

azure - 我们的 MS Graph Webhook 未收到删除 AAD 中用户的请求

microsoft-graph-api - 图形 API 在执行初始增量同步时多次返回同一组

botframework - 如何获取组织中的 MS Teams 用户列表?

Azure:从 MySQL 数据库创建 O365 事件

Python 使用 Graph API 和 Office365-REST-Python-Client 发送电子邮件