[C#] Outlook pobieranie załączników

0

Witam.
Mam problem z pobraniem załącznika z maila. wszystkie dane pobieram (wiadomość, adresatów...) ale mam problem z załącznikami. Otrzymuje błąd Cannot apply indexing with [] to an expression of type 'Outlook.Attachments' . Pisząc kod korzystałem z msdn, więc albo tam jest błąd albo sam nie wiem....
http://msdn.microsoft.com/en-us/library/ms268754.aspx

   for (int i = 1; i < objFolder.Items.Count; i++)
                {

                    objMail = (Outlook.MailItem)oItems.Item(i);
                    sw.WriteLine("*********************************************************************************************");
                    sw.WriteLine(objMail.SentOn);
                    sw.WriteLine(objMail.Subject);
                    sw.WriteLine(objMail.To);
                    sw.WriteLine(objMail.Body);
                    sw.WriteLine("*********************************************************************************************");
                   
                    // (Outlook.Attachment a in objMail.Attachments)
                    Outlook.Attachments aaa = objMail.Attachments;

                    Outlook.Attachment a = objMail.Attachments[i];
                                                             
                }
0

http://msdn.microsoft.com/en-us/library/microsoft.office.interop.outlook.attachments.item.aspx
Tak powinno zadziałać:

Outlook.Attachment a = objMail.Attachments[(object)i];

Możesz też użyć foreach:

foreach(Outlook.Attachment a in objMail.Attachments)
   Console.WriteLine(a);
0

No powstają te same błędy. Zamieszczam cały kod:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;


namespace outlook
{
    class Program
    {
        static void Main(string[] args)
        {
            Program prog = new Program();

            prog.getM();
        }
        private void getM()
        {
            Outlook.Application app = new Outlook.ApplicationClass();
            Outlook.NameSpace NS = app.GetNamespace("MAPI");
            NS.Logon("*******","*********", false, true);
            Outlook.MAPIFolder objFolder = NS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
            Outlook.MailItem objMail;

            Outlook.Items oItems;
            oItems = objFolder.Items;

        //    File.Open(@"C:\Documents and Settings\user\Pulpit\outlookTest.txt",FileMode.Open,FileAccess.Write);

            StreamWriter sw = new StreamWriter(@"C:\Documents and Settings\user\Pulpit\outlookTest.txt");
                for (int i = 1; i < objFolder.Items.Count; i++)
                {

                    objMail = (Outlook.MailItem)oItems.Item(i);
                    sw.WriteLine("*********************************************************************************************");
                    sw.WriteLine(objMail.SentOn);
                    sw.WriteLine(objMail.Subject);
                    sw.WriteLine(objMail.To);
                    sw.WriteLine(objMail.Body);
                    sw.WriteLine("*********************************************************************************************");
                   
                    // (Outlook.Attachment a in objMail.Attachments)
                    Outlook.Attachment a = objMail.Attachments[(object)i];
                   //outlook.Attachments aaa = objMail.Attachments;

                 //   Outlook.Attachment a = objMail.Attachments[i];
                    foreach (Outlook.Attachment a in objMail.Attachments)
                        Console.WriteLine(a);
                                                             
                }


                NS.Logoff();
                objFolder = null;
                objMail = null;
                app = null;


       
        }
    }
}
0

Sądząc po

oItems.Item(i);
to nie indekser tylko metoda. Użyj tak samo:

objMail.Attachments.Item(i);
0

poszlo tak :
Outlook.Attachment a = objMail.Attachments.Item((object)i);

teraz jakoś muszę wydobyć typ załącznika.
PdR

0

a.SaveAsFile(@"C:\Documents and Settings\user\Pulpit" + a.FileName); - zapisuje plik pod odpowiednią nazwą wraz z rozszerzeniem.
PdR

0

Te 2 stringi w NS.Logon to mam nadzieje ze sa nieprawdziwe :DD

0

jakie stringi... ;p

1 użytkowników online, w tym zalogowanych: 0, gości: 1