Exemple #1
0
        /// <summary>
        /// Tries to save the changes we just made
        /// </summary>
        /// <param name="mailItem"></param>
        /// <returns></returns>
        public static bool SaveChanges(Item mailItem)
        {
            // Pointer to IUnknown Interface
            IntPtr IUnknown = IntPtr.Zero;
            // Pointer to IMAPIProp Interface
            IntPtr IMAPIProp = IntPtr.Zero;

            // if we have no MAPIObject everything is senseless...
            if (mailItem == null)
            {
                return(false);
            }

            try {
                // We can pass NULL here as parameter, so we do it.
                MAPIInitialize(IntPtr.Zero);

                // retrive the IUnknon Interface from our MAPIObject comming from Outlook.
                IUnknown = Marshal.GetIUnknownForObject(mailItem.MAPIOBJECT);

                // create a Guid that we pass to retreive the IMAPIProp Interface.
                Guid guidIMAPIProp = new Guid(IID_IMAPIProp);

                // try to retrieve the IMAPIProp interface from IMessage Interface, everything else is sensless.
                if (Marshal.QueryInterface(IUnknown, ref guidIMAPIProp, out IMAPIProp) != 0)
                {
                    return(false);
                }
                IMAPIProp mapiProp = (IMAPIProp)Marshal.GetTypedObjectForIUnknown(IUnknown, typeof(IMAPIProp));
                return(mapiProp.SaveChanges(KEEP_OPEN_READWRITE) == 0);
            } catch (Exception ex) {
                LOG.Error(ex);
                return(false);
            } finally {
                // cleanup all references to COM Objects
                if (IMAPIProp != IntPtr.Zero)
                {
                    Marshal.Release(IMAPIProp);
                }
                //if (IMessage != IntPtr.Zero) Marshal.Release(IMessage);
                if (IUnknown != IntPtr.Zero)
                {
                    Marshal.Release(IUnknown);
                }
            }
        }
Exemple #2
0
        /// <summary>
        /// Use MAPI32.DLL "HrSetOneProp" from managed code
        /// </summary>
        /// <param name="attachment"></param>
        /// <param name="proptag"></param>
        /// <param name="propertyValue"></param>
        /// <returns></returns>
        public static bool SetMAPIProperty(IAttachment attachment, PropTags proptag, string propertyValue)
        {
            // Pointer to IUnknown Interface
            IntPtr IUnknown = IntPtr.Zero;
            // Pointer to IMAPIProp Interface
            IntPtr IMAPIProp = IntPtr.Zero;
            // Structure that will hold the Property Value
            SPropValue propValue;
            // A pointer that points to the SPropValue structure
            IntPtr ptrPropValue = IntPtr.Zero;
            object mapiObject   = attachment.MAPIOBJECT;

            // if we have no MAPIObject everything is senseless...
            if (mapiObject == null)
            {
                return(false);
            }

            try {
                // We can pass NULL here as parameter, so we do it.
                MAPIInitialize(IntPtr.Zero);

                // retrive the IUnknon Interface from our MAPIObject comming from Outlook.
                IUnknown = Marshal.GetIUnknownForObject(mapiObject);

                // create a Guid that we pass to retreive the IMAPIProp Interface.
                Guid guidIMAPIProp = new Guid(IID_IMAPIProp);

                // try to retrieve the IMAPIProp interface from IMessage Interface, everything else is sensless.
                if (Marshal.QueryInterface(IUnknown, ref guidIMAPIProp, out IMAPIProp) != 0)
                {
                    return(false);
                }

                // double check, if we wave no pointer, exit...
                if (IMAPIProp == IntPtr.Zero)
                {
                    return(false);
                }

                // Create structure
                propValue         = new SPropValue();
                propValue.propTag = (uint)proptag;
                // Create Ansi string
                propValue.Value = Marshal.StringToHGlobalUni(propertyValue);

                // Create unmanaged memory for structure
                ptrPropValue = Marshal.AllocHGlobal(Marshal.SizeOf(propValue));
                // Copy structure to unmanged memory
                Marshal.StructureToPtr(propValue, ptrPropValue, false);

                // Set the property
                HrSetOneProp(IMAPIProp, ptrPropValue);

                // Free string
                Marshal.FreeHGlobal(propValue.Value);
                IMAPIProp mapiProp = (IMAPIProp)Marshal.GetTypedObjectForIUnknown(IUnknown, typeof(IMAPIProp));
                return(mapiProp.SaveChanges(4) == 0);
            } catch (Exception ex) {
                LOG.Error(ex);
                return(false);
            } finally {
                // Free used Memory structures
                if (ptrPropValue != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(ptrPropValue);
                }
                // cleanup all references to COM Objects
                if (IMAPIProp != IntPtr.Zero)
                {
                    Marshal.Release(IMAPIProp);
                }
                //if (IMessage != IntPtr.Zero) Marshal.Release(IMessage);
                if (IUnknown != IntPtr.Zero)
                {
                    Marshal.Release(IUnknown);
                }
                MAPIUninitialize();
            }
        }
