如何通过win32com实现Outlook自动化邮件的密级类型指定?
Absolutely! You can set your company's required email type (Public, General, Secret, etc.) using win32com with Outlook—here’s how to do it, depending on whether your company uses Outlook’s built-in sensitivity settings or a custom field.
1. Using Outlook's Built-in Sensitivity Property
Outlook has a native Sensitivity property that maps directly to standard sensitivity levels like Confidential/Secret, Normal/General, etc. You can set this property directly via win32com without extra work.
First, here are the constant values (you can use either the numeric value or the named constant from win32.constants for readability):
olNormal(0): General/Public (default setting)olPersonal(1): PersonalolPrivate(2): PrivateolConfidential(3): Secret/Confidential
Add this line right after setting the email body in your script:
# Set to Secret/Confidential - swap the constant to match your company's required type mail.Sensitivity = win32.constants.olConfidential
2. Handling Custom Company-Specific Email Type Fields
If your company uses a custom user-defined field (like a dropdown labeled "Email Type" with options Public/General/Secret), you’ll need to interact with Outlook’s UserProperties collection to set this value.
Here’s how to add or update that custom field:
# Replace with the exact name of your company's custom field custom_field_name = "Email Type" # Set to your desired type: Public/General/Secret desired_type = "Secret" # Check if the custom field already exists on the email field_found = False for prop in mail.UserProperties: if prop.Name == custom_field_name: prop.Value = desired_type field_found = True break # If the field doesn't exist, create it (adjust olText if your field uses a different type like olDropdown) if not field_found: custom_prop = mail.UserProperties.Add(custom_field_name, win32.constants.olText) custom_prop.Value = desired_type
Fixed & Updated Full Script
I also fixed a couple small typos in your original code (like tkinter as to → tkinter as tk and misspelled "attachment"):
import win32com.client as win32 import easygui import tkinter as tk from tkinter import filedialog ### easygui commands asking user for the inputs fieldNames_text= ['',''] fieldNames= ['Subject','Link'] INPUT = easygui.multenterbox('Please fill in', 'FIN and PASSWORD', fieldNames, fieldNames_text) answer = easygui.ynbox('Any attachment?', 'Attachment', ('Yes', 'No')) ### outlook handling outlook = win32.Dispatch('outlook.application') mail = outlook.CreateItem(0) mail.To = 'name.surname@company.com' mail.Subject = INPUT[0] mail.Body = INPUT[1] # --- Set email type/sensitivity --- # Option 1: Use built-in sensitivity (uncomment below) mail.Sensitivity = win32.constants.olConfidential # Secret/Confidential # Option 2: Use custom company field (uncomment below and adjust values) # custom_field_name = "Email Type" # desired_type = "Secret" # field_found = False # for prop in mail.UserProperties: # if prop.Name == custom_field_name: # prop.Value = desired_type # field_found = True # break # if not field_found: # custom_prop = mail.UserProperties.Add(custom_field_name, win32.constants.olText) # custom_prop.Value = desired_type # --- End email type setting --- ### attachment addition if answer == True: root = tk.Tk() root.withdraw() file_path = filedialog.askopenfilename() mail.Attachments.Add(file_path) else: pass ### mail sending mail.Send()
Just uncomment the option that matches your company’s setup, adjust the values to fit your needs, and you’re ready to go!
内容的提问来源于stack exchange,提问作者akkab

