How to get the Count of attachment in a particular mail
-
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
-
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