Example #1
0
        private void DownloadAttachment()
        {
            try
            {
                string FilePath = @"C:\somefolder\", FileNamewithPath = null, Subj = "ProddowsFolder Test";
                Outlook.Application OApp       = new Outlook.Application();
                Outlook.NameSpace   ONameSpace = OApp.GetNamespace("MAPI");
                //Outlook.MailItem OMail = null;
                Outlook.MAPIFolder InboxFolder, ProddowsFolder, ProddowsLoadedFolder = null;
                ArrayList          MailCollection = new ArrayList();
                ONameSpace.Logon("group/user", null, false, true);
                InboxFolder    = ONameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
                ProddowsFolder = InboxFolder.Folders["Proddows"];

                //for (int j = 0; j <= InboxFolder.Folders.Count; j++ )
                //{
                //    MessageBox.Show("Name: " + InboxFolder.Folders.Parent + " FolderPath: " + InboxFolder.FolderPath);
                //}

                ProddowsLoadedFolder = ProddowsFolder.Folders["Downloaded"];
                Outlook.Items OItem = InboxFolder.Items;

                foreach (Outlook.MailItem item in ProddowsFolder.Items)
                {
                    if (item != null && item.Subject == Subj)
                    {
                        MailCollection.Add(item);
                    }
                }

                foreach (Outlook.MailItem OMItem in MailCollection)
                {
                    foreach (Outlook.Attachment OAttactment in OMItem.Attachments)
                    {
                        FileNamewithPath = FilePath + OAttactment.FileName;
                        OAttactment.SaveAsFile(FileNamewithPath);

                        OMItem.Move(ProddowsLoadedFolder);
                    }
                }
                ONameSpace.Logoff();
                OItem          = null;
                MailCollection = null;
                ONameSpace     = null;
                OApp           = null;
            }
            catch (Exception e)
            {
            }
        }
        private void DownloadAttachment(string MailSub, string MailFrom, string MailTo, string MailCC, string SMTPServer, ref bool ErrorStatus)
        {
            try
            {
                string FilePath                = Dts.Variables["User::FOLDER_DownloadAttachmentLocation"].Value.ToString();
                string FileNamewithPath        = null;
                string Expense_Subj            = Dts.Variables["User::MAIL_ExpenseSubject"].Value.ToString();
                Outlook.Application OApp       = new Outlook.Application();
                Outlook.NameSpace   ONameSpace = OApp.GetNamespace("MAPI");
                //Outlook.MailItem OMail = null;
                Outlook.MAPIFolder InboxFolder, ExpenseFolder, ExpenseLoadedFolder = null;
                ArrayList          MailCollection = new ArrayList();
                ONameSpace.Logon("user/someuser", null, false, true);
                InboxFolder   = ONameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
                ExpenseFolder = InboxFolder.Folders["Project"];

                ExpenseLoadedFolder = ExpenseFolder.Folders["Downloaded"];
                Outlook.Items OItem = InboxFolder.Items;

                if (ExpenseFolder.Items.Count == 0)
                {
                    string MailBody;
                    MailSub   = "Expense - Couldn't find email in mailbox";
                    MailBody  = ("<html><div style=font-family:Calibri;font-size:11.0pt;color:#000000>");
                    MailBody += "Hello Team </BR></BR>";
                    MailBody += "There were no Expense email in the Mailbox to download the required .ZIP attachments</BR></BR>";
                    MailBody += "Thank you,</BR>";
                    MailBody += "YourReports";

                    SendMail(MailFrom, MailTo, MailCC, MailSub, SMTPServer, MailBody);
                    Dts.TaskResult = (int)ScriptResults.Failure;
                }
                else
                {
                    foreach (Outlook.MailItem item in ExpenseFolder.Items)
                    {
                        if (item != null && (item.Subject.Contains(Expense_Subj)))
                        {
                            MailCollection.Add(item);
                        }
                    }

                    foreach (Outlook.MailItem OMItem in MailCollection)
                    {
                        foreach (Outlook.Attachment OAttactment in OMItem.Attachments)
                        {
                            if (OAttactment.FileName.ToString().EndsWith(".zip"))
                            {
                                FileNamewithPath = FilePath + OAttactment.FileName;
                                OAttactment.SaveAsFile(FileNamewithPath);
                            }
                        }
                        OMItem.Move(ExpenseLoadedFolder);
                    }
                    ONameSpace.Logoff();
                    OItem          = null;
                    MailCollection = null;
                    ONameSpace     = null;
                    OApp           = null;
                }
            }
            catch (Exception ex)
            {
                string MailBody;
                MailSub   = "Expense - Download attachment component failed";
                MailBody  = "Hello Team </BR></BR>";
                MailBody += string.Format("Download attachment component failed due to:</BR><b>Error message:</b>{0}</BR></BR>", ex.Message);
                MailBody += "Thank you,</BR>";
                MailBody += "YourReports";

                SendMail(MailFrom, MailTo, MailCC, MailSub, SMTPServer, MailBody);
                UpdateErrorStatus(ref ErrorStatus);
            }
        }