如何使用 Python 以编程方式访问 iCloud 邮件?

问题描述 投票:0回答:1

我正在开发一个 Python 应用程序,我需要从 iCloud 邮件访问和检索电子邮件。从一些搜索看来,iCloud 似乎没有提供任何官方 API,还有其他方法可以通过编程方式执行此操作吗?

应用程序的一个可能任务是列出邮箱中的所有消息。

python email icloud
1个回答
0
投票

电子邮件由 3 种协议管理:IMAPSMTPPOP。 SMTP 用于发送邮件,而 IMAP 和 POP 用于获取邮件。 IMAP 旨在将邮件保存在服务器上,而不是 POP,后者会在您阅读邮件时将其删除。

这些协议是 iCloud 没有 API 的原因:您只需要使用它们即可。例如,this 代表 SMTP。

您可以在此处找到 iCloud 信息。它将为您提供主要信息:

  • 服务器网址:
    imap.mail.me.com
  • 服务器端口:993
  • 需要 SSL
  • 用户名应该是您的电子邮件没有
    @icloud.com
  • 可以按照此处
  • 的说明专门为此应用程序生成密码

在这种情况下,iCloud 支持 IMAP,可以这样使用:

import sys
import chilkat

# This example assumes Chilkat Imap to have been previously unlocked.
# See Unlock Imap for sample code.

imap = chilkat.CkImap()

# Connect to the iCloud IMAP Mail Server
imap.put_Ssl(True)
imap.put_Port(993)
success = imap.Connect("imap.mail.me.com")
if (success != True):
    print(imap.lastErrorText())
    sys.exit()

# The username is usually the name part of your iCloud email address 
# (for example, emilyparker, not [email protected]).
success = imap.Login("ICLOUD_USERNAME","ICLOUD_PASSWORD")
if (success != True):
    print(imap.lastErrorText())
    sys.exit()

# Select an IMAP folder/mailbox
success = imap.SelectMailbox("Inbox")
if (success != True):
    print(imap.lastErrorText())
    sys.exit()

# Once the folder/mailbox is selected, the NumMessages property
# will contain the number of emails in the mailbox.
# Loop from 1 to NumMessages to fetch each email by sequence number.

n = imap.get_NumMessages()
bUid = False
for i in range(1,(n)-1):

    # Download the email by sequence number.
    # email is a CkEmail
    email = imap.FetchSingle(i,bUid)
    if (imap.get_LastMethodSuccess() != True):
        print(imap.lastErrorText())
        sys.exit()

    print(str(i) + ": " + email.ck_from())
    print("    " + email.subject())
    print("-")

# Disconnect from the IMAP server.
success = imap.Disconnect()

print("Success.")

# Sample output:

#   1: iCloud <[email protected]>
#       Welcome to iCloud Mail.
#   -
#   2: "Chilkat Software" <[email protected]>
#       This is a test
#   -
#   Success.

来源这里

© www.soinside.com 2019 - 2024. All rights reserved.