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