C# VSTO Outlook 插件分别计算收件人 .CC、.TO、.BCC

C# VSTO Outlook Addin count recipients .CC, .TO, .BCC separately

在我的 Outlook 插件中,我需要添加创建新电子邮件的条件。 .TO.

字段中只能有一个收件人

不允许向字段 .CCBCC 添加 recipinets。

(inspectorMailItem.Recipients.Count != 1) 的条件和 (inspectorMailItem.Attachments.Count == 0) 的条件也适用。

但为什么不能对 (inspectorMailItem.CC.Count != 0) 做同样的事情?

错误Operator '!=' cannot be applied to operands of type 'method group' and 'int'

Microsoft.Office.Interop.Outlook.Application application = new 
    Microsoft.Office.Interop.Outlook.Application();
Inspector inspector = application.ActiveInspector();

        if (inspector.CurrentItem is MailItem inspectorMailItem)
        {
            String Subject = inspectorMailItem.Subject;
            String EmailAddress = inspectorMailItem.To;

            if (inspectorMailItem.Recipients.Count != 1)
            {
                MessageBox.Show("More then one recipient.");
            }

            else if (inspectorMailItem.CC.Count != 0)
            {
               MessageBox.Show("Recipient in CC not allowed.");    
            }

            else if (inspectorMailItem.Bcc.Count != 0)
            {
               MessageBox.Show("Recipient in Bcc not allowed.");    
            }

            else if (inspectorMailItem.Attachments.Count == 0)
            {
                MessageBox.Show("No attachment.");
            }
            
            else
            {
              //
            }

更新

我把count替换成length:

Microsoft.Office.Interop.Outlook.Application application = new 
    Microsoft.Office.Interop.Outlook.Application();
Inspector inspector = application.ActiveInspector();

        if (inspector.CurrentItem is MailItem inspectorMailItem)
        {
            String Subject = inspectorMailItem.Subject;
            String EmailAddress = inspectorMailItem.To;

            if (inspectorMailItem.Recipients.Count != 1)
            {
                MessageBox.Show("More then one recipient.");
            }

            else if (inspectorMailItem.CC.Length != 0)
            {
               MessageBox.Show("Recipient in CC not allowed.");    
            }

            else if (inspectorMailItem.Bcc.Length != 0)
            {
               MessageBox.Show("Recipient in Bcc not allowed.");    
            }

            else if (inspectorMailItem.Attachments.Count == 0)
            {
                MessageBox.Show("No attachment.");
            }
            
            else
            {
              //
            }

有效,但是当 .To 为空白且 .CC 为空白时,只有 BCC 填写有此错误:Object reference not set to an instance of an object.

详细问题是这一行 else if (inspectorMailItem.CC.Length != 0)

工作解决方案:

     Inspector inspector = Globals.ThisAddIn.Application.ActiveInspector();
     MailItem inspectorMailItem = inspector.CurrentItem as MailItem;

     String Subject = inspectorMailItem.Subject;
     String EmailAddress = inspectorMailItem.To;

     if (inspectorMailItem.Recipients.Count != 1)
     {
       MessageBox.Show("More then one recipient.");

                this.Hide();
            }

            else if ((inspectorMailItem.CC != null) || (inspectorMailItem.BCC != null))
            {
                MessageBox.Show("CC or BCC not allowed");

                this.Hide();
            }

            else if (inspectorMailItem.Attachments.Count == 0)
            {
                MessageBox.Show("Missing attachment");

                this.Hide();
            }
            else
            {
                //
            }

遍历 MailItem.Recipients 集合并检查 Recipient.Type 属性 (olTo / olCC / olBCC).