Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to get the Count of attachment in a particular mail

How to get the Count of attachment in a particular mail

Scheduled Pinned Locked Moved C#
helptutorialworkspace
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Neeraj Kr
    wrote on last edited by
    #1

    Below is the code I am using to get the unread emails, checking their subject and processing accordingly. The problem is that I am getting the exception "System.ArgumentException" which says "Cannot find the method on the object instance" on the line where I am trying to find out the number of attachments in a mail. The line is - iAttachCnt = mailItem.Attachments.Count; // Here is the complete code

    private void WeeklyExpenses_Load(object sender, EventArgs e)
    {
    Outlook.NameSpace oNs;
    Outlook.MAPIFolder oFldr;

            int iAttachCnt;
    
            DirectoryInfo di = new DirectoryInfo(System.Environment.CurrentDirectory.ToString() + "\\\\centralexpenses\\\\" + DateTime.Now.ToString("yyMMdd") + "\\\\");
            if (!di.Exists)
            {
                di.Create();
            }
            string strPath = System.Environment.CurrentDirectory.ToString() + "\\\\centralexpenses\\\\" + DateTime.Now.ToString("yyMMdd") + "\\\\";
            //MessageBox.Show(strPath);
            try
            {
                String sClassComp = "IPM.Note";
                oOutlook = new Outlook.Application();
                
                oNs = oOutlook.GetNamespace("MAPI");
                //Outlook.Recipient rcFieldAudit = default(Outlook.Recipient);
                //rcFieldAudit = oNS.CreateRecipient("Administrator");
                //getting mail folder from inbox
                //oFldr = oNs.GetSharedDefaultFolder(rcFieldAudit, Outlook.OlDefaultFolders.olFolderInbox);
                oFldr = oNs.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
                Outlook.Items newMailItems = null;
                Outlook.MailItem mailItem = null;
                Outlook.Attachments atmt = null;
                newMailItems = oFldr.Items.Restrict("\[unread\] = true");
                // loop over the new mails
                   
                foreach (object item in newMailItems)
                {
                    if (((Outlook.MailItem)item).MessageClass == sClassComp)
                    {
                        // cast from object to Outlook.MailItem
                        mailItem = (Outlook.MailItem)item;
                        if (mailItem.SenderEmailType == "EX")
                        {
                            string emailString = X4UMapi.GetMessageSenderAddress(mailItem.MAPIOBJECT).ToString();
                            int lastindex = emailString.LastIndexOf("=") + 1;
                            empEmail = emailStri
    
    W 1 Reply Last reply
    0
    • N Neeraj Kr

      Below is the code I am using to get the unread emails, checking their subject and processing accordingly. The problem is that I am getting the exception "System.ArgumentException" which says "Cannot find the method on the object instance" on the line where I am trying to find out the number of attachments in a mail. The line is - iAttachCnt = mailItem.Attachments.Count; // Here is the complete code

      private void WeeklyExpenses_Load(object sender, EventArgs e)
      {
      Outlook.NameSpace oNs;
      Outlook.MAPIFolder oFldr;

              int iAttachCnt;
      
              DirectoryInfo di = new DirectoryInfo(System.Environment.CurrentDirectory.ToString() + "\\\\centralexpenses\\\\" + DateTime.Now.ToString("yyMMdd") + "\\\\");
              if (!di.Exists)
              {
                  di.Create();
              }
              string strPath = System.Environment.CurrentDirectory.ToString() + "\\\\centralexpenses\\\\" + DateTime.Now.ToString("yyMMdd") + "\\\\";
              //MessageBox.Show(strPath);
              try
              {
                  String sClassComp = "IPM.Note";
                  oOutlook = new Outlook.Application();
                  
                  oNs = oOutlook.GetNamespace("MAPI");
                  //Outlook.Recipient rcFieldAudit = default(Outlook.Recipient);
                  //rcFieldAudit = oNS.CreateRecipient("Administrator");
                  //getting mail folder from inbox
                  //oFldr = oNs.GetSharedDefaultFolder(rcFieldAudit, Outlook.OlDefaultFolders.olFolderInbox);
                  oFldr = oNs.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
                  Outlook.Items newMailItems = null;
                  Outlook.MailItem mailItem = null;
                  Outlook.Attachments atmt = null;
                  newMailItems = oFldr.Items.Restrict("\[unread\] = true");
                  // loop over the new mails
                     
                  foreach (object item in newMailItems)
                  {
                      if (((Outlook.MailItem)item).MessageClass == sClassComp)
                      {
                          // cast from object to Outlook.MailItem
                          mailItem = (Outlook.MailItem)item;
                          if (mailItem.SenderEmailType == "EX")
                          {
                              string emailString = X4UMapi.GetMessageSenderAddress(mailItem.MAPIOBJECT).ToString();
                              int lastindex = emailString.LastIndexOf("=") + 1;
                              empEmail = emailStri
      
      W Offline
      W Offline
      Wendelius
      wrote on last edited by
      #2

      Not sure about this but what happens if you cast the mailItem to primary interface (_MailItem[^]):

      iAttachCnt = ((_MailItem)mailItem).Attachments.Count;

      The need to optimize rises from a bad design. My articles[^]

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups