如何在发送电子邮件时获取附件数 C#

How to get the attachment count when sending email C#

老实说,我不太擅长 C#,我正在尝试为我的工作创建一个小应用程序,它需要计算用户要发送的电子邮件中的附件数量。我查看了 MS 文档和其他 Whosebug 的线程,但我仍然找不到适合我的东西。

这是我目前拥有的,我只需要获取电子邮件的附件数:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Windows.Forms;
using Outlook = Microsoft.Office.Interop.Outlook;
using Office = Microsoft.Office.Core;

namespace OutlookAddIn1
{
    public partial class ThisAddIn
    {
        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            Application.ItemSend += new Outlook.ApplicationEvents_11_ItemSendEventHandler(Application_ItemSend);
        }

        void Application_ItemSend(object Item, ref bool Cancel)
        {
            MessageBox.Show("You have" + Application_ItemAttached().ToString() + "item(s) attached in this mail");
            Cancel = true;
        }

        public static int Application_ItemAttached(Outlook.MailItem mailitems)
        {
            var attachments = mailitems.Attachments;
            return attachments.Count;
        }

        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
            // Remarque : Outlook ne déclenche plus cet événement. Si du code
            //    doit s'exécuter à la fermeture d'Outlook (consultez https://go.microsoft.com/fwlink/?LinkId=506785)
        }

        #region Code généré par VSTO

        /// <summary>
        /// Méthode requise pour la prise en charge du concepteur - ne modifiez pas
        /// le contenu de cette méthode avec l'éditeur de code.
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }
        
        #endregion
    }
}

感谢您的帮助!

你的代码真的编译了吗?您已经拥有计算附件的功能,但它需要 MailItem 对象作为参数,并且您没有传递任何内容。改成

MessageBox.Show("You have " + Application_ItemAttached(Item as Outlook.MailItem).ToString() + " item(s) attached in this mail");