设置Outlook MailItem的加密标志如何?

huangapple go评论89阅读模式
英文:

How to set the encrypted Flag for an Outlook MailItem?

问题

Sure, here is the translation of the code you provided:

我需要能够以编程方式使用Excel VSTO Add-In发送加密的密码电子邮件,因此我编写了这个静态方法来发送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);
    // 检查电子邮件是否应准备使用PGP或PKI加密发送。
    if (PGP)
    {
        eMail.Subject = string.Concat("PGP: ", subject);
    }
    else
    {
        eMail.Subject = subject;
        // 将当前MailItem的安全标志设置为加密。
        // 已尝试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()
{
    // 检查是否有Outlook进程正在运行。
    if (Process.GetProcessesByName("OUTLOOK").Count() > 0)
    {
        // 如果是这样,使用GetActiveObject方法获取进程并将其转换为Application对象。
        outlookApp = Marshal.GetActiveObject("Outlook.Application") as Outlook.Application;
    }
    else
    {
        // 如果没有,则创建Outlook的新实例并登录到默认配置文件。
        outlookApp = new Outlook.Application();
        Outlook.NameSpace session = outlookApp.GetNamespace("MAPI");
        session.Logon("", "", false, true);
    }
}

Regarding your question about the VBA code block:

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

This VBA code is used to set the security flag of an Outlook MailItem to indicate that it should be sent as encrypted. Here's a breakdown of what it does:

  1. Set MailItem = Application.ActiveInspector.CurrentItem: This line retrieves the currently open MailItem in the active Outlook inspector.

  2. prop = CLng(MailItem.PropertyAccessor.GetProperty(PR_SECURITY_FLAGS)): This line retrieves the current value of the PR_SECURITY_FLAGS property for the MailItem and stores it in the variable prop.

  3. ulFlags = ulFlags Or &H1 ' SECFLAG_ENCRYPTED: It updates the ulFlags variable to include the SECFLAG_ENCRYPTED flag, which is represented by &H1.

  4. MailItem.PropertyAccessor.SetProperty PR_SECURITY_FLAGS, ulFlags: This line sets the PR_SECURITY_FLAGS property of the MailItem to the updated ulFlags, indicating that the email should be encrypted.

  5. MailItem.Send: Finally, it sends the MailItem.

As for your question about a reference or list of available MAPI properties and their accepted values, you can refer to Microsoft's official documentation for MAPI properties. Microsoft provides documentation for MAPI properties on their website, which includes information about each property and its accepted values.

英文:

I need to be able to programmatically send encrypted password Emails with an Excel VSTO Add-In so i have written this static method to send Outlook Emails:

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();
		}

i am using this method to get the Outlook Application:

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);
			}
		}

But i cant seem to set the security flag of the MailItem to encrypted. No Exception thrown ! it just that outlook doesn't send the Email as encrypted !

i have this VBA Code which is working fine:

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

can you please explain to me what is this code block doing exactly?

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

Is there a refrence or a List of the available MAPI properties and their accepted values ?

答案1

得分: 0

你必须使用PR_SECURITY_FLAGS属性的DASL名称,而不是属性名称。例如:

private const string PR_SECURITY_FLAGS = "http://schemas.microsoft.com/mapi/proptag/0x6E010003";
eMail.PropertyAccessor.SetProperty(PR_SECURITY_FLAGS, 1);

有关更多信息,请参阅如何在OOM中以编程方式签署或加密消息

英文:

You must use a DASL name of the PR_SECURITY_FLAGS property, not a property name. For example:

private const string PR_SECURITY_FLAGS = "http://schemas.microsoft.com/mapi/proptag/0x6E010003";
eMail.PropertyAccessor.SetProperty(PR_SECURITY_FLAGS, 1);

See How to sign or encrypt a message programmatically from OOM for more information.

huangapple
  • 本文由 发表于 2020年1月3日 22:23:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/59580219.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定