Mapi.cs
author Thomas
Tue, 04 Sep 2018 10:57:07 +0200
branchOUT-283
changeset 2354 e5b26ee3c384
parent 2353 81975e284b07
child 2368 53cbaf41e57c
permissions -rw-r--r--
Set SmartNoAttach MAPI property after decryption if needed
Thomas@2353
     1
´╗┐using System;
Thomas@2353
     2
using System.Runtime.InteropServices;
Thomas@2353
     3
using Outlook = Microsoft.Office.Interop.Outlook;
Thomas@2353
     4
Thomas@2353
     5
namespace pEp
Thomas@2353
     6
{
Thomas@2354
     7
    /// <summary>
Thomas@2354
     8
    /// Provides methods to interact with MAPI via native (p/invoke) calls.
Thomas@2354
     9
    /// </summary>
Thomas@2353
    10
    internal class Mapi
Thomas@2353
    11
    {
Thomas@2353
    12
        #region Definitions
Thomas@2353
    13
        /// <summary>
Thomas@2353
    14
        /// A collection of MAPI error or warning codes.
Thomas@2353
    15
        /// </summary>
Thomas@2353
    16
        internal static class HResult
Thomas@2353
    17
        {
Thomas@2353
    18
            public const uint S_FALSE                           = 0x00000001;
Thomas@2353
    19
            public const uint S_OK                              = 0x00000000;
Thomas@2353
    20
Thomas@2353
    21
            public const uint E_NOTIMPL                         = 0x80004001;
Thomas@2353
    22
Thomas@2353
    23
            public const uint MAPI_E_CALL_FAILED                = 0x80004005;
Thomas@2353
    24
            public const uint MAPI_E_NOT_ENOUGH_MEMORY          = 0x8007000E;
Thomas@2353
    25
            public const uint MAPI_E_INVALID_PARAMETER          = 0x80000003;
Thomas@2353
    26
            public const uint MAPI_E_INTERFACE_NOT_SUPPORTED    = 0x80000004;
Thomas@2353
    27
            public const uint MAPI_E_NO_ACCESS                  = 0x80000009;
Thomas@2353
    28
Thomas@2353
    29
            public const uint E_INVALIDARG                      = 0x80070057;
Thomas@2353
    30
            public const uint E_OUTOFMEMORY                     = 0x80000002;
Thomas@2353
    31
            public const uint E_UNEXPECTED                      = 0x8000FFFF;
Thomas@2353
    32
            public const uint E_FAIL                            = 0x80000008;
Thomas@2353
    33
Thomas@2353
    34
            public const uint MAPI_E_NO_SUPPORT                 = 0x80040000 | 0x102;
Thomas@2353
    35
            public const uint MAPI_E_BAD_CHARWIDTH              = 0x80040000 | 0x103;
Thomas@2353
    36
            public const uint MAPI_E_STRING_TOO_LONG            = 0x80040000 | 0x105;
Thomas@2353
    37
            public const uint MAPI_E_UNKNOWN_FLAGS              = 0x80040000 | 0x106;
Thomas@2353
    38
            public const uint MAPI_E_INVALID_ENTRYID            = 0x80040000 | 0x107;
Thomas@2353
    39
            public const uint MAPI_E_INVALID_OBJECT             = 0x80040000 | 0x108;
Thomas@2353
    40
            public const uint MAPI_E_OBJECT_CHANGED             = 0x80040000 | 0x109;
Thomas@2353
    41
            public const uint MAPI_E_OBJECT_DELETED             = 0x80040000 | 0x10A;
Thomas@2353
    42
            public const uint MAPI_E_BUSY                       = 0x80040000 | 0x10B;
Thomas@2353
    43
            public const uint MAPI_E_NOT_ENOUGH_DISK            = 0x80040000 | 0x10D;
Thomas@2353
    44
            public const uint MAPI_E_NOT_ENOUGH_RESOURCES       = 0x80040000 | 0x10E;
Thomas@2353
    45
            public const uint MAPI_E_NOT_FOUND                  = 0x80040000 | 0x10F;
Thomas@2353
    46
            public const uint MAPI_E_VERSION                    = 0x80040000 | 0x110;
Thomas@2353
    47
            public const uint MAPI_E_LOGON_FAILED               = 0x80040000 | 0x111;
Thomas@2353
    48
            public const uint MAPI_E_SESSION_LIMIT              = 0x80040000 | 0x112;
Thomas@2353
    49
            public const uint MAPI_E_USER_CANCEL                = 0x80040000 | 0x113;
Thomas@2353
    50
            public const uint MAPI_E_UNABLE_TO_ABORT            = 0x80040000 | 0x114;
Thomas@2353
    51
            public const uint MAPI_E_NETWORK_ERROR              = 0x80040000 | 0x115;
Thomas@2353
    52
            public const uint MAPI_E_DISK_ERROR                 = 0x80040000 | 0x116;
Thomas@2353
    53
            public const uint MAPI_E_TOO_COMPLEX                = 0x80040000 | 0x117;
Thomas@2353
    54
            public const uint MAPI_E_BAD_COLUMN                 = 0x80040000 | 0x118;
Thomas@2353
    55
            public const uint MAPI_E_EXTENDED_ERROR             = 0x80040000 | 0x119;
Thomas@2353
    56
            public const uint MAPI_E_COMPUTED                   = 0x80040000 | 0x11A;
Thomas@2353
    57
            public const uint MAPI_E_CORRUPT_DATA               = 0x80040000 | 0x11B;
Thomas@2353
    58
            public const uint MAPI_E_UNCONFIGURED               = 0x80040000 | 0x11C;
Thomas@2353
    59
            public const uint MAPI_E_FAILONEPROVIDER            = 0x80040000 | 0x11D;
Thomas@2353
    60
            public const uint MAPI_E_UNKNOWN_CPID               = 0x80040000 | 0x11E;
Thomas@2353
    61
            public const uint MAPI_E_UNKNOWN_LCID               = 0x80040000 | 0x11F;
Thomas@2353
    62
Thomas@2353
    63
            public const uint MAPI_E_CORRUPT_STORE              = 0x80040000 | 0x600;
Thomas@2353
    64
            public const uint MAPI_E_NOT_IN_QUEUE               = 0x80040000 | 0x601;
Thomas@2353
    65
            public const uint MAPI_E_NO_SUPPRESS                = 0x80040000 | 0x602;
Thomas@2353
    66
            public const uint MAPI_E_COLLISION                  = 0x80040000 | 0x604;
Thomas@2353
    67
            public const uint MAPI_E_NOT_INITIALIZED            = 0x80040000 | 0x605;
Thomas@2353
    68
            public const uint MAPI_E_NON_STANDARD               = 0x80040000 | 0x606;
Thomas@2353
    69
            public const uint MAPI_E_NO_RECIPIENTS              = 0x80040000 | 0x607;
Thomas@2353
    70
            public const uint MAPI_E_SUBMITTED                  = 0x80040000 | 0x608;
Thomas@2353
    71
            public const uint MAPI_E_HAS_FOLDERS                = 0x80040000 | 0x609;
Thomas@2353
    72
            public const uint MAPI_E_HAS_MESSAGES               = 0x80040000 | 0x60A;
Thomas@2353
    73
            public const uint MAPI_E_FOLDER_CYCLE               = 0x80040000 | 0x60B;
Thomas@2353
    74
        };
Thomas@2353
    75
Thomas@2353
    76
        /// <summary>
Thomas@2353
    77
        /// Interface IDs used to retrieve the specific MAPI Interfaces from the IUnknown object.
Thomas@2353
    78
        /// </summary>
Thomas@2353
    79
        internal static class MAPIInterfaceIds
Thomas@2353
    80
        {
Thomas@2353
    81
            public const string IMAPISession        = "00020300-0000-0000-C000-000000000046";
Thomas@2353
    82
            public const string IMAPIProp           = "00020303-0000-0000-C000-000000000046";
Thomas@2353
    83
            public const string IMAPITable          = "00020301-0000-0000-C000-000000000046";
Thomas@2353
    84
            public const string IMAPIMsgStore       = "00020306-0000-0000-C000-000000000046";
Thomas@2353
    85
            public const string IMAPIFolder         = "0002030C-0000-0000-C000-000000000046";
Thomas@2353
    86
            public const string IMAPISpoolerService = "0002031E-0000-0000-C000-000000000046";
Thomas@2353
    87
            public const string IMAPIStatus         = "0002031E-0000-0000-C000-000000000046";
Thomas@2353
    88
            public const string IMessage            = "00020307-0000-0000-C000-000000000046";
Thomas@2353
    89
            public const string IAddrBook           = "00020309-0000-0000-C000-000000000046";
Thomas@2353
    90
            public const string IProfSect           = "00020304-0000-0000-C000-000000000046";
Thomas@2353
    91
            public const string IMAPIContainer      = "0002030B-0000-0000-C000-000000000046";
Thomas@2353
    92
            public const string IABContainer        = "0002030D-0000-0000-C000-000000000046";
Thomas@2353
    93
            public const string IMsgServiceAdmin    = "0002031D-0000-0000-C000-000000000046";
Thomas@2353
    94
            public const string IProfAdmin          = "0002031C-0000-0000-C000-000000000046";
Thomas@2353
    95
            public const string IMailUser           = "0002030A-0000-0000-C000-000000000046";
Thomas@2353
    96
            public const string IDistList           = "0002030E-0000-0000-C000-000000000046";
Thomas@2353
    97
            public const string IAttachment         = "00020308-0000-0000-C000-000000000046";
Thomas@2353
    98
            public const string IMAPIControl        = "0002031B-0000-0000-C000-000000000046";
Thomas@2353
    99
            public const string IMAPILogonRemote    = "00020346-0000-0000-C000-000000000046";
Thomas@2353
   100
            public const string IMAPIForm           = "00020327-0000-0000-C000-000000000046";
Thomas@2353
   101
        }
Thomas@2353
   102
Thomas@2353
   103
        /// <summary>
Thomas@2353
   104
        /// Save options for the IMAPIProp.SaveChanges method.
Thomas@2353
   105
        /// </summary>
Thomas@2353
   106
        internal static class SaveOptions
Thomas@2353
   107
        {
Thomas@2353
   108
            public const int KEEP_OPEN_READONLY     = 0x00000001;
Thomas@2353
   109
            public const int KEEP_OPEN_READWRITE    = 0x00000002;
Thomas@2353
   110
            public const int FORCE_SAVE             = 0x00000004;
Thomas@2353
   111
        }
Thomas@2353
   112
        #endregion
Thomas@2353
   113
Thomas@2353
   114
        #region Interfaces
Thomas@2353
   115
        /// <summary>
Thomas@2353
   116
        /// Enables clients, service providers, and MAPI to work with properties. All objects that 
Thomas@2353
   117
        /// support properties implement this interface.
Thomas@2353
   118
        /// </summary>
Thomas@2353
   119
        [
Thomas@2353
   120
            ComImport,
Thomas@2353
   121
            ComVisible(false),
Thomas@2353
   122
            InterfaceType(ComInterfaceType.InterfaceIsIUnknown),
Thomas@2353
   123
            Guid(Mapi.MAPIInterfaceIds.IMAPIProp)
Thomas@2353
   124
        ]
Thomas@2353
   125
        internal interface IMAPIProp
Thomas@2353
   126
        {
Thomas@2353
   127
            [return: MarshalAs(UnmanagedType.I4)]
Thomas@2353
   128
            [PreserveSig]
Thomas@2353
   129
            int GetLastError();
Thomas@2353
   130
            [return: MarshalAs(UnmanagedType.I4)]
Thomas@2353
   131
            [PreserveSig]
Thomas@2353
   132
            int SaveChanges(uint uFlags);
Thomas@2353
   133
            [return: MarshalAs(UnmanagedType.I4)]
Thomas@2353
   134
            [PreserveSig]
Thomas@2353
   135
            int GetProps();
Thomas@2353
   136
            [return: MarshalAs(UnmanagedType.I4)]
Thomas@2353
   137
            [PreserveSig]
Thomas@2353
   138
            int GetPropList();
Thomas@2353
   139
            [return: MarshalAs(UnmanagedType.I4)]
Thomas@2353
   140
            [PreserveSig]
Thomas@2353
   141
            int OpenProperty();
Thomas@2353
   142
            [return: MarshalAs(UnmanagedType.I4)]
Thomas@2353
   143
            [PreserveSig]
Thomas@2353
   144
            int SetProps(uint values, IntPtr propArray, IntPtr problems);
Thomas@2353
   145
            [return: MarshalAs(UnmanagedType.I4)]
Thomas@2353
   146
            [PreserveSig]
Thomas@2353
   147
            int DeleteProps();
Thomas@2353
   148
            [return: MarshalAs(UnmanagedType.I4)]
Thomas@2353
   149
            [PreserveSig]
Thomas@2353
   150
            int CopyTo();
Thomas@2353
   151
            [return: MarshalAs(UnmanagedType.I4)]
Thomas@2353
   152
            [PreserveSig]
Thomas@2353
   153
            int CopyProps();
Thomas@2353
   154
            [return: MarshalAs(UnmanagedType.I4)]
Thomas@2353
   155
            [PreserveSig]
Thomas@2353
   156
            int GetNamesFromIDs();
Thomas@2353
   157
            [return: MarshalAs(UnmanagedType.I4)]
Thomas@2353
   158
            [PreserveSig]
Thomas@2353
   159
            int GetIDsFromNames();
Thomas@2353
   160
        }
Thomas@2353
   161
        #endregion
Thomas@2353
   162
Thomas@2353
   163
        #region Structures
Thomas@2353
   164
Thomas@2353
   165
        /// <summary>
Thomas@2353
   166
        /// The SPropValue structure describes a MAPI property.
Thomas@2353
   167
        /// </summary>
Thomas@2353
   168
        public struct SPropValue
Thomas@2353
   169
        {
Thomas@2353
   170
            /// <summary>
Thomas@2353
   171
            /// Property tag for the property. Property tags are 32-bit unsigned integers consisting of the property's unique identifier in the high-order 16 bits and the property's type in the low-order 16 bits.
Thomas@2353
   172
            /// </summary>
Thomas@2353
   173
            public uint PropTag;
Thomas@2353
   174
Thomas@2353
   175
            /// <summary>
Thomas@2353
   176
            /// Reserved for MAPI; do not use.
Thomas@2353
   177
            /// </summary>
Thomas@2353
   178
            public uint DwAlignPad;
Thomas@2353
   179
Thomas@2353
   180
            /// <summary>
Thomas@2353
   181
            /// Union of data values, the specific value dictated by the property type.
Thomas@2353
   182
            /// </summary>
Thomas@2353
   183
            public PV Value;
Thomas@2353
   184
        }
Thomas@2353
   185
Thomas@2353
   186
        /// <summary>
Thomas@2353
   187
        /// Union of data values for the SPropValue.value property.
Thomas@2353
   188
        /// </summary>
Thomas@2353
   189
        [StructLayout(LayoutKind.Explicit, Size = 8)]
Thomas@2353
   190
        public struct PV
Thomas@2353
   191
        {
Thomas@2353
   192
            [FieldOffset(0)]
Thomas@2353
   193
            public short i;
Thomas@2353
   194
            [FieldOffset(0)]
Thomas@2353
   195
            public int l;
Thomas@2353
   196
            [FieldOffset(0)]
Thomas@2353
   197
            public uint ul;
Thomas@2353
   198
            [FieldOffset(0)]
Thomas@2353
   199
            public float flt;
Thomas@2353
   200
            [FieldOffset(0)]
Thomas@2353
   201
            public double dbl;
Thomas@2353
   202
            [FieldOffset(0)]
Thomas@2353
   203
            public ushort b;
Thomas@2353
   204
            [FieldOffset(0)]
Thomas@2353
   205
            public double at;
Thomas@2353
   206
            [FieldOffset(0)]
Thomas@2353
   207
            public IntPtr lpszA;
Thomas@2353
   208
            [FieldOffset(0)]
Thomas@2353
   209
            public IntPtr lpszW;
Thomas@2353
   210
            [FieldOffset(0)]
Thomas@2353
   211
            public IntPtr lpguid;
Thomas@2353
   212
            /*[FieldOffset(0)]
Thomas@2353
   213
            public IntPtr bin;*/
Thomas@2353
   214
            [FieldOffset(0)]
Thomas@2353
   215
            public ulong li;
Thomas@2353
   216
            [FieldOffset(0)]
Thomas@2353
   217
            public SRowSet bin;
Thomas@2353
   218
        }
Thomas@2353
   219
Thomas@2353
   220
        /// <summary>
Thomas@2353
   221
        /// Contains an array of SRow structures. Each SRow structure describes a row from a table.
Thomas@2353
   222
        /// </summary>
Thomas@2353
   223
        [StructLayout(LayoutKind.Sequential)]
Thomas@2353
   224
        public struct SRowSet
Thomas@2353
   225
        {
Thomas@2353
   226
            public uint cRows;
Thomas@2353
   227
            public IntPtr aRow; // pSRow
Thomas@2353
   228
Thomas@2353
   229
            public byte[] AsBytes
Thomas@2353
   230
            {
Thomas@2353
   231
                get
Thomas@2353
   232
                {
Thomas@2353
   233
                    byte[] b = new byte[this.cRows];
Thomas@2353
   234
                    for (int i = 0; i < this.cRows; i++)
Thomas@2353
   235
                        b[i] = Marshal.ReadByte(aRow, i);
Thomas@2353
   236
                    return b;
Thomas@2353
   237
                }
Thomas@2353
   238
            }
Thomas@2353
   239
        }
Thomas@2353
   240
        #endregion
Thomas@2353
   241
Thomas@2353
   242
        #region Methods
Thomas@2353
   243
        /// <summary>
Thomas@2353
   244
        /// Sets the given MAPI properties on the Outlook mail item.
Thomas@2353
   245
        /// Note: the caller has to make sure that the count of MAPI properties
Thomas@2353
   246
        /// and respective values matches and that the given values match the
Thomas@2353
   247
        /// required data type for each MAPI property.
Thomas@2354
   248
        /// Do not call this method from a background thread!
Thomas@2353
   249
        /// </summary>
Thomas@2353
   250
        /// <param name="omi">The Outlook mail item to set its properties.</param>
Thomas@2353
   251
        /// <param name="mapiProperties">The MAPI properties to set.</param>
Thomas@2353
   252
        /// <param name="values">The values to set.</param>
Thomas@2353
   253
        /// <returns>True if the method succeeds, otherwise false.</returns>
Thomas@2353
   254
        public static bool SetMAPIProperties(Outlook.MailItem omi, MapiProperty.MapiProp[] mapiProperties, object[] values)
Thomas@2353
   255
        {
Thomas@2353
   256
            // The return status of this method
Thomas@2353
   257
            bool success = false;
Thomas@2353
   258
Thomas@2353
   259
            // Pointer to IUnknown interface
Thomas@2353
   260
            IntPtr IUnknown = IntPtr.Zero;
Thomas@2353
   261
Thomas@2353
   262
            // Pointer to the MAPI properties array
Thomas@2353
   263
            IntPtr propArray = IntPtr.Zero;
Thomas@2353
   264
Thomas@2353
   265
            // Pointer to the value
Thomas@2353
   266
            IntPtr valuePtr = IntPtr.Zero;
Thomas@2353
   267
Thomas@2353
   268
            // Get MAPI object from mail item
Thomas@2353
   269
            object mapiObject = omi?.MAPIOBJECT;
Thomas@2353
   270
            if (mapiObject == null)
Thomas@2353
   271
            {
Thomas@2353
   272
                Log.Error("SetMAPIProperties: MAPI object is null. Property could not be set.");
Thomas@2353
   273
                return success;
Thomas@2353
   274
            }
Thomas@2353
   275
Thomas@2353
   276
            // Make sure the properties count matches the values count
Thomas@2353
   277
            int propCount = mapiProperties.Length;
Thomas@2353
   278
            if (propCount != values.Length)
Thomas@2353
   279
            {
Thomas@2353
   280
                Log.Error("SetMAPIProperties: Mismatch between tag count and value count.");
Thomas@2353
   281
                return success;
Thomas@2353
   282
            }
Thomas@2353
   283
Thomas@2353
   284
            try
Thomas@2353
   285
            {
Thomas@2353
   286
                // Initialize MAPI
Thomas@2353
   287
                Mapi.MAPIInitialize(IntPtr.Zero);
Thomas@2353
   288
Thomas@2353
   289
                // Get the IUnknown interface from the MAPI object
Thomas@2353
   290
                IUnknown = Marshal.GetIUnknownForObject(mapiObject);
Thomas@2353
   291
Thomas@2353
   292
                // Get the IMAPIProp interface
Thomas@2353
   293
                Mapi.IMAPIProp IMAPIProp = (Mapi.IMAPIProp)Marshal.GetTypedObjectForIUnknown(IUnknown, typeof(Mapi.IMAPIProp));
Thomas@2353
   294
Thomas@2353
   295
                // Create the SPropValue structure
Thomas@2353
   296
                Mapi.SPropValue sPropValue = new Mapi.SPropValue();
Thomas@2353
   297
                var propValueSize = Marshal.SizeOf(sPropValue);
Thomas@2353
   298
Thomas@2353
   299
                // Allocate memory for the array of SPropValues to set
Thomas@2353
   300
                propArray = Marshal.AllocHGlobal(propValueSize * propCount);
Thomas@2353
   301
Thomas@2353
   302
                // Create the property values array
Thomas@2353
   303
                for (int i = 0; i < propCount; i++)
Thomas@2353
   304
                {
Thomas@2353
   305
                    // Get the current property/value pair
Thomas@2353
   306
                    MapiProperty.MapiProp mapiProperty = mapiProperties[i];
Thomas@2353
   307
                    uint tag = mapiProperty.Tag;
Thomas@2353
   308
                    object value = values[i];
Thomas@2353
   309
Thomas@2353
   310
                    // Set the property value's property tag
Thomas@2353
   311
                    sPropValue.PropTag = (uint)tag;
Thomas@2353
   312
Thomas@2353
   313
                    // Set the property value's value
Thomas@2353
   314
                    switch (mapiProperty.DataType)
Thomas@2353
   315
                    {
Thomas@2353
   316
                        case MapiProperty.MapiDataType.PtypBoolean:
Thomas@2353
   317
                            {
Thomas@2353
   318
                                try
Thomas@2353
   319
                                {
Thomas@2353
   320
                                    sPropValue.Value.b = (ushort)Convert.ToInt16((bool)value);
Thomas@2353
   321
                                }
Thomas@2353
   322
                                catch (Exception ex)
Thomas@2353
   323
                                {
Thomas@2353
   324
                                    throw new Exception(string.Format("Error converting to Boolean. Property tag: {0}. Value: {1}. Exception: {2}.", mapiProperty.DaslName, value?.ToString(), ex.ToString()));
Thomas@2353
   325
                                }
Thomas@2353
   326
                            }
Thomas@2353
   327
                            break;
Thomas@2353
   328
                        case MapiProperty.MapiDataType.PtypString:
Thomas@2353
   329
                            {
Thomas@2353
   330
                                try
Thomas@2353
   331
                                {
Thomas@2353
   332
                                    valuePtr = Marshal.StringToHGlobalUni(value as string);
Thomas@2353
   333
                                    sPropValue.Value.lpszW = valuePtr;
Thomas@2353
   334
                                }
Thomas@2353
   335
                                catch (Exception ex)
Thomas@2353
   336
                                {
Thomas@2353
   337
                                    throw new Exception(string.Format("Error converting to String. Property tag: {0}. Value: {1}. Exception: {2}.", mapiProperty.DaslName, value?.ToString(), ex.ToString()));
Thomas@2353
   338
                                }
Thomas@2353
   339
                            }
Thomas@2353
   340
                            break;
Thomas@2353
   341
                        case MapiProperty.MapiDataType.PtypString8:
Thomas@2353
   342
                            {
Thomas@2353
   343
                                try
Thomas@2353
   344
                                {
Thomas@2353
   345
                                    valuePtr = Marshal.StringToHGlobalAnsi(value as string);
Thomas@2353
   346
                                    sPropValue.Value.lpszA = valuePtr;
Thomas@2353
   347
                                }
Thomas@2353
   348
                                catch (Exception ex)
Thomas@2353
   349
                                {
Thomas@2353
   350
                                    throw new Exception(string.Format("Error converting to String8. Property tag: {0}. Value: {1}. Exception: {2}.", mapiProperty.DaslName, value?.ToString(), ex.ToString()));
Thomas@2353
   351
                                }
Thomas@2353
   352
                            }
Thomas@2353
   353
                            break;
Thomas@2353
   354
                        case MapiProperty.MapiDataType.PtypInteger16:
Thomas@2353
   355
                            {
Thomas@2353
   356
                                try
Thomas@2353
   357
                                {
Thomas@2353
   358
                                    sPropValue.Value.i = (short)value;
Thomas@2353
   359
                                }
Thomas@2353
   360
                                catch (Exception ex)
Thomas@2353
   361
                                {
Thomas@2353
   362
                                    throw new Exception(string.Format("Error converting to Short. Property tag: {0}. Value: {1}. Exception: {2}.", mapiProperty.DaslName, value?.ToString(), ex.ToString()));
Thomas@2353
   363
                                }
Thomas@2353
   364
                            }
Thomas@2353
   365
                            break;
Thomas@2353
   366
                        case MapiProperty.MapiDataType.PtypInteger32:
Thomas@2353
   367
                            {
Thomas@2353
   368
                                try
Thomas@2353
   369
                                {
Thomas@2353
   370
                                    sPropValue.Value.l = (int)value;
Thomas@2353
   371
                                }
Thomas@2353
   372
                                catch (Exception ex)
Thomas@2353
   373
                                {
Thomas@2353
   374
                                    throw new Exception(string.Format("Error converting to Integer. Property tag: {0}. Value: {1}. Exception: {2}.", mapiProperty.DaslName, value?.ToString(), ex.ToString()));
Thomas@2353
   375
                                }
Thomas@2353
   376
                            }
Thomas@2353
   377
                            break;
Thomas@2353
   378
                        case MapiProperty.MapiDataType.PtypFloating32:
Thomas@2353
   379
                            {
Thomas@2353
   380
                                try
Thomas@2353
   381
                                {
Thomas@2353
   382
                                    sPropValue.Value.flt = (float)value;
Thomas@2353
   383
                                }
Thomas@2353
   384
                                catch (Exception ex)
Thomas@2353
   385
                                {
Thomas@2353
   386
                                    throw new Exception(string.Format("Error converting to Float. Property tag: {0}. Value: {1}. Exception: {2}.", mapiProperty.DaslName, value?.ToString(), ex.ToString()));
Thomas@2353
   387
                                }
Thomas@2353
   388
                            }
Thomas@2353
   389
                            break;
Thomas@2353
   390
                        case MapiProperty.MapiDataType.PtypFloating64:
Thomas@2353
   391
                        case MapiProperty.MapiDataType.PtypFloatingTime:
Thomas@2353
   392
                            {
Thomas@2353
   393
                                try
Thomas@2353
   394
                                {
Thomas@2353
   395
                                    sPropValue.Value.at = (double)value;
Thomas@2353
   396
                                }
Thomas@2353
   397
                                catch (Exception ex)
Thomas@2353
   398
                                {
Thomas@2353
   399
                                    throw new Exception(string.Format("Error converting to Double. Property tag: {0}. Value: {1}. Exception: {2}.", mapiProperty.DaslName, value?.ToString(), ex.ToString()));
Thomas@2353
   400
                                }
Thomas@2353
   401
                            }
Thomas@2353
   402
                            break;
Thomas@2353
   403
                        case MapiProperty.MapiDataType.PtypInteger64:
Thomas@2353
   404
                            {
Thomas@2353
   405
                                try
Thomas@2353
   406
                                {
Thomas@2353
   407
                                    sPropValue.Value.li = (ulong)value;
Thomas@2353
   408
                                }
Thomas@2353
   409
                                catch (Exception ex)
Thomas@2353
   410
                                {
Thomas@2353
   411
                                    throw new Exception(string.Format("Error converting to Ulong. Property tag: {0}. Value: {1}. Exception: {2}.", mapiProperty.DaslName, value?.ToString(), ex.ToString()));
Thomas@2353
   412
                                }
Thomas@2353
   413
                            }
Thomas@2353
   414
                            break;
Thomas@2353
   415
                        case MapiProperty.MapiDataType.PtypGuid:
Thomas@2353
   416
                            {
Thomas@2353
   417
                                try
Thomas@2353
   418
                                {
Thomas@2353
   419
                                    IntPtr guidPtr = Marshal.AllocHGlobal(Marshal.SizeOf(value));
Thomas@2353
   420
                                    Marshal.StructureToPtr((Guid)value, guidPtr, false);
Thomas@2353
   421
                                    sPropValue.Value.lpguid = guidPtr;
Thomas@2353
   422
                                }
Thomas@2353
   423
                                catch (Exception ex)
Thomas@2353
   424
                                {
Thomas@2353
   425
                                    throw new Exception(string.Format("Error converting to Guid. Property tag: {0}. Value: {1}. Exception: {2}.", mapiProperty.DaslName, value?.ToString(), ex.ToString()));
Thomas@2353
   426
                                }
Thomas@2353
   427
                            }
Thomas@2353
   428
                            break;
Thomas@2353
   429
                        case MapiProperty.MapiDataType.PtypBinary:
Thomas@2353
   430
                            {
Thomas@2353
   431
                                try
Thomas@2353
   432
                                {
Thomas@2353
   433
                                    sPropValue.Value.bin = (Mapi.SRowSet)value;
Thomas@2353
   434
                                }
Thomas@2353
   435
                                catch (Exception ex)
Thomas@2353
   436
                                {
Thomas@2353
   437
                                    throw new Exception(string.Format("Error converting to Binary. Property tag: {0}. Value: {1}. Exception: {2}.", mapiProperty.DaslName, value?.ToString(), ex.ToString()));
Thomas@2353
   438
                                }
Thomas@2353
   439
                            }
Thomas@2353
   440
                            break;
Thomas@2353
   441
                        default:
Thomas@2353
   442
                            {
Thomas@2353
   443
                                throw new Exception(string.Format("Error creating SPropValue. Data type {0} not supported.", Enum.GetName(typeof(MapiProperty.MapiDataType), mapiProperty.DataType)));
Thomas@2353
   444
                            }
Thomas@2353
   445
                    }
Thomas@2353
   446
                    Marshal.StructureToPtr(sPropValue, (IntPtr)((uint)propArray + (propValueSize * i)), false);
Thomas@2353
   447
Thomas@2353
   448
                    if (valuePtr != IntPtr.Zero)
Thomas@2353
   449
                    {
Thomas@2353
   450
                        Marshal.FreeHGlobal(valuePtr);
Thomas@2353
   451
                    }
Thomas@2353
   452
                }
Thomas@2353
   453
Thomas@2353
   454
                // Set properties and save
Thomas@2353
   455
                IntPtr problems = IntPtr.Zero;
Thomas@2353
   456
                if (IMAPIProp.SetProps((uint)propCount, propArray, problems) == Mapi.HResult.S_OK)
Thomas@2353
   457
                {
Thomas@2353
   458
                    success = (IMAPIProp.SaveChanges(Mapi.SaveOptions.KEEP_OPEN_READWRITE) == Mapi.HResult.S_OK);
Thomas@2353
   459
                }
Thomas@2353
   460
Thomas@2353
   461
                // Log problems
Thomas@2353
   462
                if (problems != IntPtr.Zero)
Thomas@2353
   463
                {
Thomas@2353
   464
Thomas@2353
   465
                }
Thomas@2353
   466
            }
Thomas@2353
   467
            catch (Exception ex)
Thomas@2353
   468
            {
Thomas@2353
   469
                Log.Error("SetMAPIProperties: Error occured. " + ex.ToString());
Thomas@2353
   470
                success = false;
Thomas@2353
   471
            }
Thomas@2353
   472
            finally
Thomas@2353
   473
            {
Thomas@2353
   474
                if (IUnknown != IntPtr.Zero)
Thomas@2353
   475
                {
Thomas@2353
   476
                    Marshal.Release(IUnknown);
Thomas@2353
   477
                }
Thomas@2353
   478
Thomas@2353
   479
                if (propArray != IntPtr.Zero)
Thomas@2353
   480
                {
Thomas@2353
   481
                    Marshal.FreeHGlobal(propArray);
Thomas@2353
   482
                }
Thomas@2353
   483
Thomas@2353
   484
                if (valuePtr != IntPtr.Zero)
Thomas@2353
   485
                {
Thomas@2353
   486
                    Marshal.FreeHGlobal(valuePtr);
Thomas@2353
   487
                }
Thomas@2353
   488
Thomas@2353
   489
                Mapi.MAPIUninitialize();
Thomas@2353
   490
            }
Thomas@2353
   491
Thomas@2353
   492
            return success;
Thomas@2353
   493
        }
Thomas@2353
   494
Thomas@2353
   495
        /// <summary>
Thomas@2353
   496
        /// Sets the given MAPI property on the Outlook mail item.
Thomas@2353
   497
        /// Note: the caller has to make sure that the given value matches the
Thomas@2353
   498
        /// required data type for the MAPI property.
Thomas@2354
   499
        /// Do not call this method from a background thread!
Thomas@2353
   500
        /// </summary>
Thomas@2353
   501
        /// <param name="omi">The Outlook mail item to set the property for.</param>
Thomas@2353
   502
        /// <param name="mapiProperties">The MAPI property to set.</param>
Thomas@2353
   503
        /// <param name="values">The value to set.</param>
Thomas@2354
   504
        /// <param name="useSetProps">Whether to use the IMAPIProp.SetProps method (instead of HrSetOneProp).</param>
Thomas@2353
   505
        /// <returns>True if the method succeeds, otherwise false.</returns>
Thomas@2354
   506
        public static bool SetMAPIProperty(Outlook.MailItem omi, 
Thomas@2354
   507
                                           MapiProperty.MapiProp mapiProperty, 
Thomas@2354
   508
                                           object value, 
Thomas@2354
   509
                                           bool useSetProps = false)
Thomas@2353
   510
        {
Thomas@2354
   511
            if (useSetProps)
Thomas@2354
   512
            {
Thomas@2354
   513
                return Mapi.SetMAPIProperties(omi, new MapiProperty.MapiProp[] { mapiProperty }, new object[] { value });
Thomas@2354
   514
            }
Thomas@2354
   515
Thomas@2353
   516
            // The return status of this method
Thomas@2353
   517
            bool success = false;
Thomas@2353
   518
Thomas@2353
   519
            // Pointer to IUnknown interface
Thomas@2353
   520
            IntPtr IUnknown = IntPtr.Zero;
Thomas@2353
   521
Thomas@2353
   522
            // Pointer to IMessage interface
Thomas@2353
   523
            IntPtr IMessage = IntPtr.Zero;
Thomas@2353
   524
Thomas@2353
   525
            // Pointer to IMAPIProp interface
Thomas@2353
   526
            IntPtr IMAPIProp = IntPtr.Zero;
Thomas@2353
   527
Thomas@2353
   528
            // Structure that will hold the property value
Thomas@2353
   529
            Mapi.SPropValue propValue;
Thomas@2353
   530
Thomas@2353
   531
            // A pointer that points to the SPropValue structure 
Thomas@2353
   532
            IntPtr ptrPropValue = IntPtr.Zero;
Thomas@2353
   533
Thomas@2353
   534
            // Get MAPI object from mail item
Thomas@2353
   535
            object mapiObject = omi?.MAPIOBJECT;
Thomas@2353
   536
            if (mapiObject == null)
Thomas@2353
   537
            {
Thomas@2353
   538
                Log.Error("SetMAPIProperty: MAPI object is null. Property could not be set.");
Thomas@2353
   539
                return success;
Thomas@2353
   540
            }
Thomas@2353
   541
Thomas@2353
   542
            try
Thomas@2353
   543
            {
Thomas@2353
   544
                // Initialize MAPI
Thomas@2353
   545
                Mapi.MAPIInitialize(IntPtr.Zero);
Thomas@2353
   546
Thomas@2353
   547
                // Get the IUnknown interface from the MAPI object
Thomas@2353
   548
                IUnknown = Marshal.GetIUnknownForObject(mapiObject);
Thomas@2353
   549
Thomas@2353
   550
                // Set the IMessage interface GUID that we pass to retrieve the IMessage interface.
Thomas@2353
   551
                Guid guidIMessage = new Guid(Mapi.MAPIInterfaceIds.IMessage);
Thomas@2353
   552
Thomas@2353
   553
                // Try to retrieve the IMessage interface
Thomas@2353
   554
                if (Marshal.QueryInterface(IUnknown, ref guidIMessage, out IMessage) != Mapi.HResult.S_OK)
Thomas@2353
   555
                {
Thomas@2353
   556
                    Log.Error("SetMAPIProperty: Could not retrieve IMessage interface. Property could not be set.");
Thomas@2353
   557
                    return success;
Thomas@2353
   558
                }
Thomas@2353
   559
Thomas@2353
   560
                // Set the IMAPIProp interface GUID that we pass to retrieve the IMAPIProp Interface.
Thomas@2353
   561
                Guid guidIMAPIProp = new Guid(Mapi.MAPIInterfaceIds.IMAPIProp);
Thomas@2353
   562
Thomas@2353
   563
                // Try to retrieve the IMAPIProp interface
Thomas@2353
   564
                if ((Marshal.QueryInterface(IMessage, ref guidIMAPIProp, out IMAPIProp) != Mapi.HResult.S_OK) ||
Thomas@2353
   565
                    (IMAPIProp == IntPtr.Zero))
Thomas@2353
   566
                {
Thomas@2353
   567
                    Log.Error("SetMAPIProperty: Could not retrieve IMAPIProp interface. Property could not be set.");
Thomas@2353
   568
                    return success;
Thomas@2353
   569
                }
Thomas@2353
   570
Thomas@2353
   571
                // Create the SPropValue structure
Thomas@2353
   572
                propValue = new Mapi.SPropValue
Thomas@2353
   573
                {
Thomas@2353
   574
                    PropTag = mapiProperty.Tag
Thomas@2353
   575
                };
Thomas@2353
   576
                propValue.Value.li = (bool)value ? 1UL : 0UL;
Thomas@2353
   577
Thomas@2353
   578
                // Get pointer to property value structure
Thomas@2353
   579
                ptrPropValue = Marshal.AllocHGlobal(Marshal.SizeOf(propValue));
Thomas@2353
   580
                Marshal.StructureToPtr(propValue, ptrPropValue, false);
Thomas@2353
   581
Thomas@2353
   582
                // Try to set the property
Thomas@2353
   583
                Mapi.HrSetOneProp(IMAPIProp, ptrPropValue);
Thomas@2353
   584
Thomas@2353
   585
                // Save changes
Thomas@2353
   586
                Mapi.IMAPIProp mapiProp = (Mapi.IMAPIProp)Marshal.GetTypedObjectForIUnknown(IUnknown, typeof(Mapi.IMAPIProp));
Thomas@2353
   587
                success = (mapiProp.SaveChanges(Mapi.SaveOptions.KEEP_OPEN_READWRITE) == Mapi.HResult.S_OK);
Thomas@2353
   588
            }
Thomas@2353
   589
            catch (Exception ex)
Thomas@2353
   590
            {
Thomas@2353
   591
                Log.Error("SetMAPIProperty: Error occured. " + ex.ToString());
Thomas@2353
   592
                success = false;
Thomas@2353
   593
            }
Thomas@2353
   594
            finally
Thomas@2353
   595
            {
Thomas@2353
   596
                // Free used memory structures
Thomas@2353
   597
                if (ptrPropValue != IntPtr.Zero)
Thomas@2353
   598
                {
Thomas@2353
   599
                    Mapi.MAPIFreeBuffer(ptrPropValue);
Thomas@2353
   600
                }
Thomas@2353
   601
Thomas@2353
   602
                // Clean up all references to COM Objects
Thomas@2353
   603
                if (IMAPIProp != IntPtr.Zero)
Thomas@2353
   604
                {
Thomas@2353
   605
                    Marshal.Release(IMAPIProp);
Thomas@2353
   606
                }
Thomas@2353
   607
Thomas@2353
   608
                if (IMessage != IntPtr.Zero)
Thomas@2353
   609
                {
Thomas@2353
   610
                    Marshal.Release(IMessage);
Thomas@2353
   611
                }
Thomas@2353
   612
Thomas@2353
   613
                if (IUnknown != IntPtr.Zero)
Thomas@2353
   614
                {
Thomas@2353
   615
                    Marshal.Release(IUnknown);
Thomas@2353
   616
                }
Thomas@2353
   617
Thomas@2353
   618
                Mapi.MAPIUninitialize();
Thomas@2353
   619
            }
Thomas@2353
   620
Thomas@2353
   621
            return success;
Thomas@2353
   622
        }
Thomas@2353
   623
        #endregion
Thomas@2353
   624
Thomas@2353
   625
        #region MAPI DLL Imports
Thomas@2353
   626
        /// <summary>
Thomas@2354
   627
        /// Increments the MAPI subsystem reference count and initializes global data for the MAPI DLL.
Thomas@2353
   628
        /// </summary>
Thomas@2354
   629
        /// <param name="mapiInit">Pointer to a MAPIINIT_0 structure. The mapiInit parameter can be set to IntPtr.Zero.</param>
Thomas@2354
   630
        /// <returns>The status of this method.</returns>
Thomas@2353
   631
        [DllImport("mapi32.DLL", CharSet = CharSet.Ansi)]
Thomas@2354
   632
        internal static extern int MAPIInitialize(IntPtr mapiInit);
Thomas@2353
   633
Thomas@2353
   634
        /// <summary>
Thomas@2354
   635
        /// Decrements the reference count, cleans up, and deletes per-instance global data for the MAPI DLL.
Thomas@2353
   636
        /// </summary>
Thomas@2353
   637
        [DllImport("mapi32.DLL", CharSet = CharSet.Ansi)]
Thomas@2353
   638
        internal static extern void MAPIUninitialize();
Thomas@2353
   639
Thomas@2353
   640
        /// <summary>
Thomas@2354
   641
        /// Retrieves the value of a single property from a property interface, that is, an interface derived from IMAPIProp.
Thomas@2353
   642
        /// </summary>
Thomas@2354
   643
        /// <param name="iMAPIProp">Pointer to the IMAPIProp interface from which the property value is to be retrieved.</param>
Thomas@2354
   644
        /// <param name="propTag">Property tag of the property to be retrieved.</param>
Thomas@2354
   645
        /// <param name="propertyValue">Pointer to a pointer to the returned SPropValue structure defining the retrieved property value.</param>
Thomas@2353
   646
        /// <remarks>
Thomas@2354
   647
        /// Unlike the IMAPIProp.GetProps method, the HrGetOneProp function never returns any warning.
Thomas@2353
   648
        /// Because it retrieves only one property, it simply either succeeds or fails. For retrieving multiple properties,
Thomas@2354
   649
        /// GetProps is faster.
Thomas@2353
   650
        /// </remarks>
Thomas@2353
   651
        [DllImport("mapi32.DLL", CharSet = CharSet.Ansi, EntryPoint = "HrGetOneProp@12")]
Thomas@2354
   652
        internal static extern void HrGetOneProp(IntPtr iMAPIProp, uint propTag, out IntPtr propertyValue);
Thomas@2353
   653
Thomas@2353
   654
        /// <summary>
Thomas@2354
   655
        /// Sets or changes the value of a single property on a property interface, that is, an interface derived from IMAPIProp.
Thomas@2353
   656
        /// </summary>
Thomas@2354
   657
        /// <param name="iMAPIProp">Pointer to an IMAPIProp interface on which the property value is to be set or changed.</param>
Thomas@2354
   658
        /// <param name="propertyValue">[in] Pointer to the SPropValue structure defining the property to be set or changed.</param>
Thomas@2353
   659
        /// <remarks>
Thomas@2353
   660
        /// Unlike the IMAPIProp::SetProps method, the HrSetOneProp function never returns any warning.
Thomas@2353
   661
        /// Because it sets only one property, it simply either succeeds or fails.
Thomas@2353
   662
        /// For setting or changing multiple properties, SetProps is faster. 
Thomas@2353
   663
        /// </remarks>
Thomas@2353
   664
        [DllImport("mapi32.DLL", CharSet = CharSet.Ansi, EntryPoint = "HrSetOneProp@8")]
Thomas@2354
   665
        internal static extern void HrSetOneProp(IntPtr iMAPIProp, IntPtr propertyValue);
Thomas@2353
   666
Thomas@2353
   667
        /// <summary>
Thomas@2354
   668
        /// Frees a memory buffer allocated with a call to the MAPIAllocateBuffer function or the MAPIAllocateMore function.
Thomas@2353
   669
        /// </summary>
Thomas@2354
   670
        /// <param name="buffer">Pointer to a previously allocated memory buffer. If IntPtr.Zero is passed in the buffer parameter, MAPIFreeBuffer does nothing.</param>
Thomas@2353
   671
        [DllImport("mapi32.DLL", CharSet = CharSet.Ansi, EntryPoint = "MAPIFreeBuffer@4")]
Thomas@2354
   672
        internal static extern void MAPIFreeBuffer(IntPtr buffer);
Thomas@2353
   673
        #endregion
Thomas@2353
   674
Thomas@2353
   675
    }
Thomas@2353
   676
}