OUT-477: Do not create mirror items from new item when S/MIME is enabled by default OUT-477
authorThomas
Wed, 04 Jul 2018 15:02:58 +0200
branchOUT-477
changeset 22756bd0446a25c2
parent 2274 082c7b5e81f7
child 2278 d4f7909c10ce
OUT-477: Do not create mirror items from new item when S/MIME is enabled by default
Extensions/MailItemExtensions.cs
UI/RibbonCustomizations.cs
     1.1 --- a/Extensions/MailItemExtensions.cs	Wed Jul 04 11:19:24 2018 +0200
     1.2 +++ b/Extensions/MailItemExtensions.cs	Wed Jul 04 15:02:58 2018 +0200
     1.3 @@ -217,6 +217,23 @@
     1.4          }
     1.5  
     1.6          /// <summary>
     1.7 +        /// Deletes all pEp user properties and sets the UseTnef MAPI property
     1.8 +        /// to false to prevent Outlook adding the automatic winmail.dat attachment.
     1.9 +        /// </summary>
    1.10 +        /// <param name="omi">The Outlook mail item to process with.</param>
    1.11 +        public static void AvoidWinmailDatAttachment(this Outlook.MailItem omi)
    1.12 +        {
    1.13 +            // Remove all pEp user properties
    1.14 +            omi?.DeleteUserProperty(CryptableMailItem.USER_PROPERTY_KEY_FORCE_UNENCRYPTED);
    1.15 +            omi?.DeleteUserProperty(CryptableMailItem.USER_PROPERTY_KEY_ENABLE_PROTECTION);
    1.16 +            omi?.DeleteUserProperty(CryptableMailItem.USER_PROPERTY_KEY_IS_ORIGINALLY_ENCRYPTED);
    1.17 +            omi?.DeleteUserProperty(CryptableMailItem.USER_PROPERTY_KEY_ORIGINAL_RATING);
    1.18 +
    1.19 +            // Do not allow TNEF/RTF format with 'winmail.dat' attachment
    1.20 +            MapiHelper.SetProperty(omi, MapiProperty.PidLidUseTnef, false);
    1.21 +        }
    1.22 +
    1.23 +        /// <summary>
    1.24          /// Creates a new Outlook mail item in the unencrypted folder that is a mirror copy
    1.25          /// of the original mail item. This method will also update the mirror cache.
    1.26          /// Note: Normally, it should not be needed to pass a messageId, as we normally use the
    1.27 @@ -235,202 +252,211 @@
    1.28              Outlook.MailItem mirror = null;
    1.29              Outlook.Folder folder = omi.GetMirrorFolder();
    1.30  
    1.31 -            try
    1.32 +            /* If S/MIME is enabled by default, the mirror creation by new mail item fails during the
    1.33 +             * Save() method. 
    1.34 +             */
    1.35 +            if (omi.GetIsSMIMEEnabled() == false)
    1.36              {
    1.37 -                Log.Verbose("CreateMirrorOMI: Creating by new mail item.");
    1.38 -
    1.39 -                mirror = (Outlook.MailItem)folder.Items.Add(Outlook.OlItemType.olMailItem);
    1.40 -                mirror.UnRead = false;
    1.41 -
    1.42 -                // Set received and sent time
    1.43                  try
    1.44                  {
    1.45 -                    MapiHelper.SetProperty(mirror, MapiProperty.PidTagMessageDeliveryTime, omi.ReceivedTime.ToUniversalTime());
    1.46 -                    MapiHelper.SetProperty(mirror, MapiProperty.PidTagClientSubmitTime, omi.SentOn.ToUniversalTime());
    1.47 -                }
    1.48 -                catch (Exception ex)
    1.49 -                {
    1.50 -                    Log.Verbose("CreateMirrorOMI: Failed to set received and sent time. " + ex.ToString());
    1.51 -                    throw;
    1.52 -                }
    1.53 +                    Log.Verbose("CreateMirrorOMI: Creating by new mail item.");
    1.54  
    1.55 -                // Attempt to set sender information
    1.56 -                try
    1.57 -                {
    1.58 -                    mirror.Sender = omi.Sender;
    1.59 -                }
    1.60 -                catch (Exception ex)
    1.61 -                {
    1.62 -                    Log.Verbose("CreateMirrorOMI: Failed to set sender directly. " + ex.ToString());
    1.63 -                }
    1.64 +                    mirror = (Outlook.MailItem)folder.Items.Add(Outlook.OlItemType.olMailItem);
    1.65 +                    mirror.UnRead = false;
    1.66  
    1.67 -                try
    1.68 -                {
    1.69 -                    MapiHelper.SetProperty(mirror, MapiProperty.PidTagSenderName, omi.SenderName);
    1.70 -                    MapiHelper.SetProperty(mirror, MapiProperty.PidTagSenderEmailAddress, omi.SenderEmailAddress);
    1.71 -
    1.72 -                    // Entry ID is needed to resolve the sender so a reply is possible without having to re-enter address, this is last in case it fails
    1.73 -                    MapiHelper.SetProperty(mirror, MapiProperty.PidTagSenderEntryId, MapiHelper.GetProperty(omi, MapiProperty.PidTagSenderEntryId));
    1.74 -                }
    1.75 -                catch (Exception ex)
    1.76 -                {
    1.77 -                    Log.Verbose("CreateMirrorOMI: Failed to set sender through MAPI properties. " + ex.ToString());
    1.78 -                    throw;
    1.79 -                }
    1.80 -
    1.81 -                try
    1.82 -                {
    1.83 -                    mirror.SendUsingAccount = omi.SendUsingAccount;
    1.84 -                }
    1.85 -                catch (Exception ex)
    1.86 -                {
    1.87 -                    Log.Verbose("CreateMirrorOMI: Failed to set SendUsingAccount. " + ex.ToString());
    1.88 -                }
    1.89 -
    1.90 -                // Set flags
    1.91 -                messageFlags = (System.Int32)MapiHelper.GetProperty(mirror, MapiProperty.PidTagMessageFlags);
    1.92 -                messageFlags &= ~((System.Int32)MapiPropertyValue.EnumPidTagMessageFlags.mfUnsent);  // Clear UNSENT flag -- must be done before save
    1.93 -                messageFlags |= ((System.Int32)MapiPropertyValue.EnumPidTagMessageFlags.mfRead);     // Mark as read
    1.94 -                try
    1.95 -                {
    1.96 -                    MapiHelper.SetProperty(mirror, MapiProperty.PidTagMessageFlags, messageFlags);
    1.97 -                }
    1.98 -                catch (Exception ex)
    1.99 -                {
   1.100 -                    Log.Verbose("CreateMirrorOMI: Failed to set message flags. " + ex.ToString());
   1.101 -                }
   1.102 -
   1.103 -                // Conversation information
   1.104 -                try
   1.105 -                {
   1.106 -                    /* Note: PidTagConversationId cannot be set even through the MAPI accessor.
   1.107 -                     * This is by design since this property is computed automatically from other properties.
   1.108 -                     * See: https://msdn.microsoft.com/en-us/library/ee204279.aspx
   1.109 -                     */
   1.110 -
   1.111 -                    str = omi.ConversationIndex;
   1.112 -                    bytes = MapiHelper.StringToPtypBinary(str);
   1.113 -                    if (bytes?.Length > 0)
   1.114 +                    // Set received and sent time
   1.115 +                    try
   1.116                      {
   1.117 -                        MapiHelper.SetProperty(mirror, MapiProperty.PidTagConversationIndex, bytes);
   1.118 +                        MapiHelper.SetProperty(mirror, MapiProperty.PidTagMessageDeliveryTime, omi.ReceivedTime.ToUniversalTime());
   1.119 +                        MapiHelper.SetProperty(mirror, MapiProperty.PidTagClientSubmitTime, omi.SentOn.ToUniversalTime());
   1.120 +                    }
   1.121 +                    catch (Exception ex)
   1.122 +                    {
   1.123 +                        Log.Verbose("CreateMirrorOMI: Failed to set received and sent time. " + ex.ToString());
   1.124 +                        throw;
   1.125                      }
   1.126  
   1.127 -                    MapiHelper.SetProperty(mirror, MapiProperty.PidTagConversationIndexTracking, true);
   1.128 -                    MapiHelper.SetProperty(mirror, MapiProperty.PidTagConversationTopic, omi.ConversationTopic);
   1.129 -                }
   1.130 -                catch (Exception ex)
   1.131 -                {
   1.132 -                    Log.Verbose("CreateMirrorOMI: Failed to set conversation information. " + ex.ToString());
   1.133 -                }
   1.134 +                    // Attempt to set sender information
   1.135 +                    try
   1.136 +                    {
   1.137 +                        mirror.Sender = omi.Sender;
   1.138 +                    }
   1.139 +                    catch (Exception ex)
   1.140 +                    {
   1.141 +                        Log.Verbose("CreateMirrorOMI: Failed to set sender directly. " + ex.ToString());
   1.142 +                    }
   1.143  
   1.144 -                mirror.To = omi.To;
   1.145 -                mirror.CC = omi.CC;
   1.146 -                mirror.BCC = omi.BCC;
   1.147 -                mirror.Subject = omi.Subject;
   1.148 -                mirror.Body = string.Empty;
   1.149 -                mirror.Save();
   1.150 +                    try
   1.151 +                    {
   1.152 +                        MapiHelper.SetProperty(mirror, MapiProperty.PidTagSenderName, omi.SenderName);
   1.153 +                        MapiHelper.SetProperty(mirror, MapiProperty.PidTagSenderEmailAddress, omi.SenderEmailAddress);
   1.154  
   1.155 -                /* Set all received-by information
   1.156 -                 * 
   1.157 -                 * This must be stored as a user-property because settings MAPI properties such as "PR_RECEIVED_BY_EMAIL_ADDRESS"
   1.158 -                 * is not supported by Outlook. The user-properties are named the same as the MAPI property for simplicity.
   1.159 -                 * 
   1.160 -                 * In most situations the received-by information will not be needed.
   1.161 -                 * Decryption for example uses the recipients/identities in the original mail item itself.
   1.162 -                 * However, the GetMyIdentity(omi) method requires this information in some cases:
   1.163 -                 *  • The true 'myself' identity of a mail item is not an account registerd in Outlook. This can happen
   1.164 -                 *    using aliases or when forwarding emails from another account.
   1.165 -                 *  • This is primarily needed when displaying mirrors themselves then opening the handshake dialog.
   1.166 -                 */
   1.167 -                try
   1.168 -                {
   1.169 -                    /* Get the "received by" email address. In case this returns an internal X500 Exchange address ("/O=DOMAIN/OU=EXCHANGE ADMINISTRATIVE GROUP..."),
   1.170 -                     * compare it with the recipients' addresses and take the primary SMTP address.
   1.171 +                        // Entry ID is needed to resolve the sender so a reply is possible without having to re-enter address, this is last in case it fails
   1.172 +                        MapiHelper.SetProperty(mirror, MapiProperty.PidTagSenderEntryId, MapiHelper.GetProperty(omi, MapiProperty.PidTagSenderEntryId));
   1.173 +                    }
   1.174 +                    catch (Exception ex)
   1.175 +                    {
   1.176 +                        Log.Verbose("CreateMirrorOMI: Failed to set sender through MAPI properties. " + ex.ToString());
   1.177 +                        throw;
   1.178 +                    }
   1.179 +
   1.180 +                    try
   1.181 +                    {
   1.182 +                        mirror.SendUsingAccount = omi.SendUsingAccount;
   1.183 +                    }
   1.184 +                    catch (Exception ex)
   1.185 +                    {
   1.186 +                        Log.Verbose("CreateMirrorOMI: Failed to set SendUsingAccount. " + ex.ToString());
   1.187 +                    }
   1.188 +
   1.189 +                    // Set flags
   1.190 +                    messageFlags = (System.Int32)MapiHelper.GetProperty(mirror, MapiProperty.PidTagMessageFlags);
   1.191 +                    messageFlags &= ~((System.Int32)MapiPropertyValue.EnumPidTagMessageFlags.mfUnsent);  // Clear UNSENT flag -- must be done before save
   1.192 +                    messageFlags |= ((System.Int32)MapiPropertyValue.EnumPidTagMessageFlags.mfRead);     // Mark as read
   1.193 +                    try
   1.194 +                    {
   1.195 +                        MapiHelper.SetProperty(mirror, MapiProperty.PidTagMessageFlags, messageFlags);
   1.196 +                    }
   1.197 +                    catch (Exception ex)
   1.198 +                    {
   1.199 +                        Log.Verbose("CreateMirrorOMI: Failed to set message flags. " + ex.ToString());
   1.200 +                    }
   1.201 +
   1.202 +                    // Conversation information
   1.203 +                    try
   1.204 +                    {
   1.205 +                        /* Note: PidTagConversationId cannot be set even through the MAPI accessor.
   1.206 +                         * This is by design since this property is computed automatically from other properties.
   1.207 +                         * See: https://msdn.microsoft.com/en-us/library/ee204279.aspx
   1.208 +                         */
   1.209 +
   1.210 +                        str = omi.ConversationIndex;
   1.211 +                        bytes = MapiHelper.StringToPtypBinary(str);
   1.212 +                        if (bytes?.Length > 0)
   1.213 +                        {
   1.214 +                            MapiHelper.SetProperty(mirror, MapiProperty.PidTagConversationIndex, bytes);
   1.215 +                        }
   1.216 +
   1.217 +                        MapiHelper.SetProperty(mirror, MapiProperty.PidTagConversationIndexTracking, true);
   1.218 +                        MapiHelper.SetProperty(mirror, MapiProperty.PidTagConversationTopic, omi.ConversationTopic);
   1.219 +                    }
   1.220 +                    catch (Exception ex)
   1.221 +                    {
   1.222 +                        Log.Verbose("CreateMirrorOMI: Failed to set conversation information. " + ex.ToString());
   1.223 +                    }
   1.224 +
   1.225 +                    // Remove S/MIME flags if necessary
   1.226 +                    mirror.RemoveSMIMEFlags();
   1.227 +
   1.228 +                    mirror.To = omi.To;
   1.229 +                    mirror.CC = omi.CC;
   1.230 +                    mirror.BCC = omi.BCC;
   1.231 +                    mirror.Subject = omi.Subject;
   1.232 +                    mirror.Body = string.Empty;
   1.233 +                    mirror.Save();
   1.234 +
   1.235 +                    /* Set all received-by information
   1.236 +                     * 
   1.237 +                     * This must be stored as a user-property because settings MAPI properties such as "PR_RECEIVED_BY_EMAIL_ADDRESS"
   1.238 +                     * is not supported by Outlook. The user-properties are named the same as the MAPI property for simplicity.
   1.239 +                     * 
   1.240 +                     * In most situations the received-by information will not be needed.
   1.241 +                     * Decryption for example uses the recipients/identities in the original mail item itself.
   1.242 +                     * However, the GetMyIdentity(omi) method requires this information in some cases:
   1.243 +                     *  • The true 'myself' identity of a mail item is not an account registerd in Outlook. This can happen
   1.244 +                     *    using aliases or when forwarding emails from another account.
   1.245 +                     *  • This is primarily needed when displaying mirrors themselves then opening the handshake dialog.
   1.246                       */
   1.247 -                    string email = MapiHelper.GetProperty(omi, MapiProperty.PidTagReceivedByEmailAddress) as string;
   1.248 -                    if (email.StartsWith("/O"))
   1.249 +                    try
   1.250                      {
   1.251 -                        Outlook.Recipients recipients = null;
   1.252 -                        Outlook.Recipient recipient = null;
   1.253 -                        Outlook.AddressEntry addressEntry = null;
   1.254 -                        Outlook.ExchangeUser exchangeUser = null;
   1.255 +                        /* Get the "received by" email address. In case this returns an internal X500 Exchange address ("/O=DOMAIN/OU=EXCHANGE ADMINISTRATIVE GROUP..."),
   1.256 +                         * compare it with the recipients' addresses and take the primary SMTP address.
   1.257 +                         */
   1.258 +                        string email = MapiHelper.GetProperty(omi, MapiProperty.PidTagReceivedByEmailAddress) as string;
   1.259 +                        if (email.StartsWith("/O"))
   1.260 +                        {
   1.261 +                            Outlook.Recipients recipients = null;
   1.262 +                            Outlook.Recipient recipient = null;
   1.263 +                            Outlook.AddressEntry addressEntry = null;
   1.264 +                            Outlook.ExchangeUser exchangeUser = null;
   1.265  
   1.266 -                        try
   1.267 -                        {
   1.268 -                            recipients = omi.Recipients;
   1.269 +                            try
   1.270 +                            {
   1.271 +                                recipients = omi.Recipients;
   1.272  
   1.273 -                            for (int i = 1; i <= recipients.Count; i++)
   1.274 +                                for (int i = 1; i <= recipients.Count; i++)
   1.275 +                                {
   1.276 +                                    recipient = recipients[i];
   1.277 +                                    addressEntry = recipient?.AddressEntry;
   1.278 +                                    exchangeUser = addressEntry?.GetExchangeUser();
   1.279 +
   1.280 +                                    if (email.ToUpperInvariant().Equals(exchangeUser?.Address?.ToUpperInvariant()))
   1.281 +                                    {
   1.282 +                                        email = exchangeUser?.PrimarySmtpAddress;
   1.283 +                                        break;
   1.284 +                                    }
   1.285 +
   1.286 +                                    recipient = null;
   1.287 +                                    addressEntry = null;
   1.288 +                                    exchangeUser = null;
   1.289 +                                }
   1.290 +                            }
   1.291 +                            catch (Exception ex)
   1.292                              {
   1.293 -                                recipient = recipients[i];
   1.294 -                                addressEntry = recipient?.AddressEntry;
   1.295 -                                exchangeUser = addressEntry?.GetExchangeUser();
   1.296 -
   1.297 -                                if (email.ToUpperInvariant().Equals(exchangeUser?.Address?.ToUpperInvariant()))
   1.298 -                                {
   1.299 -                                    email = exchangeUser?.PrimarySmtpAddress;
   1.300 -                                    break;
   1.301 -                                }
   1.302 -
   1.303 +                                Log.Error("CreateMirrorOMI: Error getting ReceivedByEmailAddress. " + ex.ToString());
   1.304 +                            }
   1.305 +                            finally
   1.306 +                            {
   1.307 +                                recipients = null;
   1.308                                  recipient = null;
   1.309                                  addressEntry = null;
   1.310                                  exchangeUser = null;
   1.311                              }
   1.312                          }
   1.313 -                        catch (Exception ex)
   1.314 -                        {
   1.315 -                            Log.Error("CreateMirrorOMI: Error getting ReceivedByEmailAddress. " + ex.ToString());
   1.316 -                        }
   1.317 -                        finally
   1.318 -                        {
   1.319 -                            recipients = null;
   1.320 -                            recipient = null;
   1.321 -                            addressEntry = null;
   1.322 -                            exchangeUser = null;
   1.323 -                        }
   1.324 +                        mirror.SetUserProperty(MapiProperty.PidTagReceivedByEmailAddress.DaslName, email,
   1.325 +                                            Outlook.OlUserPropertyType.olText);
   1.326 +                        mirror.SetUserProperty(MapiProperty.PidTagReceivedByName.DaslName,
   1.327 +                                            MapiHelper.GetProperty(omi, MapiProperty.PidTagReceivedByName),
   1.328 +                                            Outlook.OlUserPropertyType.olText);
   1.329                      }
   1.330 -                    mirror.SetUserProperty(MapiProperty.PidTagReceivedByEmailAddress.DaslName, email,
   1.331 -                                        Outlook.OlUserPropertyType.olText);
   1.332 -                    mirror.SetUserProperty(MapiProperty.PidTagReceivedByName.DaslName,
   1.333 -                                        MapiHelper.GetProperty(omi, MapiProperty.PidTagReceivedByName),
   1.334 -                                        Outlook.OlUserPropertyType.olText);
   1.335 +                    catch (Exception ex)
   1.336 +                    {
   1.337 +                        Log.Verbose("CreateMirrorOMI: Failed to set received-by information. " + ex.ToString());
   1.338 +                    }
   1.339 +
   1.340 +                    // Mark the mail item as a mirror
   1.341 +                    mirror.SetUserProperty(MailItemExtensions.USER_PROPERTY_KEY_IS_MIRROR, true, Outlook.OlUserPropertyType.olYesNo);
   1.342 +
   1.343 +                    /* Set the incoming status of the new mail item.
   1.344 +                     * This is necessary because manually setting the PR_RECEIVED_BY_ENTRYID fails if called by user code.
   1.345 +                     * PR_RECEIVED_BY_ENTRYID is only set when copying the original mail item.
   1.346 +                     */
   1.347 +                    if (omi.GetIsIncoming())
   1.348 +                    {
   1.349 +                        mirror.SetUserProperty(MailItemExtensions.USER_PROPERTY_KEY_IS_INCOMING, true, Outlook.OlUserPropertyType.olYesNo);
   1.350 +                    }
   1.351 +                    else
   1.352 +                    {
   1.353 +                        mirror.SetUserProperty(MailItemExtensions.USER_PROPERTY_KEY_IS_INCOMING, false, Outlook.OlUserPropertyType.olYesNo);
   1.354 +                    }
   1.355 +
   1.356 +                    // Add the original EntryID or the messageId if the latter has been passed
   1.357 +                    mirror.SetUserProperty(MailItemExtensions.USER_PROPERTY_KEY_ORIG_ENTRY_ID, messageId ?? omi.EntryID);
   1.358 +                    mirror.Save();
   1.359 +
   1.360 +                    // Move the mirror copy to the correct folder
   1.361 +                    // This is needed as some account-types/office versions will still create a mail item in the default folder
   1.362 +                    // even if folder.Items.Add is used to specify a location.
   1.363 +                    // WARNING: this creates a new mail item that will no longer be referenced here!!
   1.364 +                    mirror.Move(folder);
   1.365 +                    created = true;
   1.366                  }
   1.367                  catch (Exception ex)
   1.368                  {
   1.369 -                    Log.Verbose("CreateMirrorOMI: Failed to set received-by information. " + ex.ToString());
   1.370 +                    Log.Verbose("CreateMirrorOMI: Creating new mail item failed. " + ex.ToString());
   1.371 +                    mirror.PermanentlyDelete();
   1.372 +                    mirror = null;
   1.373                  }
   1.374 -
   1.375 -                // Mark the mail item as a mirror
   1.376 -                mirror.SetUserProperty(MailItemExtensions.USER_PROPERTY_KEY_IS_MIRROR, true, Outlook.OlUserPropertyType.olYesNo);
   1.377 -
   1.378 -                /* Set the incoming status of the new mail item.
   1.379 -                 * This is necessary because manually setting the PR_RECEIVED_BY_ENTRYID fails if called by user code.
   1.380 -                 * PR_RECEIVED_BY_ENTRYID is only set when copying the original mail item.
   1.381 -                 */
   1.382 -                if (omi.GetIsIncoming())
   1.383 -                {
   1.384 -                    mirror.SetUserProperty(MailItemExtensions.USER_PROPERTY_KEY_IS_INCOMING, true, Outlook.OlUserPropertyType.olYesNo);
   1.385 -                }
   1.386 -                else
   1.387 -                {
   1.388 -                    mirror.SetUserProperty(MailItemExtensions.USER_PROPERTY_KEY_IS_INCOMING, false, Outlook.OlUserPropertyType.olYesNo);
   1.389 -                }
   1.390 -
   1.391 -                // Add the original EntryID or the messageId if the latter has been passed
   1.392 -                mirror.SetUserProperty(MailItemExtensions.USER_PROPERTY_KEY_ORIG_ENTRY_ID, messageId ?? omi.EntryID);
   1.393 -                mirror.Save();
   1.394 -
   1.395 -                // Move the mirror copy to the correct folder
   1.396 -                // This is needed as some account-types/office versions will still create a mail item in the default folder
   1.397 -                // even if folder.Items.Add is used to specify a location.
   1.398 -                // WARNING: this creates a new mail item that will no longer be referenced here!!
   1.399 -                mirror.Move(folder);
   1.400 -                created = true;
   1.401 -            }
   1.402 -            catch (Exception ex)
   1.403 -            {
   1.404 -                Log.Verbose("CreateMirrorOMI: Creating new mail item failed. " + ex.ToString());
   1.405 -                mirror.PermanentlyDelete();
   1.406 -                mirror = null;
   1.407              }
   1.408  
   1.409              if (created == false)
   1.410 @@ -502,6 +528,8 @@
   1.411                      // Add the original EntryID to the mirror copy
   1.412                      // This must be done before the move as the reference is then lost
   1.413                      mirror.SetUserProperty(MailItemExtensions.USER_PROPERTY_KEY_ORIG_ENTRY_ID, messageId ?? omi.EntryID);
   1.414 +
   1.415 +                    mirror.RemoveSMIMEFlags();
   1.416                      mirror.Save();
   1.417  
   1.418                      // Move the mirror copy to the correct folder
   1.419 @@ -526,17 +554,8 @@
   1.420              }
   1.421  
   1.422              // Release objects
   1.423 -            if (mirror != null)
   1.424 -            {
   1.425 -                // Marshal.ReleaseComObject(omi);
   1.426 -                mirror = null;
   1.427 -            }
   1.428 -
   1.429 -            if (folder != null)
   1.430 -            {
   1.431 -                // Marshal.ReleaseComObject(folder);
   1.432 -                folder = null;
   1.433 -            }
   1.434 +            mirror = null;
   1.435 +            folder = null;
   1.436  
   1.437              /* Locate and return the mirror that was just created.
   1.438               * During this find method, the mirror cache will be updated when found.
   1.439 @@ -1786,23 +1805,6 @@
   1.440          }
   1.441  
   1.442          /// <summary>
   1.443 -        /// Deletes all pEp user properties and sets the UseTnef MAPI property
   1.444 -        /// to false to prevent Outlook adding the automatic winmail.dat attachment.
   1.445 -        /// </summary>
   1.446 -        /// <param name="omi">The Outlook mail item to process with.</param>
   1.447 -        public static void AvoidWinmailDatAttachment(this Outlook.MailItem omi)
   1.448 -        {
   1.449 -            // Remove all pEp user properties
   1.450 -            omi?.DeleteUserProperty(CryptableMailItem.USER_PROPERTY_KEY_FORCE_UNENCRYPTED);
   1.451 -            omi?.DeleteUserProperty(CryptableMailItem.USER_PROPERTY_KEY_ENABLE_PROTECTION);
   1.452 -            omi?.DeleteUserProperty(CryptableMailItem.USER_PROPERTY_KEY_IS_ORIGINALLY_ENCRYPTED);
   1.453 -            omi?.DeleteUserProperty(CryptableMailItem.USER_PROPERTY_KEY_ORIGINAL_RATING);
   1.454 -
   1.455 -            // Do not allow TNEF/RTF format with 'winmail.dat' attachment
   1.456 -            MapiHelper.SetProperty(omi, MapiProperty.PidLidUseTnef, false);
   1.457 -        }
   1.458 -
   1.459 -        /// <summary>
   1.460          /// Gets the own pEp identity for the SendUsingAccount of the MailItem.
   1.461          /// Warning: This can return null.
   1.462          /// </summary>
   1.463 @@ -1838,6 +1840,26 @@
   1.464          }
   1.465  
   1.466          /// <summary>
   1.467 +        /// Removes the S/MIME encryption and signature flags.
   1.468 +        /// Note: this method doesn't save the mail item.
   1.469 +        /// </summary>
   1.470 +        /// <param name="omi">The Outlook mail item to process with.</param>
   1.471 +        public static void RemoveSMIMEFlags(this Outlook.MailItem omi)
   1.472 +        {
   1.473 +            try
   1.474 +            {
   1.475 +                // Get the security flags and remove S/MIME encryption and signature
   1.476 +                int secFlags = (int)MapiHelper.GetProperty(omi, MapiProperty.PidTagSecurityFlags);
   1.477 +                secFlags &= ~(int)MapiPropertyValue.SecurityFlags.Encrypted;
   1.478 +                secFlags &= ~(int)MapiPropertyValue.SecurityFlags.Signed;
   1.479 +            }
   1.480 +            catch (Exception ex)
   1.481 +            {
   1.482 +                Log.Error("RemoveSMIMEFlags: Error removing S/MIME flags. " + ex.ToString());
   1.483 +            }
   1.484 +        }
   1.485 +
   1.486 +        /// <summary>
   1.487          /// Creates a message container for the given Outlook mail item that represents its data
   1.488          /// and how it is stored/located within Outlook.
   1.489          /// Warning: This method can take some time (especially if a mirror is being located).
     2.1 --- a/UI/RibbonCustomizations.cs	Wed Jul 04 11:19:24 2018 +0200
     2.2 +++ b/UI/RibbonCustomizations.cs	Wed Jul 04 15:02:58 2018 +0200
     2.3 @@ -600,8 +600,8 @@
     2.4          {
     2.5              // When a message is marked as never unsecure or forcefully proteced, it cannot be forcefully unencrypted
     2.6              if (((bool)this.GetProperty(MailItemExtensions.PEPProperty.NeverUnsecure) || (this.GetProperty(MailItemExtensions.PEPProperty.ForceProtection) != null)) ||
     2.7 -                (((bool)this.GetProperty(MailItemExtensions.PEPProperty.ForceUnencrypted) == false) &&
     2.8 -                 (this.GetDisplayRating() == pEpRating.pEpRatingUnencrypted)))
     2.9 +                (((bool)this.GetProperty(MailItemExtensions.PEPProperty.ForceUnencrypted) == false) && (this.GetDisplayRating() == pEpRating.pEpRatingUnencrypted)) ||
    2.10 +                (this.GetIsSMIMEEnabled()))
    2.11              {
    2.12                  return (false);
    2.13              }
    2.14 @@ -781,11 +781,11 @@
    2.15              // Not in Reader mode, not forcefully unencrypted and unsecure rating - or if property is set
    2.16              bool visible = false;
    2.17  
    2.18 -            if (Globals.RELEASE_MODE != Globals.ReleaseMode.Reader)
    2.19 +            if ((Globals.RELEASE_MODE != Globals.ReleaseMode.Reader) &&
    2.20 +                (this.GetIsSMIMEEnabled() == false) &&
    2.21 +                (this.GetHasBccRecipients() == false))
    2.22              {
    2.23 -                if ((this.GetIsPEPEnabled()) &&
    2.24 -                    (this.GetIsSMIMEEnabled() == false) &&
    2.25 -                    (this.GetHasBccRecipients() == false))
    2.26 +                if (this.GetIsPEPEnabled())
    2.27                  {
    2.28                      visible = ((this.GetProperty(MailItemExtensions.PEPProperty.ForceUnencrypted) as bool? == false) &&
    2.29                                 ((this.GetDisplayRating() == pEpRating.pEpRatingUnencrypted) || (this.GetDisplayRating() == pEpRating.pEpRatingUnencryptedForSome)) ||
    2.30 @@ -813,7 +813,9 @@
    2.31          {
    2.32              // When a message is being sent forcefully unencrypted, it cannot be marked never unsecure
    2.33              if (((this.GetProperty(MailItemExtensions.PEPProperty.ForceUnencrypted) as bool?) == true) ||
    2.34 -                 (this.GetDisplayRating() < pEpRating.pEpRatingReliable))
    2.35 +                (this.GetDisplayRating() < pEpRating.pEpRatingReliable) ||
    2.36 +                (this.GetIsSMIMEEnabled() ||
    2.37 +                (this.GetHasBccRecipients())))
    2.38              {
    2.39                  return (false);
    2.40              }