Exemple #3
0
        /// <summary>
        /// Uses the IMAPIPROP.SetProps to set the content ID
        /// </summary>
        /// <param name="attachment"></param>
        /// <param name="contentId"></param>
        public static void SetContentID(IAttachment attachment, string contentId)
        {
            // Pointer to IUnknown Interface
            IntPtr IUnknown = IntPtr.Zero;
            // Pointer to IMAPIProp Interface
            IntPtr IMAPIProp = IntPtr.Zero;
            // A pointer that points to the SPropValue structure
            IntPtr ptrPropValue = IntPtr.Zero;
            // Structure that will hold the Property Value
            SPropValue propValue;

            // if we have no MAPIObject everything is senseless...
            if (attachment == null)
            {
                return;
            }

            try {
                // We can pass NULL here as parameter, so we do it.
                MAPIInitialize(IntPtr.Zero);

                // retrive the IUnknon Interface from our MAPIObject comming from Outlook.
                IUnknown = Marshal.GetIUnknownForObject(attachment.MAPIOBJECT);
                IMAPIProp mapiProp = (IMAPIProp)Marshal.GetTypedObjectForIUnknown(IUnknown, typeof(IMAPIProp));

                // Create structure
                propValue         = new SPropValue();
                propValue.propTag = (uint)PropTags.PR_ATTACH_CONTENT_ID;
                //propValue.propTag = 0x3712001E;
                // Create Ansi string
                propValue.Value = Marshal.StringToHGlobalUni(contentId);

                // Create unmanaged memory for structure
                ptrPropValue = Marshal.AllocHGlobal(Marshal.SizeOf(propValue));
                // Copy structure to unmanged memory
                Marshal.StructureToPtr(propValue, ptrPropValue, false);
                mapiProp.SetProps(1, ptrPropValue, IntPtr.Zero);

                propValue.propTag = (uint)PropTags.PR_ATTACH_CONTENT_LOCATION;
                // Copy structure to unmanged memory
                Marshal.StructureToPtr(propValue, ptrPropValue, false);
                mapiProp.SetProps(1, ptrPropValue, IntPtr.Zero);


                // Free string
                Marshal.FreeHGlobal(propValue.Value);
                mapiProp.SaveChanges(KEEP_OPEN_READWRITE);
            } catch (Exception ex) {
                LOG.Error(ex);
            } finally {
                // Free used Memory structures
                if (ptrPropValue != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(ptrPropValue);
                }
                // cleanup all references to COM Objects
                if (IMAPIProp != IntPtr.Zero)
                {
                    Marshal.Release(IMAPIProp);
                }
                //if (IMessage != IntPtr.Zero) Marshal.Release(IMessage);
                if (IUnknown != IntPtr.Zero)
                {
                    Marshal.Release(IUnknown);
                }
            }
        }