Merge with default sync
authorThomas
Wed, 06 Feb 2019 11:58:06 +0100
branchsync
changeset 2566 f53ce7a235c7
parent 2564 bc35c9ba5fbb (current diff)
parent 2565 2bc5570f217d (diff)
child 2568 c2085f8016dc
Merge with default
ThisAddIn.cs
--- a/Extensions/AccountExtensions.cs	Wed Feb 06 10:23:01 2019 +0100
+++ b/Extensions/AccountExtensions.cs	Wed Feb 06 11:58:06 2019 +0100
@@ -452,5 +452,49 @@
 
             return (string.IsNullOrEmpty(serverAddress) == false);
         }
+
+        /// <summary>
+        /// Gets an Outlook account for a given SMTP address.
+        /// </summary>
+        /// <param name="smtpAddress">The account's SMTP address.</param>
+        /// <returns>The account or null if no account was found or an error occured.</returns>
+        internal static Outlook.Account GetAccountForSMTPAddress(string smtpAddress)
+        {
+            Outlook.Account account = null;
+            Outlook.Accounts accounts = null;
+            Outlook.Application application = null;
+            Outlook.NameSpace session = null;
+
+            try
+            {
+                application = Globals.ThisAddIn.Application;
+                session = application.Session;
+                accounts = session.Accounts;
+
+                for (int i = 1; i <= accounts.Count; i++)
+                {
+                    account = accounts[i];
+                    if (account?.SmtpAddress?.Equals(smtpAddress) == true)
+                    {
+                        break;
+                    }
+
+                    account = null;
+                }
+            }
+            catch (Exception ex)
+            {
+                account = null;
+                Log.Error("GetAccountForSMTPAddress: Error getting account. " + ex.ToString());
+            }
+            finally
+            {
+                accounts = null;
+                application = null;
+                session = null;
+            }
+
+            return account;
+        }
     }
 }
--- a/Extensions/MailItemExtensions.cs	Wed Feb 06 10:23:01 2019 +0100
+++ b/Extensions/MailItemExtensions.cs	Wed Feb 06 11:58:06 2019 +0100
@@ -1916,7 +1916,7 @@
                 recipients = null;
                 store = null;
             }
-        }
+        }       
 
         /// <summary>
         /// Gets the own pEp identity for the SendUsingAccount of the MailItem.
@@ -1954,6 +1954,57 @@
         }
 
         /// <summary>
+        /// Checks if the given account is set as Send account and sets it
+        /// otherwise.
+        /// </summary>
+        /// <param name="omi">The Outlook mail item to process with.</param>
+        /// <param name="smtpAddress">The address of the account to set.</param>
+        /// <returns>True if the account is set correctly, otherwise false.</returns>
+        public static bool SetSendUsingAccount(this Outlook.MailItem omi, 
+                                              string smtpAddress)
+        {
+            bool success = false;
+
+            if (string.IsNullOrEmpty(smtpAddress))
+            {
+                Log.Error("SetSendUsingAccount: Address is null or empty.");
+                return success;
+            }
+
+            if ((omi.GetSendUsingAccountIdentity() is PEPIdentity sendIdentity) &&
+                (sendIdentity.Address.Equals(smtpAddress)))
+            {
+                success = true;
+                Log.Verbose("SetSendUsingAccount: Account already set.");
+            }
+            else
+            {
+                Outlook.Account account = null;
+
+                try
+                {
+                    account = AccountExtensions.GetAccountForSMTPAddress(smtpAddress);
+                    if (account != null)
+                    {
+                        omi.SendUsingAccount = account;
+                        success = true;
+                    }
+                }
+                catch (Exception ex)
+                {
+                    Log.Error("SetSendUsingAccount: Error setting account. " + ex.ToString());
+
+                }
+                finally
+                {
+                    account = null;
+                }
+            }
+
+            return success;
+        }
+
+        /// <summary>
         /// Adds the 'pEp Processing' category to a mail item's categories.
         /// </summary>
         /// <param name="omi">The Outlook mail item to process with.</param>
--- a/ThisAddIn.cs	Wed Feb 06 10:23:01 2019 +0100
+++ b/ThisAddIn.cs	Wed Feb 06 11:58:06 2019 +0100
@@ -818,9 +818,9 @@
                     if ((validateSendingAccount) &&
                         (message.From != null))
                     {
-                        if (message.From.EqualsByAddress(newItem.GetSendUsingAccountIdentity()) == false)
+                        if (newItem.SetSendUsingAccount(message.From.Address) == false)
                         {
-                            throw new Exception("Sending account does not match from identity");
+                            throw new Exception("Sending account does not match from identity or error setting Send account");
                         }
                     }