在 Python 中使用 IGmailClient 管理 Gmail 消息

Aspose.Email for Python via .NET 通过以下方式提供对 Gmail 账户的扩展功能 IGmailClient 类。这包括列出、发送、追加、获取和删除邮件,以及管理 Gmail 过滤器。

在使用之前 IGmailClient,使用您的 client ID、client secret、refresh token 和电子邮件地址进行身份验证。然后创建 Gmail 客户端实例:

from aspose.email.clients.google import GmailClient

client = GmailClient.get_instance(client_id, client_secret, refresh_token, email)

使用 Gmail API 发送电子邮件

要通过 Gmail 账户发送带附件的电子邮件,请使用 send_message 方法的 IGmailClient 类。

下面的代码示例演示了如何使用 Gmail API 创建并发送带附件的电子邮件:

from aspose.email import MailMessage, Attachment

# Create the message
message = MailMessage("sender@example.com", "recipient@example.com", "Weekly Report", "Attached is the weekly report.")

# Add an attachment
attachment_path = "path/to/report.pdf"
message.attachments.add(Attachment(attachment_path))

# Send the message
message_id = client.send_message(message)
print(f"Message with attachment sent! ID: {message_id}")

将邮件追加到 Gmail 文件夹

要直接向 Gmail 邮箱添加邮件,绕过标准分类,请使用以下方法:

下面的代码示例演示了如何使用 Gmail 客户端创建电子邮件并使用特定标签将其追加到收件人的收件箱中:

message = MailMessage("sender@example.com", "recipient@example.com", "Subject for inbox message", "Body of the message")

# Append the message to the inbox with a label
message_id = client.append_message(message, "INBOX")
print(f"Message appended to the Inbox. ID: {message_id}")

列出 Gmail 邮件

您可以使用以下方式检索 Gmail 邮箱中所有邮件的列表 list_messages() 方法的 IGmailClient 类。返回列表中的每个项目都是一个 GmailMessageInfo 包含轻量元数据(如 ‘id’’thread_id’)的对象。

下面的代码示例演示了如何检索并显示用户收件箱中所有 Gmail 邮件的信息:

# List all Gmail messages
messages = client.list_messages()

# Print basic info for each message
for i, msg_info in enumerate(messages):
    print(f"Message {i + 1}: ID = {msg_info.id}, Thread ID = {msg_info.thread_id}")

获取 Gmail 邮件内容

检索邮件元数据后,使用 fetch_message(message_id) 以…的形式下载特定邮件的完整内容 MailMessage 对象。这允许访问主题、正文、附件以及其他邮件详情。

下面的代码示例演示了如何获取并显示用户收件箱中前三封 Gmail 邮件的内容:

# Fetch and display content for the first 3 messages
for i in range(min(3, len(messages))):
    message = client.fetch_message(messages[i].id)
    print(f"Message {i + 1}")
    print("Subject:", message.subject)
    print("Body:", message.body)

删除 Gmail 邮件

使用 delete_message(message_id, move_to_trash) 方法的 IGmailClient 用于删除消息的类。您可以将其移动到垃圾箱(非永久)或通过省略第二个参数或传入 False 来立即永久删除。

以下代码示例演示如何将列表中的前三条消息(如电子邮件)移动到垃圾箱:

# Move the first 3 messages to trash
for i in range(min(3, len(messages))):
    client.delete_message(messages[i].id, True)  # True = move to trash
    print(f"Message {i + 1} moved to trash.")

管理 Gmail 过滤器

Aspose.Email for Python 提供以下方法: IGmailClient 用于自动化设置过滤器的类,可根据主题行组织收件邮件并管理 Google 邮箱中已有的过滤器:

  • list_filters() - 检索应用于邮箱的所有过滤器。
  • create_filter(filter) - 添加具有自定义条件和操作的新过滤器。
  • get_filter(id) - 获取特定过滤器的详细信息。
  • delete_filter(id) - 永久删除过滤器。

创建并列出 Gmail 过滤器

以下代码示例演示如何使用 Aspose.Email 库专为 Google 客户端创建和管理电子邮件过滤器:

from aspose.email.clients.google.filters import Filter, Criteria, Action

# Create a new filter
filter_obj = Filter()
filter_obj.matching_criteria = Criteria()
filter_obj.matching_criteria.subject = "Important"

filter_obj.action = Action()
filter_obj.action.add_label_ids = ["IMPORTANT"]

# Create the filter
filter_id = client.create_filter(filter_obj)
print(f"Filter created! ID: {filter_id}")

# List all filters
filters = client.list_filters()
for f in filters:
    print(f"Filter ID: {f.id}")

删除 Gmail 过滤器

以下代码示例演示如何删除 Gmail 帐户的所有过滤器:

filters = client.list_filters()

# Delete each filter
for f in filters:
    client.delete_filter(f.id)
    print(f"Filter ID: {f.id} deleted.")