Tyler Hrutkay

Tyler Hrutkay

  • NA
  • 22
  • 861

Receiving Outlook attachment name into C# datagrid

Sep 11 2017 12:41 PM

0down votefavorite

currently my application is pulling in all unread emails from my Outlook and displaying them in the datagridview. What is causing me a boat load of stress is the error: "System.__ComObject" that is showing up in the "Attachment" column.

All I'm trying to do is get the attachment names from unread Outlook emails into the attachment column.

 Any help would be great, thank you.     

     

 

 

  1.  private void Form1_Load(object sender, EventArgs e)  
  2.     {  
  3.   
  4. try  
  5.         {  
  6.             Outlook._Application app = new Outlook.Application();  
  7.             Outlook.NameSpace ns = app.GetNamespace("MAPI");  
  8.             Outlook.MAPIFolder inbox = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);  
  9.             Outlook.Items UnRead = inbox.Items.Restrict("[Unread]=true");  
  10.   
  11.             ns.SendAndReceive(true);  
  12.   
  13.             dt = new DataTable("Inbox");  
  14.             dt.Columns.Add("From Email", typeof(String));  
  15.             dt.Columns.Add("Date", typeof(String));  
  16.             dt.Columns.Add("Subject", typeof(String));  
  17.             dt.Columns.Add("Body", typeof(String));  
  18.             dt.Columns.Add("Attachments", typeof(String));  
  19.             dt.Columns.Add("Print Body", typeof(String));  
  20.             dt.Columns.Add("File Format", typeof(String));  
  21.             dt.Columns.Add("Selected", typeof(String));  
  22.   
  23.             dataGridView1.DataSource = dt;  
  24.             foreach (Object item in inbox.Items)  
  25.             {  
  26.                 if ((item as Outlook.MailItem) != null && (item as Outlook.MailItem).UnRead == true)  
  27.                 {  
  28.                     Outlook.MailItem item1 = (Outlook.MailItem)item;  
  29.   
  30.         dt.Rows.Add(new Object[] { item1.SenderName, item1.SentOn.ToLongDateString() + "" + item1.SentOn.ToLongTimeString(), item1.Subject,  
  31.         item1.Body, item1.Attachments});  
  32.                 }  
  33.             }  
  34.             MessageBox.Show("Retrieving Unread Email Messages.");  
  35.         }  
  36.         catch (Exception ex)  
  37.         {  
  38.             MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);  
  39.         }  
  40.   
  41.   
  42.     }  


Answers (3)