【问题标题】:C# VSTO Outlook Addin count recipients .CC, .TO, .BCC separatelyC# VSTO Outlook Addin 分别计算收件人 .CC、.TO、.BCC
【发布时间】:2021-03-03 13:39:01
【问题描述】:

在我的 Outlook 插件中,我需要添加创建新电子邮件的条件。 .TO 字段中只能有一个收件人。

不允许将配方添加到字段.CCBCC

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

【问题讨论】:

    标签: c# vsto outlook-addin mailitem


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      工作解决方案:

           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
                  {
                      //
                  }
      

      【讨论】:

        猜你喜欢
        • 2014-09-07
        • 1970-01-01
        • 2018-06-27
        • 1970-01-01
        • 2018-03-25
        • 2021-01-21
        • 2023-04-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多