如何在Python中使用电子邮件ID和密码不使用Graph API读取Outlook电子邮件?

发布于 2025-02-08 20:24:49 字数 84 浏览 1 评论 0 原文

我无法使用PYWIN32 -MAPI在本地运行我的脚本,因为它将在机器上安装的Outlook应用程序上托管在服务器/远程桌面上。 你能帮我吗? 提前致谢。

I cannot run my script locally using pywin32 - MAPI as it is to be hosted on a server/remote desktop without Outlook application installed on the machine.
Can you please help me with this?
Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

岁月打碎记忆 2025-02-15 20:24:49

如果仅处理Exchange帐户,则还可以考虑使用EWS,请参见在Exchange中探索EWS托管的API,EWS和Web Services 以获取更多信息。

If you deal with Exchange accounts only you may also consider using EWS, see Explore the EWS Managed API, EWS, and web services in Exchange for more information.

长梦不多时 2025-02-15 20:24:49
  1. 确保您有Azure P2订阅。
  2. 查看以下链接并获取已注册的应用程序,并将权限分配给相同。
  3. 创建“ creds.py”文件并添加predentisl-base_path-base_path,client_id,client_secret,object_id,object_id,scope,scope,url_access_token,url_access_token,url_access_token,url_access_token ,tenant_id,url_attachments,url_messages作为字符串。 (请参阅上面的链接以了解每个链接的含义以及如何获得它们的值)。

以下代码检查电子邮件正文中是否存在特定的字符串“ body_tag”,然后在此处(如果存在)下载该特定电子邮件的所有附件。另外,它只读取“未读”电子邮件。

代码

import requests
import base64
from Creds import base_path, client_id, client_secret, object_id, scope, url_access_token, tenant_id, url_attachments, url_messages
# Access Token
url_access_token = url_access_token.format(tenant_id)
body_tag = ""
payload = {
  'client_id': client_id,
  'scope': scope,
  'client_secret': client_secret,
  'grant_type': 'client_credentials',
  'Content-Type': 'application/x-www-form-urlencoded'
  }
files=[]
headers = {}
response = requests.request("POST", url_access_token, headers=headers, data=payload, files=files)
access_token = response.json()['access_token']
# Get Emails
url_messages = url_messages.format(object_id)
payload={}
files={}
headers = {
  "Authorization": "Bearer " + access_token
}
response = requests.request("GET", url_messages, headers=headers, data=payload, files=files)
emails = response.json()['value']
for email in emails:
  body = email['body']
  if(body_tag in str(body)):
    message_id = ""
    if(email['hasAttachments']):
      message_id = email['id']
      # Get the attachments
      if(len(message_id) > 0):
        # Attachments
        url_attachments = url_attachments.format(object_id, message_id)
        response = requests.request("GET", url_attachments, headers=headers, data=payload)
        val = response.json()["value"]
        for i in range(len(val)):
          attachment_id = val[i]["id"]
          attachment_name = val[i]["name"]
          attachment_content_type = val[i]["contentType"]
          content_bytes = val[i]["contentBytes"]
          base64String = content_bytes
          with open(base_path+attachment_name, 'wb') as theFile:
            theFile.write(base64.b64decode(base64String))
  1. Make sure you have Azure P2 subscription.
  2. Look into the following link and get an app registered and assign permissions to the same. https://www.linkedin.com/pulse/reading-o365-emails-from-daemon-process-using-graph-bhattacharya/
  3. Create "Creds.py" file and add the credentisl - base_path, client_id, client_secret, object_id, scope, url_access_token, tenant_id, url_attachments, url_messages to it as strings to it. (refer the link above to understand what each of it means and how to get the values of them).

The following code checks if a particular string "body_tag" is present in the body of an email and then downloads all the attachments of that particular email if it is present. Also, it only reads the "unread" emails.

CODE

import requests
import base64
from Creds import base_path, client_id, client_secret, object_id, scope, url_access_token, tenant_id, url_attachments, url_messages
# Access Token
url_access_token = url_access_token.format(tenant_id)
body_tag = ""
payload = {
  'client_id': client_id,
  'scope': scope,
  'client_secret': client_secret,
  'grant_type': 'client_credentials',
  'Content-Type': 'application/x-www-form-urlencoded'
  }
files=[]
headers = {}
response = requests.request("POST", url_access_token, headers=headers, data=payload, files=files)
access_token = response.json()['access_token']
# Get Emails
url_messages = url_messages.format(object_id)
payload={}
files={}
headers = {
  "Authorization": "Bearer " + access_token
}
response = requests.request("GET", url_messages, headers=headers, data=payload, files=files)
emails = response.json()['value']
for email in emails:
  body = email['body']
  if(body_tag in str(body)):
    message_id = ""
    if(email['hasAttachments']):
      message_id = email['id']
      # Get the attachments
      if(len(message_id) > 0):
        # Attachments
        url_attachments = url_attachments.format(object_id, message_id)
        response = requests.request("GET", url_attachments, headers=headers, data=payload)
        val = response.json()["value"]
        for i in range(len(val)):
          attachment_id = val[i]["id"]
          attachment_name = val[i]["name"]
          attachment_content_type = val[i]["contentType"]
          content_bytes = val[i]["contentBytes"]
          base64String = content_bytes
          with open(base_path+attachment_name, 'wb') as theFile:
            theFile.write(base64.b64decode(base64String))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文