c#outlook MailItem添加/删除BCC类型的收件人

时间:2018-04-09 10:43:29

标签: c# outlook mailitem

我想以编程方式将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字段中。

enter image description here

当我做这样的事情时:

item.BCC = "test.user";

看起来很正确...

enter image description here

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

问题是当我用

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

在BCC字段中删除了所有的recpients。

2 个答案:

答案 0 :(得分:1)

显然我们都错过了这一点: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应该做的伎俩

以下内容 - 在bcc中打开了一个包含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);
    }

答案 1 :(得分:0)

正如用户BugFinder所提到的,由于UI将BCC收件人添加到olTO字段,因此需要一种解决方法。我首先添加一个假人解决了这个问题,然后添加了BCC并移除了假人:

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