예제 #1
0
        /// <summary>
        ///     Writes all the string and binary <see cref="Property">properties</see> as a <see cref="CFStream" /> to the
        ///     given <paramref name="storage" />
        /// </summary>
        /// <param name="storage">The <see cref="CFStorage" /></param>
        /// <param name="index">The <see cref="Attachment"/> index</param>
        /// <returns>
        ///     Total size of the written <see cref="Attachment"/> object and it's <see cref="Properties"/>
        /// </returns>
        internal long WriteProperties(CFStorage storage, int index)
        {
            var propertiesStream = new AttachmentProperties();

            propertiesStream.AddProperty(PropertyTags.PR_ATTACH_NUM, index, PropertyFlags.PROPATTR_READABLE);
            propertiesStream.AddProperty(PropertyTags.PR_INSTANCE_KEY, Mapi.GenerateInstanceKey(), PropertyFlags.PROPATTR_READABLE);
            propertiesStream.AddProperty(PropertyTags.PR_RECORD_KEY, Mapi.GenerateRecordKey(), PropertyFlags.PROPATTR_READABLE);
            propertiesStream.AddProperty(PropertyTags.PR_RENDERING_POSITION, RenderingPosition, PropertyFlags.PROPATTR_READABLE);
            propertiesStream.AddProperty(PropertyTags.PR_OBJECT_TYPE, MapiObjectType.MAPI_ATTACH);

            if (!string.IsNullOrEmpty(FileName))
            {
                propertiesStream.AddProperty(PropertyTags.PR_DISPLAY_NAME_W, FileName);
                propertiesStream.AddProperty(PropertyTags.PR_ATTACH_FILENAME_W, GetShortFileName(FileName));
                propertiesStream.AddProperty(PropertyTags.PR_ATTACH_LONG_FILENAME_W, FileName);
                propertiesStream.AddProperty(PropertyTags.PR_ATTACH_EXTENSION_W, Path.GetExtension(FileName));

                if (!string.IsNullOrEmpty(ContentId))
                {
                    propertiesStream.AddProperty(PropertyTags.PR_ATTACH_CONTENT_ID_W, ContentId);
                }

                propertiesStream.AddProperty(PropertyTags.PR_ATTACH_MIME_TAG_W, MimeTypes.GetMimeType(FileName));
            }

            propertiesStream.AddProperty(PropertyTags.PR_ATTACH_METHOD, Type);

            switch (Type)
            {
            case AttachmentType.ATTACH_BY_VALUE:
            case AttachmentType.ATTACH_EMBEDDED_MSG:
                propertiesStream.AddProperty(PropertyTags.PR_ATTACH_DATA_BIN, Stream.ToByteArray());
                propertiesStream.AddProperty(PropertyTags.PR_ATTACH_SIZE, Stream.Length);
                break;

            case AttachmentType.ATTACH_BY_REF_ONLY:
                propertiesStream.AddProperty(PropertyTags.PR_ATTACH_DATA_BIN, new byte[0]);
                propertiesStream.AddProperty(PropertyTags.PR_ATTACH_SIZE, _file.Length);
                propertiesStream.AddProperty(PropertyTags.PR_ATTACH_LONG_PATHNAME_W, _file.FullName);
                break;

            //case AttachmentType.ATTACH_EMBEDDED_MSG:
            //    var msgStorage = storage.AddStorage(PropertyTags.PR_ATTACH_DATA_BIN.Name);
            //    var cf = new CompoundFile(Stream);
            //    Storage.Copy(cf.RootStorage, msgStorage);
            //    propertiesStream.AddProperty(PropertyTags.PR_ATTACH_SIZE, Stream.Length);
            //    propertiesStream.AddProperty(PropertyTags.PR_ATTACH_ENCODING, 0);
            //    break;

            case AttachmentType.ATTACH_BY_REFERENCE:
            case AttachmentType.ATTACH_BY_REF_RESOLVE:
            case AttachmentType.NO_ATTACHMENT:
            case AttachmentType.ATTACH_OLE:
                throw new NotSupportedException("AttachByReference, AttachByRefResolve, NoAttachment and AttachOle are not supported");
            }

            if (IsInline)
            {
                propertiesStream.AddProperty(PropertyTags.PR_ATTACHMENT_HIDDEN, true);
                propertiesStream.AddProperty(PropertyTags.PR_ATTACH_FLAGS, AttachmentFlags.ATT_MHTML_REF);
            }

            var utcNow = DateTime.UtcNow;

            propertiesStream.AddProperty(PropertyTags.PR_CREATION_TIME, utcNow);
            propertiesStream.AddProperty(PropertyTags.PR_LAST_MODIFICATION_TIME, utcNow);
            propertiesStream.AddProperty(PropertyTags.PR_STORE_SUPPORT_MASK, StoreSupportMaskConst.StoreSupportMask, PropertyFlags.PROPATTR_READABLE);

            return(propertiesStream.WriteProperties(storage));
        }