Merge with default optimizations
authorThomas
Wed, 26 Sep 2018 14:29:57 +0200
branchoptimizations
changeset 2371b2458c206976
parent 2367 c13cacd3d707
parent 2370 38dc1da79cf5
child 2372 24779f4ed749
Merge with default
CryptableMailItem.cs
PEPMessage.cs
     1.1 --- a/CryptableMailItem.cs	Wed Sep 26 11:33:32 2018 +0200
     1.2 +++ b/CryptableMailItem.cs	Wed Sep 26 14:29:57 2018 +0200
     1.3 @@ -1600,7 +1600,8 @@
     1.4                                      }
     1.5  
     1.6                                      // Apply message to mirror and save
     1.7 -                                    processedMessage.ApplyTo(mirrorMailItem, true, false);
     1.8 +                                    processedMessage.ApplyTo(mirrorMailItem, true, true);
     1.9 +
    1.10                                      try
    1.11                                      {
    1.12                                          mirrorMailItem.Save();
    1.13 @@ -1770,6 +1771,22 @@
    1.14                  ThisAddIn.DecryptionStack.DecrementDecryptionCounter();
    1.15              }
    1.16  
    1.17 +            // Catch any errors that occured during processing
    1.18 +            if (e.Error != null)
    1.19 +            {
    1.20 +                Log.Error("Processor_RunWorkerCompleted: Error during processing of mail item. " + e.Error.ToString());
    1.21 +
    1.22 +                if (this.disposeAfterProcessing)
    1.23 +                {
    1.24 +                    this.Dispose(false);
    1.25 +
    1.26 +                    // Must be called last!
    1.27 +                    this.DisposeInternalMailItem();
    1.28 +                }
    1.29 +
    1.30 +                return;
    1.31 +            }
    1.32 +
    1.33              // Get processing result
    1.34              ProcessingResult result = e.Result as ProcessingResult;
    1.35  
    1.36 @@ -1818,9 +1835,6 @@
    1.37  
    1.38                  this.ProcessingCompleted?.Invoke(this, args);
    1.39              }
    1.40 -
    1.41 -
    1.42 -            return;
    1.43          }
    1.44  
    1.45          /**************************************************************
     2.1 --- a/Mapi.cs	Wed Sep 26 11:33:32 2018 +0200
     2.2 +++ b/Mapi.cs	Wed Sep 26 14:29:57 2018 +0200
     2.3 @@ -240,6 +240,199 @@
     2.4          #endregion
     2.5  
     2.6          #region Methods
     2.7 +
     2.8 +        /// <summary>
     2.9 +        /// Gets a MAPI property value from an Outlook mail item.
    2.10 +        /// </summary>
    2.11 +        /// <param name="omi">The Outlook mail item to get the property value for.</param>
    2.12 +        /// <param name="mapiProperty">The MAPI property to get its value.</param>
    2.13 +        /// <returns>The value or null if it doesn't exist or an error occured.</returns>
    2.14 +        public static object GetMAPIProperty(Outlook.MailItem omi, MapiProperty.MapiProp mapiProperty)
    2.15 +        {
    2.16 +            object propertyValue = null;
    2.17 +
    2.18 +            // Pointer to IUnknown interface
    2.19 +            IntPtr IUnknown = IntPtr.Zero;
    2.20 +
    2.21 +            // Pointer to IMessage interface
    2.22 +            IntPtr IMessage = IntPtr.Zero;
    2.23 +
    2.24 +            // Pointer to IMAPIProp interface
    2.25 +            IntPtr IMAPIProp = IntPtr.Zero;
    2.26 +
    2.27 +            // Structure that will hold the property value
    2.28 +            Mapi.SPropValue sPropValue;
    2.29 +
    2.30 +            // A pointer that points to the SPropValue structure 
    2.31 +            IntPtr ptrPropValue = IntPtr.Zero;
    2.32 +
    2.33 +            // Get MAPI object from mail item
    2.34 +            object mapiObject = omi?.MAPIOBJECT;
    2.35 +            if (mapiObject == null)
    2.36 +            {
    2.37 +                Log.Error("GetMAPIProperty: MAPI object is null. Returning null.");
    2.38 +                return null;
    2.39 +            }
    2.40 +
    2.41 +            try
    2.42 +            {
    2.43 +                // Initialize MAPI
    2.44 +                Mapi.MAPIInitialize(IntPtr.Zero);
    2.45 +
    2.46 +                // Get the IUnknown interface from the MAPI object
    2.47 +                IUnknown = Marshal.GetIUnknownForObject(mapiObject);
    2.48 +
    2.49 +                // Set the IMessage interface GUID that we pass to retrieve the IMessage interface.
    2.50 +                Guid guidIMessage = new Guid(Mapi.MAPIInterfaceIds.IMessage);
    2.51 +
    2.52 +                // Try to retrieve the IMessage interface
    2.53 +                if (Marshal.QueryInterface(IUnknown, ref guidIMessage, out IMessage) != Mapi.HResult.S_OK)
    2.54 +                {
    2.55 +                    Log.Error("GetMAPIProperty: Could not retrieve IMessage interface. Returning null.");
    2.56 +                    return null;
    2.57 +                }
    2.58 +
    2.59 +                // Set the IMAPIProp interface GUID that we pass to retrieve the IMAPIProp Interface.
    2.60 +                Guid guidIMAPIProp = new Guid(Mapi.MAPIInterfaceIds.IMAPIProp);
    2.61 +
    2.62 +                // Try to retrieve the IMAPIProp interface
    2.63 +                if ((Marshal.QueryInterface(IMessage, ref guidIMAPIProp, out IMAPIProp) != Mapi.HResult.S_OK) ||
    2.64 +                    (IMAPIProp == IntPtr.Zero))
    2.65 +                {
    2.66 +                    Log.Error("GetMAPIProperty: Could not retrieve IMAPIProp interface. Returning null.");
    2.67 +                    return null;
    2.68 +                }
    2.69 +
    2.70 +                // Try to get the property
    2.71 +                Mapi.HrGetOneProp(IMAPIProp, mapiProperty.Tag, out ptrPropValue);
    2.72 +
    2.73 +                if (ptrPropValue == IntPtr.Zero)
    2.74 +                {
    2.75 +                    Log.Error("GetMAPIProperty: Could not retrieve pointer to property value. Returning null.");
    2.76 +                    return null;
    2.77 +                }
    2.78 +
    2.79 +                // Get the SPropValue structure
    2.80 +                sPropValue = (SPropValue)Marshal.PtrToStructure(ptrPropValue, typeof(SPropValue));
    2.81 +
    2.82 +                // Set the property value's value
    2.83 +                switch (mapiProperty.DataType)
    2.84 +                {
    2.85 +                    case MapiProperty.MapiDataType.PtypBoolean:
    2.86 +                        {
    2.87 +                            propertyValue = sPropValue.Value.b;
    2.88 +                        }
    2.89 +                        break;
    2.90 +                    case MapiProperty.MapiDataType.PtypString:
    2.91 +                        {
    2.92 +                            try
    2.93 +                            {
    2.94 +                                propertyValue = Marshal.PtrToStringUni(sPropValue.Value.lpszW);
    2.95 +                            }
    2.96 +                            catch (Exception ex)
    2.97 +                            {
    2.98 +                                propertyValue = null;
    2.99 +                                throw new Exception(string.Format("Error converting to String. Property tag: {0}. Exception: {1}.", mapiProperty.DaslName, ex.ToString()));
   2.100 +                            }
   2.101 +                        }
   2.102 +                        break;
   2.103 +                    case MapiProperty.MapiDataType.PtypString8:
   2.104 +                        {
   2.105 +                            try
   2.106 +                            {
   2.107 +                                propertyValue = Marshal.PtrToStringUni(sPropValue.Value.lpszA);
   2.108 +                            }
   2.109 +                            catch (Exception ex)
   2.110 +                            {
   2.111 +                                propertyValue = null;
   2.112 +                                throw new Exception(string.Format("Error converting to String8. Property tag: {0}. Exception: {1}.", mapiProperty.DaslName, ex.ToString()));
   2.113 +                            }
   2.114 +                        }
   2.115 +                        break;
   2.116 +                    case MapiProperty.MapiDataType.PtypInteger16:
   2.117 +                        {
   2.118 +                            propertyValue = sPropValue.Value.i;
   2.119 +                        }
   2.120 +                        break;
   2.121 +                    case MapiProperty.MapiDataType.PtypInteger32:
   2.122 +                        {
   2.123 +                            propertyValue = sPropValue.Value.l;
   2.124 +                        }
   2.125 +                        break;
   2.126 +                    case MapiProperty.MapiDataType.PtypFloating32:
   2.127 +                        {
   2.128 +                            propertyValue = sPropValue.Value.flt;
   2.129 +                        }
   2.130 +                        break;
   2.131 +                    case MapiProperty.MapiDataType.PtypFloating64:
   2.132 +                    case MapiProperty.MapiDataType.PtypFloatingTime:
   2.133 +                        {
   2.134 +                            propertyValue = sPropValue.Value.at;
   2.135 +                        }
   2.136 +                        break;
   2.137 +                    case MapiProperty.MapiDataType.PtypInteger64:
   2.138 +                        {
   2.139 +                            propertyValue = sPropValue.Value.li;
   2.140 +                        }
   2.141 +                        break;
   2.142 +                    case MapiProperty.MapiDataType.PtypGuid:
   2.143 +                        {
   2.144 +                            try
   2.145 +                            {
   2.146 +                                propertyValue = Marshal.PtrToStructure(sPropValue.Value.lpguid, typeof(Guid));
   2.147 +                            }
   2.148 +                            catch (Exception ex)
   2.149 +                            {
   2.150 +                                throw new Exception(string.Format("Error converting to Guid. Property tag: {0}. Exception: {1}.", mapiProperty.DaslName, ex.ToString()));
   2.151 +                            }
   2.152 +                        }
   2.153 +                        break;
   2.154 +                    case MapiProperty.MapiDataType.PtypBinary:
   2.155 +                        {
   2.156 +                            propertyValue = sPropValue.Value.bin;
   2.157 +                        }
   2.158 +                        break;
   2.159 +                    default:
   2.160 +                        {
   2.161 +                            throw new Exception(string.Format("Error creating SPropValue. Data type {0} not supported.", Enum.GetName(typeof(MapiProperty.MapiDataType), mapiProperty.DataType)));
   2.162 +                        }
   2.163 +                }
   2.164 +            }
   2.165 +            catch (Exception ex)
   2.166 +            {
   2.167 +                propertyValue = null;
   2.168 +                Log.Error("GetMAPIProperty: Error occured. " + ex.ToString());
   2.169 +            }
   2.170 +            finally
   2.171 +            {
   2.172 +                // Free used memory structures
   2.173 +                if (ptrPropValue != IntPtr.Zero)
   2.174 +                {
   2.175 +                    Mapi.MAPIFreeBuffer(ptrPropValue);
   2.176 +                }
   2.177 +
   2.178 +                // Clean up all references to COM Objects
   2.179 +                if (IMAPIProp != IntPtr.Zero)
   2.180 +                {
   2.181 +                    Marshal.Release(IMAPIProp);
   2.182 +                }
   2.183 +
   2.184 +                if (IMessage != IntPtr.Zero)
   2.185 +                {
   2.186 +                    Marshal.Release(IMessage);
   2.187 +                }
   2.188 +
   2.189 +                if (IUnknown != IntPtr.Zero)
   2.190 +                {
   2.191 +                    Marshal.Release(IUnknown);
   2.192 +                }
   2.193 +
   2.194 +                Mapi.MAPIUninitialize();
   2.195 +            }
   2.196 +
   2.197 +            return propertyValue;
   2.198 +        }
   2.199 +
   2.200          /// <summary>
   2.201          /// Sets the given MAPI properties on the Outlook mail item.
   2.202          /// Note: the caller has to make sure that the count of MAPI properties
   2.203 @@ -443,6 +636,7 @@
   2.204                                  throw new Exception(string.Format("Error creating SPropValue. Data type {0} not supported.", Enum.GetName(typeof(MapiProperty.MapiDataType), mapiProperty.DataType)));
   2.205                              }
   2.206                      }
   2.207 +
   2.208                      Marshal.StructureToPtr(sPropValue, (IntPtr)((uint)propArray + (propValueSize * i)), false);
   2.209  
   2.210                      if (valuePtr != IntPtr.Zero)
   2.211 @@ -457,12 +651,6 @@
   2.212                  {
   2.213                      success = (IMAPIProp.SaveChanges(Mapi.SaveOptions.KEEP_OPEN_READWRITE) == Mapi.HResult.S_OK);
   2.214                  }
   2.215 -
   2.216 -                // Log problems
   2.217 -                if (problems != IntPtr.Zero)
   2.218 -                {
   2.219 -
   2.220 -                }
   2.221              }
   2.222              catch (Exception ex)
   2.223              {
   2.224 @@ -503,9 +691,9 @@
   2.225          /// <param name="values">The value to set.</param>
   2.226          /// <param name="useSetProps">Whether to use the IMAPIProp.SetProps method (instead of HrSetOneProp).</param>
   2.227          /// <returns>True if the method succeeds, otherwise false.</returns>
   2.228 -        public static bool SetMAPIProperty(Outlook.MailItem omi, 
   2.229 -                                           MapiProperty.MapiProp mapiProperty, 
   2.230 -                                           object value, 
   2.231 +        public static bool SetMAPIProperty(Outlook.MailItem omi,
   2.232 +                                           MapiProperty.MapiProp mapiProperty,
   2.233 +                                           object value,
   2.234                                             bool useSetProps = false)
   2.235          {
   2.236              if (useSetProps)
     3.1 --- a/PEPMessage.cs	Wed Sep 26 11:33:32 2018 +0200
     3.2 +++ b/PEPMessage.cs	Wed Sep 26 14:29:57 2018 +0200
     3.3 @@ -1588,7 +1588,7 @@
     3.4                          if (string.IsNullOrWhiteSpace(this._Bcc[i].Address) == false)
     3.5                          {
     3.6                              // Add by address
     3.7 -                            newRecipient = recipients.Add(this._Bcc[i].Address);
     3.8 +                            newRecipient = recipients.Add(this._Bcc[i].Address);                            
     3.9                              newRecipient.Type = (int)Outlook.OlMailRecipientType.olBCC;
    3.10  
    3.11                              // Marshal.ReleaseComObject(newRecipient);
    3.12 @@ -1633,7 +1633,7 @@
    3.13                          {
    3.14                              // Add by address
    3.15                              newRecipient = recipients.Add(this._To[i].Address);
    3.16 -                            newRecipient.Type = (int)Outlook.OlMailRecipientType.olTo;
    3.17 +                            newRecipient.Type = (int)Outlook.OlMailRecipientType.olTo;                           
    3.18  
    3.19                              // Marshal.ReleaseComObject(newRecipient);
    3.20                              newRecipient = null;