none
Receiving Attachments into C# Datagrid from Outlook RRS feed

  • Question

  • Hello, I'm trying to receive the attachments of unread Outlook Email into my data grid. When the attachment loads into the grid I'm getting something that says 

    System._ComObject

    How do I get the name of the attachment in that column?

    Code:

      private void Form1_Load(object sender, EventArgs e)
            {
    
        try
                {
                    Outlook.Application app = new Outlook.Application();
                    Outlook.NameSpace ns = app.GetNamespace("MAPI");
                    Outlook.MAPIFolder inbox = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
                    Outlook.Items UnRead = inbox.Items.Restrict("[Unread]=true");
                   
    
    
                    ns.SendAndReceive(true);
    
                    dt = new DataTable("Inbox");
                    dt.Columns.Add("From Email", typeof(String));
                    dt.Columns.Add("Date", typeof(String));
                    dt.Columns.Add("Subject", typeof(String));
                    dt.Columns.Add("Body", typeof(String));
                    dt.Columns.Add("Attachments", typeof(String));
                    dt.Columns.Add("Print Body", typeof(String));
                    dt.Columns.Add("File Format", typeof(String));
                    dt.Columns.Add("Selected", typeof(String));
    
                    dataGridView1.DataSource = dt;
                    foreach (Object item in inbox.Items)
            
            //for (int i = 0; i < inbox.Items.Count; i++ )
                       
                        {
                            if ((item as Outlook.MailItem) != null && (item as Outlook.MailItem).UnRead == true)
                            {
                                Outlook.MailItem item1 = (Outlook.MailItem)item;
                                { 
    
    }
                                //GetAttachmentsName();
                                
                                dt.Rows.Add(new Object[] { item1.SenderName, item1.SentOn.ToLongDateString() + "" + item1.SentOn.ToLongTimeString(), item1.Subject,
                item1.Body, item1.Attachments});
                            }
                        }
                    MessageBox.Show("Retrieving Unread Email Messages.");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
    
    
            }


    -thanks

    Monday, September 11, 2017 5:08 PM

All replies

  • A MailItem's Attachments property is a collection of the Attachment objects.  See Attachments Object.  The Attachments object contains a Count of the number of Attachment objects in the collection and they can be retrieved through the Attachments.Item method.  Each Attachment Object has a FileName property for a string containing the file name of the attachment.

    • Proposed as answer by Terry Xu - MSFT Tuesday, September 12, 2017 5:19 AM
    Monday, September 11, 2017 5:39 PM
  • Hi Sasqwatch,

    Has your issue been resolved? If not, I suggest you try below code.

                string attachMentName = "";
                foreach (Outlook.Attachment attachment in item1.Attachments)
                {
                    attachMentName += attachment.DisplayName + ";";
                }
    

    Best Regards,

    Edward


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, September 21, 2017 9:08 AM