Tạo và Quản lý Các tùy chọn Bầu chọn

Microsoft Outlook cho phép người dùng tạo một cuộc thăm dò khi soạn email mới, cho phép họ bao gồm các tùy chọn bầu chọn như Có, Không, Có thể, và hơn thế nữa. Tương tự, Aspose.Email cung cấp chức năng tạo cuộc thăm dò khi tạo tin nhắn Outlook mới một cách lập trình. FollowUpOptions lớp bao gồm thuộc tính voting_buttons, có thể được sử dụng để đặt hoặc lấy các tùy chọn bầu chọn mong muốn.

Trong bài viết này, chúng tôi sẽ khám phá một ví dụ chi tiết về cách tạo một MapiMessage với các tùy chọn bầu chọn để thiết lập cuộc thăm dò một cách hiệu quả.

Tạo Cuộc Thăm Dò bằng MapiMessage

Để tạo một cuộc thăm dò, sử dụng thuộc tính voting_buttons của FollowUpOptions lớp như được minh họa trong đoạn mã mẫu dưới đây:

import aspose.email as ae

eml = ae.MailMessage("from@domain.com", "to@domain.com", "Poll", "Please vote")
msg = ae.mapi.MapiMessage.from_mail_message(eml)

# Set the voting buttons
options = ae.mapi.FollowUpOptions()
options.voting_buttons = "Yes;No;Maybe;Exactly!"
ae.mapi.FollowUpManager.set_options(msg, options)

msg.save("voting_btns.msg")

Đọc Các tùy chọn Bầu chọn từ MapiMessages

Đoạn mã dưới đây cho bạn thấy cách đọc các tùy chọn bầu chọn từ một MapiMessage.

from aspose.email.mapi import MapiMessage, FollowUpManager

message = MapiMessage.load(data_dir + "voting_btns.msg")

# This method is useful when, besides the voting buttons, other parameters
# (a category, for example) have to be read as well
options = FollowUpManager.get_options(message)

# The voting buttons are returned as a single string with a semicolon as a separator
print(options.voting_buttons)

Đọc Chỉ Các Nút Bầu chọn

Để chỉ đọc các nút bầu chọn, hãy xem đoạn mã mẫu dưới đây:

from aspose.email.mapi import MapiMessage, FollowUpManager

message = MapiMessage.load(data_dir + "voting_btns.msg")

buttons = FollowUpManager.get_voting_buttons(message)
for button in buttons:
    print(button)

Thêm Nút Bầu chọn vào Tin nhắn Đã Tồn tại

Đoạn mã sau cho bạn thấy cách thêm nút bầu chọn vào một tin nhắn hiện có:

from aspose.email.mapi import MapiMessage, FollowUpManager

message = MapiMessage.load(data_dir + "message.msg")

FollowUpManager.add_voting_button(message, "Indeed!")

message.save(data_dir + "AddVotingButtonToExistingMessage_out.msg")

Xóa Nút Bầu chọn khỏi Tin nhắn

Đoạn mã dưới đây cho bạn thấy cách xóa một nút bầu chọn khỏi tin nhắn:

from aspose.email.mapi import MapiMessage, FollowUpManager

message = MapiMessage.load(data_dir + "voting_btns.msg")

# Delete a single button
FollowUpManager.remove_voting_button(message, "Exactly!")

# Or delete all the buttons of the message
FollowUpManager.clear_voting_buttons(message)

message.save(data_dir + "MapiMsgWithoutPoll_out.msg")

Đọc Thông tin Kết quả Bầu chọn

Đoạn mã dưới đây cho bạn thấy cách đọc thông tin kết quả bầu chọn:

from aspose.email.mapi import MapiMessage, MapiPropertyTag

msg = MapiMessage.load(data_dir + "voting_btns.msg")

for recipient in msg.recipients:
    print(f"Recipient: {recipient.display_name}")

    # Get the PR_RECIPIENT_AUTORESPONSE_PROP_RESPONSE property
    if recipient.properties.contains_key(MapiPropertyTag.RECIPIENT_AUTORESPONSE_PROP_RESPONSE):
        response = recipient.properties[MapiPropertyTag.RECIPIENT_AUTORESPONSE_PROP_RESPONSE]
        print(f"Response: {response.get_string()}")

    # Get the PR_RECIPIENT_TRACKSTATUS_TIME property
    if recipient.properties.contains_key(MapiPropertyTag.RECIPIENT_TRACKSTATUS_TIME):
        response_time = recipient.properties[MapiPropertyTag.RECIPIENT_TRACKSTATUS_TIME]
        print(f"Response time: {response_time.get_date_time()}")

Lấy Thông Tin Phản Ứng từ MSG

Cái get_reactions phương thức của FollowUpManager lớp cho phép bạn lấy danh sách các phản ứng được để lại trên một tin nhắn MAPI, giúp dễ dàng phân tích mức độ tương tác của người dùng. Mỗi mục trong danh sách trả về là một UserReaction đối tượng mở rộng name, email, type và reaction_date_time các thuộc tính. Kiểu phản ứng là một giá trị của ReactionType enumeration: LIKE, HEART, CELEBRATE, LAUGH, SURPRISED, SAD hoặc UNKNOWN.

Đoạn mã mẫu sau minh họa cách lấy các phản ứng khả dụng cho một tin nhắn cụ thể:

from aspose.email.mapi import MapiMessage, FollowUpManager

# Load the message file
msg = MapiMessage.load("message.msg")

# Retrieve the list of reactions on the message
reactions = FollowUpManager.get_reactions(msg)

# Iterate through each reaction and output the details
for reaction in reactions:
    print(f"User: {reaction.name}, Email: {reaction.email}, "
          f"Reaction: {reaction.type}, Date: {reaction.reaction_date_time}")