【问题标题】:c# outlook MailItem add/remove recipient of type BCCc# outlook MailItem 添加/删除密件抄送类型的收件人
【发布时间】:2018-09-18 17:49:17
【问题描述】:

我想以编程方式将 C# 收件人添加到现有/正在编写的 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)添加收件人并将其显示在密件抄送邮件字段(2nd sn-p)中? 我想这样做,因为这样我可以遍历所有收件人并在调用特殊条件时删除一些收件人。

问题是当我删除添加的密件抄送收件人时

item.BCC = ""; 

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

【问题讨论】:

  • 看来 Outlook 只提供了 To 选项的列表,而不是 cc 或 bcc。
  • 你可以使用 LIST bccEmails {"test.user"}; item.BCC = String.join() 等

标签: c# outlook mailitem


【解决方案1】:

正如用户 BugFinder 所提到的,由于 UI 将密件抄送收件人添加到 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;
                    }
                }

【讨论】:

    【解决方案2】:

    显然我们都没有抓住重点: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 项显然不是 items[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);
        }
    

    【讨论】:

    • mRecipient.Type = OlMailRecipientType.olBCC -> 与我在问题中发布的相同!
    • 嗯嗯,它为我将它移动到密件抄送......所以..其他东西一定是错的。我做了 m = olApplication.CreateItem(OlItemType.olMailItem);收件人 r = m.Recipients.Add("joe.bloggs"); r.Type = (int)OlMailRecipientType.olBCC; m.显示(真);并使用密件抄送打开邮件窗口...
    • 但是是的,我在你的代码中错过了它,因为它显然没有用。
    • 我认为它确实适用于 CreateItem(MailItem) 对象。在我的场景中,我使用 Inspector 获取当前 MailItem 对象。然后收件人将出现在 TO 字段中..
    • 那你还没有提供一个最小可行的例子......我很难复制它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-08
    • 2014-12-27
    • 1970-01-01
    • 2015-01-23
    • 1970-01-01
    • 2012-01-03
    相关资源
    最近更新 更多