从Outlook接收附件到C#Datagrid [英] Receiving Attachments into C# Datagrid from Outlook

查看:73
本文介绍了从Outlook接收附件到C#Datagrid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在尝试将未读Outlook电子邮件的附件接收到我的数据网格中。当附件加载到网格中时,我得到的内容是 

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?

代码:

  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

推荐答案

MailItem的附件属性是附件对象的集合。 请参阅

附件对象
。  Attachments对象包含集合中Attachment对象数量的Count,可以通过Attachments.Item方法检索它们。 每个

附件对象
都有一个FileName属性包含附件文件名的字符串。

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.


这篇关于从Outlook接收附件到C#Datagrid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