如何为 Outlook MailItem 设置加密标志?
How to set the encrypted Flag for an Outlook MailItem?
我需要能够使用 Excel VSTO 加载项以编程方式发送加密密码电子邮件,所以我编写了这个静态方法来发送 Outlook 电子邮件:
private static Outlook.Application outlookApp;
private const string PR_SECURITY_FLAGS = "http://schemas.microsoft.com/mapi/proptag/0x6E010003";
public static void SendPasswordEmail(string emailAddress, string password, string subject, bool PGP)
{
GetOutlookApp();
Outlook.MailItem eMail = (Outlook.MailItem)outlookApp.CreateItem(Outlook.OlItemType.olMailItem);
// Check if the Email should be prepared to be sent with PGP or PKI encryption.
if (PGP)
{
eMail.Subject = String.Concat("PGP: ", subject);
}
else
{
eMail.Subject = subject;
// Set the Security Flags of the current MailItem to encrypted.
// already tried 1,2
eMail.PropertyAccessor.SetProperty("PR_SECURITY_FLAGS", 1);
}
String eMailBody = String.Format(@Properties.Resources.PasswordEmailBody, password);
eMail.To = emailAddress;
eMail.BodyFormat = Outlook.OlBodyFormat.olFormatHTML;
eMail.HTMLBody = eMailBody;
eMail.Send();
}
我正在使用此方法获取 Outlook 应用程序:
private static void GetOutlookApp()
{
// Check whether there is an Outlook process running.
if (Process.GetProcessesByName("OUTLOOK").Count() > 0)
{
// If so, use the GetActiveObject method to obtain the process and cast it to an Application object.
outlookApp = Marshal.GetActiveObject("Outlook.Application") as Outlook.Application;
}
else
{
// If not, create a new instance of Outlook and sign in to the default profile.
outlookApp = new Outlook.Application();
Outlook.NameSpace session = outlookApp.GetNamespace("MAPI");
session.Logon("", "", false, true);
}
}
但我似乎无法将 MailItem 的安全标志设置为加密。没有抛出异常!只是 outlook 不发送加密的电子邮件!
我有这个 VBA 代码可以正常工作:
Const PR_SECURITY_FLAGS = "http://schemas.microsoft.com/mapi/proptag/0x6E010003"
Dim prop As Long
Set MailItem = oApp.CreateItem(olMailItem)
MailItem.To = something
MailItem.Subject = something
MailItem.BodyFormat = olFormatPlain
MailItem.Body = something
// set encrypted flag
Set MailItem = Application.ActiveInspector.CurrentItem
prop = CLng(MailItem.PropertyAccessor.GETPROPERTY(PR_SECURITY_FLAGS))
ulFlags = ulFlags Or &H1 ' SECFLAG_ENCRYPTED
MailItem.PropertyAccessor.SetProperty PR_SECURITY_FLAGS, ulFlags
MailItem.SEND
你能给我解释一下这个代码块到底在做什么吗?
Set MailItem = Application.ActiveInspector.CurrentItem
prop = CLng(MailItem.PropertyAccessor.GETPROPERTY(PR_SECURITY_FLAGS))
ulFlags = ulFlags Or &H1 ' SECFLAG_ENCRYPTED
MailItem.PropertyAccessor.SetProperty PR_SECURITY_FLAGS, ulFlags
是否有可用 MAPI 属性及其可接受值的参考或列表?
您必须使用 PR_SECURITY_FLAGS 属性 的 DASL 名称,而不是 属性 名称。例如:
private const string PR_SECURITY_FLAGS = "http://schemas.microsoft.com/mapi/proptag/0x6E010003";
eMail.PropertyAccessor.SetProperty(PR_SECURITY_FLAGS, 1);
有关详细信息,请参阅 How to sign or encrypt a message programmatically from OOM。
我需要能够使用 Excel VSTO 加载项以编程方式发送加密密码电子邮件,所以我编写了这个静态方法来发送 Outlook 电子邮件:
private static Outlook.Application outlookApp;
private const string PR_SECURITY_FLAGS = "http://schemas.microsoft.com/mapi/proptag/0x6E010003";
public static void SendPasswordEmail(string emailAddress, string password, string subject, bool PGP)
{
GetOutlookApp();
Outlook.MailItem eMail = (Outlook.MailItem)outlookApp.CreateItem(Outlook.OlItemType.olMailItem);
// Check if the Email should be prepared to be sent with PGP or PKI encryption.
if (PGP)
{
eMail.Subject = String.Concat("PGP: ", subject);
}
else
{
eMail.Subject = subject;
// Set the Security Flags of the current MailItem to encrypted.
// already tried 1,2
eMail.PropertyAccessor.SetProperty("PR_SECURITY_FLAGS", 1);
}
String eMailBody = String.Format(@Properties.Resources.PasswordEmailBody, password);
eMail.To = emailAddress;
eMail.BodyFormat = Outlook.OlBodyFormat.olFormatHTML;
eMail.HTMLBody = eMailBody;
eMail.Send();
}
我正在使用此方法获取 Outlook 应用程序:
private static void GetOutlookApp()
{
// Check whether there is an Outlook process running.
if (Process.GetProcessesByName("OUTLOOK").Count() > 0)
{
// If so, use the GetActiveObject method to obtain the process and cast it to an Application object.
outlookApp = Marshal.GetActiveObject("Outlook.Application") as Outlook.Application;
}
else
{
// If not, create a new instance of Outlook and sign in to the default profile.
outlookApp = new Outlook.Application();
Outlook.NameSpace session = outlookApp.GetNamespace("MAPI");
session.Logon("", "", false, true);
}
}
但我似乎无法将 MailItem 的安全标志设置为加密。没有抛出异常!只是 outlook 不发送加密的电子邮件!
我有这个 VBA 代码可以正常工作:
Const PR_SECURITY_FLAGS = "http://schemas.microsoft.com/mapi/proptag/0x6E010003"
Dim prop As Long
Set MailItem = oApp.CreateItem(olMailItem)
MailItem.To = something
MailItem.Subject = something
MailItem.BodyFormat = olFormatPlain
MailItem.Body = something
// set encrypted flag
Set MailItem = Application.ActiveInspector.CurrentItem
prop = CLng(MailItem.PropertyAccessor.GETPROPERTY(PR_SECURITY_FLAGS))
ulFlags = ulFlags Or &H1 ' SECFLAG_ENCRYPTED
MailItem.PropertyAccessor.SetProperty PR_SECURITY_FLAGS, ulFlags
MailItem.SEND
你能给我解释一下这个代码块到底在做什么吗?
Set MailItem = Application.ActiveInspector.CurrentItem
prop = CLng(MailItem.PropertyAccessor.GETPROPERTY(PR_SECURITY_FLAGS))
ulFlags = ulFlags Or &H1 ' SECFLAG_ENCRYPTED
MailItem.PropertyAccessor.SetProperty PR_SECURITY_FLAGS, ulFlags
是否有可用 MAPI 属性及其可接受值的参考或列表?
您必须使用 PR_SECURITY_FLAGS 属性 的 DASL 名称,而不是 属性 名称。例如:
private const string PR_SECURITY_FLAGS = "http://schemas.microsoft.com/mapi/proptag/0x6E010003";
eMail.PropertyAccessor.SetProperty(PR_SECURITY_FLAGS, 1);
有关详细信息,请参阅 How to sign or encrypt a message programmatically from OOM。