示例#1
0
        private void StartRun()
        {
            string _Subject  = GetSubject();
            string _strPath  = GetAttachpath();
            string _strPromt = "Обробка закінчена";

            Outlook.Application objOutlook   = new Outlook.Application();
            Outlook.Explorer    _actExplorer = objOutlook.Application.ActiveExplorer();

            Outlook.Stores stores = objOutlook.Application.Session.Stores;

            if (_actExplorer == null)
            {
                ToolStripStatusLabel1.Text = "OutLook not running!";
                return;
            }



            Outlook.Store storework = null;
            for (int i = 1; i <= stores.Count; i++)
            {
                if (stores[i].DisplayName == GetStore())
                {
                    storework = stores[i];
                    break;
                }
            }


            if (storework == null)
            {
                ToolStripStatusLabel1.Text = "OutLook store:" + GetStore() + "not found!";
                return;
            }


            Outlook.MAPIFolder inBox         = storework.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
            Outlook.Items      inBoxItems    = inBox.Items; //Outlook.MAPIFolder inBox
            Outlook.MAPIFolder OutBoxSubject = null;

            foreach (Outlook.Folder folder in inBox.Folders)
            {
                if (folder.Name.Equals(_Subject))
                {
                    OutBoxSubject = folder;
                }
            }

            if (OutBoxSubject == null)
            {
                OutBoxSubject = inBox.Folders.Add(_Subject);
            }


            ToolStripStatusLabel1.Text = "Вхідних : " + inBoxItems.Count;
            //inBoxItems = inBoxItems.Restrict( " [Unread] = true");
            inBoxItems = inBoxItems.Restrict("[Subject] = " + _Subject + "");

            ToolStripStatusLabel2.Visible    = true;
            ToolStripStatusLabel3.Visible    = true;
            ToolStripProgressBarMain.Visible = true;
            ToolStripProgressBarMain.Maximum = inBoxItems.Count;
            ToolStripProgressBarMain.Value   = 0;

            try
            {
                // BASIC CICLE

                SetRunStage(true);

                int _allattachment = 0, _allmail = 1;

                for (int i = inBoxItems.Count; i > 0; i--)

                {
                    Application.DoEvents();
                    Outlook.MailItem workLetter = inBoxItems[i] as Outlook.MailItem;

                    ToolStripStatusLabel3.Text = "Обробка листа: " + _allmail++;
                    ToolStripProgressBarMain.Value++;
                    Application.DoEvents();


                    if (!GetRunStage())
                    {
                        _strPromt = "Обробка перервана";

                        break;
                    }

                    Application.DoEvents();

                    if (workLetter.Subject == _Subject)

                    {
                        if (workLetter.Attachments.Count > 0)
                        {
                            string _folder = "";

                            _allattachment++;

                            for (int j = 1; j <= workLetter.Attachments.Count; j++)
                            {
                                _folder = MakeFolderForAttacment(workLetter.Attachments[j].FileName);
                                if (!Directory.Exists(_folder))
                                {
                                    Directory.CreateDirectory(_folder);
                                }
                                workLetter.Attachments[j].SaveAsFile(Path.Combine(_folder + "\\" + workLetter.Attachments[j].FileName));
                                ToolStripStatusLabel2.Text = "Оброблено вкладень: " + _allattachment;
                            }
                            workLetter.Move(OutBoxSubject);
                        }

                        else
                        {
                            workLetter.Delete();
                        }
                    }
                }
            }// end try


            catch (Exception ex)
            {
                string errorInfo = (string)ex.Message
                                   .Substring(0, 11);
                if (errorInfo == "Cannot save")
                {
                    MessageBox.Show(@"Bad folder to a save attacment");
                }
                MessageBox.Show(ex.Message);
                _strPromt = "OutLook was lost!";
            }

            // finally
            {
                EndRun(_strPromt);
                inBoxItems = null;
                objOutlook = null;
            }
        }
        public void OnConvertButton(Office.IRibbonControl control)
        {
            StagingTicket ticket = new StagingTicket();

            Outlook.Application application = new Outlook.Application();
            Outlook.NameSpace   ns          = application.GetNamespace("MAPI");

            //get selected outlook object / mail item
            Object selectedObject = application.ActiveExplorer().Selection[1];

            Outlook.MailItem selectedMail = (Outlook.MailItem)selectedObject;

            //in case a email object si selected run the workflow
            if (selectedObject != null)
            {
                //1. invoke web service
                try
                {
                    //instanciate service and invoke it create the ticket and to receive the ticket reference (ID)
                    Remedy2OutlookService service = new Remedy2OutlookService();
                    ticket = service.invoke(selectedMail);
                }
                catch (Exception ex)
                {
                    string path = AppDomain.CurrentDomain.BaseDirectory + @"\temp";
                    if (!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }

                    using (System.IO.StreamWriter file = new System.IO.StreamWriter(path + @"\errors.log", true))
                    {
                        file.WriteLine(DateTime.Now.ToString() + " - " + ex.Message + "\n" + ex.ToString());
                    }

                    // display an error message
                    MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }


                //2. open submitted ticket in browser or display info/warn/error messages
                if (Properties.Settings.Default.OpenInBrowser && ticket.wid != null)
                {
                    dynamic ie  = Activator.CreateInstance(Type.GetTypeFromProgID("InternetExplorer.Application"));
                    string  url = Properties.Settings.Default.AppConsoleURL;

                    if (url.EndsWith("/"))
                    {
                        url.Remove(url.Length - 1);
                    }
                    if (url.IndexOf("?", 0) > 0)
                    {
                        url += "&eid=" + ticket.wid;
                    }
                    else
                    {
                        url += "?eid=" + ticket.wid;
                    }

                    ie.AddressBar = false;
                    ie.MenuBar    = false;
                    ie.ToolBar    = false;
                    ie.Visible    = true;
                    ie.Navigate2(url);
                }
                else
                {
                    if (ticket.wid != null && String.Equals(ticket.sts, "done"))
                    {
                        MessageBox.Show("Remedy workflow ticket has been created: " + ticket.wid, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else if (ticket.rid != null && String.Equals(ticket.sts, "open"))
                    {
                        MessageBox.Show("Selected email is transferred to Remedy into the staging record [" + ticket.rid + "] but the fulfillment ticket was not created due to an error or misconfiguration.\n\nContact your Administrator!", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else if (String.Equals(ticket.sts, "error") && ticket.log != null)
                    {
                        MessageBox.Show("Remedy workflow error: " + ticket.log + ".\n\nContact your Administrator!", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }


                //3. mark selected email as read
                if (Properties.Settings.Default.ReadMail && ticket.wid != null)
                {
                    selectedMail.UnRead = false;
                    selectedMail.Save();
                }


                //4. insert reference ticket number in the
                if (Properties.Settings.Default.InsertInSubject && ticket.wid != null)
                {
                    selectedMail.Subject = ticket.wid + ": " + selectedMail.Subject;
                    selectedMail.Save();
                }

                //5. copy selected email item into Backup MAPI folder (if not exist will be created)
                if (Properties.Settings.Default.BackupMail && ticket.wid != null)
                {
                    Outlook.Folder     inbox  = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox) as Outlook.Folder;
                    Outlook.MAPIFolder backup = null;

                    foreach (Outlook.MAPIFolder subfolder in inbox.Folders)
                    {
                        if (String.Equals(subfolder.Name, "Backup"))
                        {
                            backup = subfolder;
                            break;
                        }
                    }

                    if (backup == null)
                    {
                        try
                        {
                            backup = inbox.Folders.Add("Backup", Outlook.OlDefaultFolders.olFolderInbox);
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("Error trying to create Backup MAPI folder: " + ex.Message + ".\n\nContact your Administrator or create it manually!", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }

                    if (backup != null)
                    {
                        Outlook.MailItem copyMail = selectedMail.Copy();
                        copyMail.Move(backup);
                    }

                    if (inbox != null)
                    {
                        Marshal.ReleaseComObject(inbox);
                    }
                    if (backup != null)
                    {
                        Marshal.ReleaseComObject(backup);
                    }
                }

                //6. delete selected email item
                if (Properties.Settings.Default.RemoveMail && ticket.wid != null)
                {
                    selectedMail.Delete();
                }
            }
        }
示例#3
0
        private static void ProcessEmail(Outlook.MailItem email)
        {
            bool ok = true;

            try
            {
                if (email.Sender is null)
                {
                    ok = false;
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex);
                ok = false;
            }

            InTouchContact mailContact = null;

            Outlook.ContactItem contact = null;

            try
            {
                contact = InTouch.Contacts.FindContactFromEmailAddress(email.Sender.Address);
            }
            catch (InvalidComObjectException ex)
            {
                Log.Error(ex);
            }

            if (contact is object)
            {
                mailContact = new InTouchContact(contact);
            }
            else
            {
                ok = false;
            }
            if (mailContact is null)
            {
                ok = false;
            }

            if (ok)
            {
                //If unread the process delivery option else process read option.
                if (email.UnRead)
                {
                    switch (mailContact.DeliveryAction)
                    {
                    case EmailAction.None:     //Don't do anything to the email.
                        Log.Message("Move Email : Delivery Action set to None. " + email.Sender.Address);
                        break;

                    case EmailAction.Delete:     //Delete the email if it is passed its action date.
                        Log.Message("Move Email : Deleting email from " + email.Sender.Address);
                        email.Delete();
                        break;

                    case EmailAction.Move:     //Move the email if its passed its action date.
                        Log.Message("Move Email : Moving email from " + email.Sender.Address);
                        MoveEmailToFolder(mailContact.InboxPath, email);
                        break;

                    case EmailAction.Junk:
                        Log.Message("Move Email to Junk: Moving email from " + email.Sender.Address);
                        email.Move(Globals.ThisAddIn.Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderJunk));
                        break;
                    }
                }
                else
                {
                    switch (mailContact.ReadAction)
                    {
                    case EmailAction.None:     //Don't do anything to the email.
                        Log.Message("Move Email : Read Action set to None. " + email.Sender.Address);
                        break;

                    case EmailAction.Delete:     //Delete the email.
                        Log.Message("Move Email : Deleting email from " + email.Sender.Address);
                        email.Delete();
                        break;

                    case EmailAction.Move:     //Move the email.
                        Log.Message("Move Email : Moving email from " + email.Sender.Address);
                        MoveEmailToFolder(mailContact.InboxPath, email);
                        break;

                    case EmailAction.Junk:
                        Log.Message("Move Email to Junk: Moving email from " + email.Sender.Address);
                        email.Move(Globals.ThisAddIn.Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderJunk));
                        break;
                    }
                }

                mailContact.SaveAndDispose();
            }
            else
            {
                //Get the 'On Behalf' property from the email.
                string onBehalfEmailAddress;

                try
                {
                    Outlook.PropertyAccessor mapiPropertyAccessor;
                    string propertyName = "http://schemas.microsoft.com/mapi/proptag/0x0065001F";
                    mapiPropertyAccessor = email.PropertyAccessor;
                    onBehalfEmailAddress = mapiPropertyAccessor.GetProperty(propertyName).ToString();
                    if (mapiPropertyAccessor is object)
                    {
                        Marshal.ReleaseComObject(mapiPropertyAccessor);
                    }
                }
                catch (Exception ex)
                {
                    Log.Error(ex);
                }

                try
                {
                    if (email is object)
                    {
                        if (email.SenderEmailAddress is object)
                        {
                            Log.Message("Move Email : No Contact for " + email.SenderEmailAddress);
                        }
                        else
                        {
                            Log.Message("Move Email : No Contact (detatched object?)");
                        }
                    }
                    else
                    {
                        Log.Message("Move Email : No email object");
                    }
                    //Op.LogMessage("SenderName         : " + email.SenderName);
                    //Op.LogMessage("SentOnBehalfOfName : " + email.SentOnBehalfOfName);
                    //Op.LogMessage("ReplyRecipientNames: " + email.ReplyRecipientNames);
                    //Op.LogMessage("On Behalf: " + onBehalfEmailAddress);
                    //Op.LogMessage("");
                }
                catch (Exception ex)
                {
                    Log.Error(ex);
                }

                //Log the details.
            }

            if (email is object)
            {
                Marshal.ReleaseComObject(email);
            }
            if (contact is object)
            {
                Marshal.ReleaseComObject(contact);
            }
        }
示例#4
0
        void Inspectors_NewInspector(Microsoft.Office.Interop.Outlook.Inspector Inspector)
        {
            Outlook.MailItem mailItem = Inspector.CurrentItem as Outlook.MailItem;

            try
            {
                if ((mailItem == null) || (mailItem.EntryID == null))
                {
                    return;
                }

                Outlook.Folder parentFolder = mailItem.Parent as Outlook.Folder;
                if (parentFolder.Name != "Caixa de Entrada")
                {
                    return;
                }

                if (mailItem.Categories == null || !mailItem.Categories.Contains("#"))
                {
                    return;
                }

                if (!this.BuscarPorEmail(getSenderEmailAddress(mailItem)))
                {
                    MessageBox.Show("Email de contato de cliente não econtrado!"
                                    + "\n\nTicket não pode ser cadastrado!", "Ticket.OutlookAddIn", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                if (MessageBox.Show("Contato: " + emailSender
                                    + "\n\nCliente: " + customerAbrev
                                    + "\n\nCriar Ticket #?", "Ticket.OutlookAddIn", MessageBoxButtons.YesNo,
                                    MessageBoxIcon.Question) == DialogResult.No)
                {
                    return;
                }

                Outlook.MAPIFolder inBox = (Outlook.MAPIFolder) this.Application.
                                           ActiveExplorer().Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);

                Outlook.MAPIFolder destFolder = inBox.Folders["_Tickets"];

                //mailItem.Body += "\n\n# Ticket";
                mailItem.Categories = "#; Novo; " + customerAbrev;
                mailItem.Save();
                mailItem = mailItem.Move(destFolder);

                string msg = string.Empty;

                this.GravarTicket(ref mailItem, ref msg);

                if (msg != string.Empty)
                {
                    MessageBox.Show("Erro:\n\n" + msg, "Ticket.OutlookAddIn", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }

            #endregion
        }