locked
How To save attachments? Outlook! C# RRS feed

  • Question

  • Look my code... im save a menssages... but i dont save attachments =/

    _____________________________________________
     static void Main(string[] args)
            {
                try
                {
                    //Criando a aplicação
                    Outlook.Application oApp = new Outlook.Application();

                    //Pegando o namespace MAPI
                    Outlook.NameSpace oNS = oApp.GetNamespace("mapi");

                    //Logando usando o profile default
                    oNS.Logon(Missing.Value, Missing.Value, false, true);

                    //Pegando o Folder
                    Outlook.MAPIFolder oInbox = oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);

                    //Pegando os itens do inbox
                    Outlook.Items oItems = oInbox.Items;

                    // Pegado a primeira menssagem
                    Outlook.MailItem oMsg = (Outlook.MailItem)oItems.GetLast();

                    //Retirando os Acentos...
                    string subject = oMsg.Subject.ToString().Replace(':', '_')
                                                            .Replace('*', '_')
                                                            .Replace('/', '_')
                                                            .Replace('?', '_')
                                                            .Replace('"', '_')
                                                            .Replace('<', '_')
                                                            .Replace('>', '_')
                                                            .Replace('|', '_');
                    //Propriedade
                    Directory.CreateDirectory("C:\\teste\\");
                    StreamWriter oFile = new StreamWriter("C:\\teste\\" + subject + ".txt");
                    oFile.WriteLine("Assunto: "+oMsg.Subject);
                    oFile.WriteLine("Enviado por: "+oMsg.SenderName);
                    oFile.WriteLine("Data Recebida: "+oMsg.ReceivedTime);
                    oFile.WriteLine(oMsg.Body);
                    oFile.Close();

                    //Checando c tem atacchments
                    int AttachCnt = oMsg.Attachments.Count;
                    Console.WriteLine("Attachments: " + AttachCnt.ToString());

                    if (AttachCnt > 0)
                    {
                        for (int i = 1; i <= AttachCnt; i++)
                            Console.WriteLine(i.ToString() + "-" + oMsg.AttachmentsIdea.DisplayName);
                    }

                    //Deslogando.
                    oNS.Logoff();

                    //Release dos Objetos.
                    oMsg = null;
                    oItems = null;
                    oInbox = null;
                    oNS = null;
                    oApp = null;
                }

                //Caso de Algum erro
                catch (Exception e)
                {
                    Console.WriteLine("{0} Exception caught: ", e);
                }
            }
    ___________________________________________

    Please Help me to save attachments!
    Thank's
    Tuesday, October 17, 2006 7:32 PM