private static void ProcessEmail(Outlook.MailItem email)
        {
            //Email may have been deleted or moved so check if it exists first.
            if (email is object)
            {
                //Check if the email has a Sender.
                if (email.Recipients is object)
                {
                    Outlook.Recipients recipients = email.Recipients;
                    Outlook.Recipient  recipient  = recipients[1];

                    if (recipient is object)
                    {
                        //Find the Contact accociated with the Sender.
                        InTouchContact      mailContact = null;
                        Outlook.ContactItem contact     = InTouch.Contacts.FindContactFromEmailAddress(recipient.Address);
                        if (contact is object)
                        {
                            mailContact = new InTouchContact(contact);
                        }

                        //If found then try to process the email.
                        if (mailContact is object)
                        {
                            switch (mailContact.SentAction)
                            {
                            case EmailAction.None:     //Don't do anything to the email.
                                Log.Message("Sent Email : Delivery Action set to None. " + recipient.Address);
                                break;

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

                            case EmailAction.Move:     //Move the email if its passed its action date.
                                Log.Message("Sent Email : Moving email from " + recipient.Address);
                                MoveEmailToFolder(mailContact.SentPath, email);
                                break;
                            }
                            mailContact.SaveAndDispose();
                        }
                    }
                    else //If not found then just log it for the moment.
                    {
                        try
                        {
                            //Get the 'On Behalf' property from the email.
                            Outlook.PropertyAccessor mapiPropertyAccessor;
                            string propertyName = "http://schemas.microsoft.com/mapi/proptag/0x0065001F";
                            mapiPropertyAccessor = email.PropertyAccessor;
                            string onBehalfEmailAddress = mapiPropertyAccessor.GetProperty(propertyName).ToString();
                            if (mapiPropertyAccessor is object)
                            {
                                Marshal.ReleaseComObject(mapiPropertyAccessor);
                            }

                            //Log the details.
                            Log.Message("Sent Email : No Contact for " + email.SenderEmailAddress);
                            Log.Message("SenderName         : " + email.SenderName);
                            Log.Message("SentOnBehalfOfName : " + email.SentOnBehalfOfName);
                            Log.Message("ReplyRecipientNames: " + email.ReplyRecipientNames);
                            Log.Message("On Behalf: " + onBehalfEmailAddress);
                            Log.Message("");
                        }
                        catch (Exception ex)
                        {
                            Log.Error(ex);
                            throw;
                        }
                    }
                }
            }
        }
Beispiel #2
0
        private void CheckEmailSender()
        {
            Outlook.Selection selection = Globals.ThisAddIn.Application.ActiveExplorer().Selection;
            Outlook.MailItem  email     = null;
            if (selection.Count > 0)
            {
                email = selection[1] as Outlook.MailItem;
            }

            if (email is object)
            {
                if (email.Sender is object)
                {
                    //Try to find contact from email adddress.
                    InTouchContact emailContact = null;
                    try
                    {
                        Outlook.ContactItem contact = InTouch.Contacts.FindContactFromEmailAddress(email.Sender.Address);
                        if (contact is object)
                        {
                            emailContact = new InTouchContact(contact);
                        }
                    }
                    catch (Exception ex)
                    {
                        Log.Error(ex);
                        throw;
                    }

                    if (emailContact is object)
                    {
                        //Make the Contact Button visable and add the image and name to the button.
                        Globals.Ribbons.RibExplorer.buttonContact.Visible = true;

                        if (emailContact.FullName is object)
                        {
                            Globals.Ribbons.RibExplorer.buttonContact.Label = emailContact.FullName;
                        }
                        else
                        {
                            Globals.Ribbons.RibExplorer.buttonContact.Label = "";
                        }

                        if (emailContact.HasPicture)
                        {
                            Globals.Ribbons.RibExplorer.buttonContact.Image = Image.FromFile(emailContact.GetContactPicturePath());
                        }
                        else
                        {
                            Globals.Ribbons.RibExplorer.buttonContact.Image = Properties.Resources.contact;
                        }

                        //Check if the contact details are valid.
                        if (!emailContact.CheckDetails())
                        {
                            Globals.Ribbons.RibExplorer.buttonAttention.Visible = true;
                        }
                        emailContact.SaveAndDispose();
                    }
                    else
                    {
                        //As the contact was not found, make the add contact button visible.
                        Globals.Ribbons.RibExplorer.buttonAddContactPersonal.Visible = true;
                        Globals.Ribbons.RibExplorer.buttonAddContactOther.Visible    = true;
                        Globals.Ribbons.RibExplorer.buttonAddContactJunk.Visible     = true;
                    }
                }
                else
                {
                    Globals.Ribbons.RibExplorer.buttonAddContactPersonal.Visible = true;
                    Globals.Ribbons.RibExplorer.buttonAddContactOther.Visible    = true;
                    Globals.Ribbons.RibExplorer.buttonAddContactJunk.Visible     = true;
                }



                //if (email.EntryID != lastEntryID)
                //{
                //    //Track last EntryID as Explorer_SelectionChange event fires twice for each selection change.
                //    lastEntryID = email.EntryID;

                //    ClearButtons();


                //}
            }

            if (email is object)
            {
                Marshal.ReleaseComObject(email);
            }
            if (selection is object)
            {
                Marshal.ReleaseComObject(selection);
            }
        }
        // Occurs before the form region is displayed.
        // Use this.OutlookItem to get a reference to the current Outlook item.
        // Use this.OutlookFormRegion to get a reference to the form region.
        private void ContactInTouchSettings_FormRegionShowing(object sender, System.EventArgs e)
        {
            //change the colors on the FormRegion to suit the dark theme if needed.
            if ((BackColor.R == 38) && (BackColor.G == 38) && (BackColor.B == 38))
            {
                ForeColor = Color.White;

                ButtonDeliveryPath.ForeColor = Color.Black;
                ButtonReadPath.ForeColor     = Color.Black;
                ButtonSendPath.ForeColor     = Color.Black;
            }

            contact = new InTouchContact(this.OutlookItem as Outlook.ContactItem);

            LabelDeliveryPath.Text  = contact.InboxPath;
            LabelReadPath.Text      = contact.InboxPath;
            LabelSendPathValue.Text = contact.SentPath;

            switch (contact.DeliveryAction)
            {
            case EmailAction.None:
                RadioButtonDeliveryNoAction.Checked = true;
                break;

            case EmailAction.Delete:
                RadioButtonDeliveryDelete.Checked = true;
                break;

            case EmailAction.Move:
                RadioButtonDeliveryFile.Checked = true;
                break;

            case EmailAction.Junk:
                RadioButtonDeliveryJunk.Checked = true;
                break;
            }

            switch (contact.ReadAction)
            {
            case EmailAction.None:
                RadioButtonReadNoAction.Checked = true;
                break;

            case EmailAction.Delete:
                RadioButtonReadDelete.Checked = true;
                break;

            case EmailAction.Move:
                RadioButtonReadFile.Checked = true;
                break;

            case EmailAction.Junk:
                RadioButtonReadJunk.Checked = true;
                break;
            }

            switch (contact.SentAction)
            {
            case EmailAction.None:
                RadioButtonSendNoAction.Checked = true;
                break;

            case EmailAction.Delete:
                RadioButtonSendDelete.Checked = true;
                break;

            case EmailAction.Move:
                RadioButtonSendFile.Checked = true;
                break;
            }

            CheckBoxUseSamePathDelivery.Checked = contact.SamePath;

            AdjustForm();
        }
        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);
            }
        }