You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Python发送Outlook邮件 - Azure信息保护标签自动化设置问题

Fix: Automatically Set Azure Information Protection Sensitivity Tags in Outlook via Python Win32com

Hey there! I’ve dealt with this exact frustration before—Outlook popping up that AIP dialog when trying to automate emails is a real workflow killer. The built-in Sensitivity property in Outlook’s object model only handles the legacy sensitivity levels (Normal/Personal/Private/Confidential), not the modern AIP sensitivity tags. Here’s how to fix it using the tag’s GUID:

Step 1: Get Your Sensitivity Tag’s GUID

First, you need to grab the unique GUID associated with the AIP sensitivity tag you want to use. There are a couple easy ways to do this:

  • Outlook VBA Method:

    1. Open Outlook, create a new email, manually apply the target sensitivity tag, then save it as a draft.
    2. Press Alt+F11 to open the VBA Editor, then Ctrl+G to open the Immediate Window.
    3. Paste this code and press Enter:
      For Each prop In Application.ActiveInspector.CurrentItem.PropertyAccessor.GetProperties _
          (Array("http://schemas.microsoft.com/mapi/string/{00020329-0000-0000-C000-000000000046}/MSIP_Label*"))
          Debug.Print prop.Name & ": " & prop.Value
      Next
      
    4. Look for lines starting with MSIP_Label_<GUID>_Id—the <GUID> part is what you need.
  • Azure Portal Method:
    If you have admin access, log into the Azure Portal, navigate to Microsoft Purview Compliance Portal > Information protection > Sensitivity labels, select your tag, and check the "Label ID" field (that’s the GUID).

Step 2: Modify Your Python Code to Set the Tag

Once you have the GUID, use Outlook’s PropertyAccessor to set the required MAPI properties for the AIP tag. Here’s your updated code:

import win32com.client
import datetime

outlook = win32com.client.Dispatch('outlook.application')
mail = outlook.CreateItem(0)

# Basic email setup
mail.To = 'emailaddress@email.com'
mail.Subject = 'Sample Email'
mail.HTMLBody = '<h3>This is HTML Body</h3>'
mail.Body = "This is the normal body"

# Replace this with your sensitivity tag's GUID
SENSITIVITY_TAG_GUID = "YOUR_TAG_GUID_HERE"

# Get the PropertyAccessor for the mail item
prop_accessor = mail.PropertyAccessor

# Define the AIP tag properties (these follow a standard format)
prop_tag_id = f"http://schemas.microsoft.com/mapi/string/{{00020329-0000-0000-C000-000000000046}}/MSIP_Label_{SENSITIVITY_TAG_GUID}_Id"
prop_tag_enabled = f"http://schemas.microsoft.com/mapi/string/{{00020329-0000-0000-C000-000000000046}}/MSIP_Label_{SENSITIVITY_TAG_GUID}_Enabled"
prop_tag_set_date = f"http://schemas.microsoft.com/mapi/string/{{00020329-0000-0000-C000-000000000046}}/MSIP_Label_{SENSITIVITY_TAG_GUID}_SetDate"

# Set the properties to apply the tag
prop_accessor.SetProperty(prop_tag_id, SENSITIVITY_TAG_GUID)
prop_accessor.SetProperty(prop_tag_enabled, "true")
# Set the date the tag was applied (requires a VT_DATE variant)
date_variant = win32com.client.VARIANT(win32com.client.pythoncom.VT_DATE, datetime.datetime.now())
prop_accessor.SetProperty(prop_tag_set_date, date_variant)

# Optional: Keep the legacy sensitivity level if needed (not required for AIP tags)
# mail.Sensitivity = 3  # 3 = Confidential

# Send the email without the AIP dialog popping up
mail.Send()

Key Notes:

  • Permissions: Make sure the Outlook account you’re using has permission to apply the sensitivity tag.
  • Bit Version Match: Ensure your Python installation (32/64-bit) matches your Outlook installation—mismatches cause COM errors.
  • Test First: Before sending, you can replace mail.Send() with mail.Save() to save as a draft, then check if the tag is applied correctly in Outlook.

内容的提问来源于stack exchange,提问作者DaniB

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.27 20:57:35