c# outlook MailItem add/remove BCC 类型的收件人

c# outlook MailItem add/remove recipient of type BCC

我想以编程方式将 C# 收件人添加到 existing/composing MailItem。当我这样添加收件人时:

Microsoft.Office.Interop.Outlook.Inspector inspector = Globals.ThisAddIn.Application.ActiveInspector();
Microsoft.Office.Interop.Outlook.MailItem item = inspector.CurrentItem as Microsoft.Office.Interop.Outlook.MailItem;
Microsoft.Office.Interop.Outlook.Recipient mRecipient = item.Recipients.Add("test.user");

mRecipient.Type = (int)Microsoft.Office.Interop.Outlook.OlMailRecipientType.olBCC;

它出现在 MailItem 的 TO 字段中。

当我做这样的事情时:

item.BCC = "test.user";

看起来正确...

有没有办法使用第一种方法 (Type.olBCC) 添加收件人并将其显示在 BCC 邮件字段中(第二个片段)? 我想这样做是因为这样我就可以遍历所有收件人并在调用特殊条件时删除一些收件人。

问题是当我使用

删除添加的 BCC 收件人时
item.BCC = ""; 

密件抄送字段中的所有收件人都已删除。

显然我们都忽略了这一点:https://msdn.microsoft.com/en-us/vba/outlook-vba/articles/recipients-object-outlook

据此

Microsoft.Office.Interop.Outlook.Recipient mRecipient = item.Recipients.Add("test.user");
mRecipient..Type = olCC   // at a guess here olBCC would be BCC...

我们可以将列表修改为列表.. :)

注意:我在我的列表中找不到 olCC.. 但也许我需要更仔细地寻找。

注2!找到了。

OlMailRecipientType。有 olTo、olOriginator、olCC 和 olBCC

给你。所以 mRecipient.Type = OlMailRecipientType.olBCC 应该可以解决问题

以下 - 在密件抄送中使用 joe.bloggs 打开了一个新邮件项目:

    olApplication = new Microsoft.Office.Interop.Outlook.Application();
    m = olApplication.CreateItem(OlItemType.olMailItem);
    Recipient r = m.Recipients.Add("joe.bloggs");
    r.Type = (int)OlMailRecipientType.olBCC;
    m.Display(true);

保存草稿并想出如何将其作为 1 项保留下来显然不是项目[0] ..

以下也适用:

    Microsoft.Office.Interop.Outlook.MAPIFolder folderDrafts = ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderDrafts);
    if (folderDrafts.Items.Count > 0)
    {
        m = (Microsoft.Office.Interop.Outlook.MailItem)folderDrafts.Items[1];
        Recipient r = m.Recipients.Add("joe.bloggs");
        r.Type = (int)OlMailRecipientType.olBCC;
        m.Display(true);
    }

正如 BugFinder 用户提到的那样,由于 UI 将 BCC 收件人添加到 olTO 字段,因此需要一个解决方法。我通过首先添加一个虚拟对象解决了这个问题,然后添加密件抄送并删除虚拟对象:

Microsoft.Office.Interop.Outlook.Inspector inspector = Globals.ThisAddIn.Application.ActiveInspector();
Microsoft.Office.Interop.Outlook.MailItem item = inspector.CurrentItem as Microsoft.Office.Interop.Outlook.MailItem;

//Create dummy recipient object for UI bug
 Microsoft.Office.Interop.Outlook.Recipient dummy = 
 item.Recipients.Add("bugDummy");
 dummy.Type = (int)Microsoft.Office.Interop.Outlook.OlMailRecipientType.olBCC;
 //Create the BCC object that will be used
 Microsoft.Office.Interop.Outlook.Recipient mRecipient = item.Recipients.Add("test@user.de");
 mRecipient.Type = (int)Microsoft.Office.Interop.Outlook.OlMailRecipientType.olBCC;
//iterate through the recipients and delete the dummy
foreach (Microsoft.Office.Interop.Outlook.Recipient recipient in item.Recipients)
                {
                    if (string.Compare(recipient.Name, "bugDummy", true) == 0)
                    {
                        recipient.Delete();
                        break;
                    }
                }