Beispiel #1
0
        internal static ReadOnlyCollection <AttachmentLink> MergeAttachmentLinks(IList <AttachmentLink> existingLinks, CoreAttachmentCollection attachments)
        {
            IList <AttachmentLink> list;

            if (attachments != null)
            {
                list = ((existingLinks == null) ? new List <AttachmentLink>(attachments.Count) : new List <AttachmentLink>(existingLinks));
                ICollection <PropertyDefinition> preloadProperties = new PropertyDefinition[]
                {
                    AttachmentSchema.AttachContentId
                };
                using (IEnumerator <AttachmentHandle> enumerator = attachments.GetEnumerator())
                {
                    while (enumerator.MoveNext())
                    {
                        AttachmentHandle handle = enumerator.Current;
                        using (CoreAttachment coreAttachment = attachments.Open(handle, preloadProperties))
                        {
                            using (Attachment attachment = AttachmentCollection.CreateTypedAttachment(coreAttachment, null))
                            {
                                if (AttachmentLink.Find(attachment.Id, list) == null)
                                {
                                    AttachmentLink item = new AttachmentLink(attachment);
                                    list.Add(item);
                                }
                            }
                        }
                    }
                    goto IL_C5;
                }
            }
            list = ((existingLinks == null) ? new List <AttachmentLink>(0) : new List <AttachmentLink>(existingLinks));
IL_C5:
            return(new ReadOnlyCollection <AttachmentLink>(list));
        }
Beispiel #2
0
        public static bool IsMessageOpaqueSigned(Item item)
        {
            Util.ThrowOnNullArgument(item, "item");
            ICoreItem coreItem       = item.CoreItem;
            string    valueOrDefault = coreItem.PropertyBag.GetValueOrDefault <string>(InternalSchema.ItemClass, string.Empty);

            if (!ObjectClass.IsSmime(valueOrDefault))
            {
                return(false);
            }
            if (coreItem.AttachmentCollection.Count != 1)
            {
                return(false);
            }
            IList <AttachmentHandle> allHandles = coreItem.AttachmentCollection.GetAllHandles();
            bool result;

            using (CoreAttachment coreAttachment = coreItem.AttachmentCollection.Open(allHandles[0]))
            {
                using (StreamAttachment streamAttachment = (StreamAttachment)AttachmentCollection.CreateTypedAttachment(coreAttachment, new AttachmentType?(AttachmentType.Stream)))
                {
                    if (streamAttachment == null)
                    {
                        result = false;
                    }
                    else
                    {
                        result = ConvertUtils.IsMessageOpaqueSigned(coreItem, streamAttachment);
                    }
                }
            }
            return(result);
        }
        public ItemAttachment Create(StoreObjectType type)
        {
            EnumValidator.ThrowIfInvalid <StoreObjectType>(type, "type");
            ItemAttachment result = null;

            using (DisposeGuard disposeGuard = default(DisposeGuard))
            {
                CoreAttachment coreAttachment = this.CoreAttachmentCollection.CreateItemAttachment(type);
                disposeGuard.Add <CoreAttachment>(coreAttachment);
                result = (ItemAttachment)AttachmentCollection.CreateTypedAttachment(coreAttachment, new AttachmentType?(AttachmentType.EmbeddedMessage));
                disposeGuard.Success();
            }
            return(result);
        }
        public ItemAttachment AddExistingItem(IItem item)
        {
            Util.ThrowOnNullArgument(item, "item");
            ItemAttachment result = null;

            using (DisposeGuard disposeGuard = default(DisposeGuard))
            {
                CoreAttachment coreAttachment = this.CoreAttachmentCollection.CreateFromExistingItem(item);
                disposeGuard.Add <CoreAttachment>(coreAttachment);
                result = (ItemAttachment)AttachmentCollection.CreateTypedAttachment(coreAttachment, new AttachmentType?(AttachmentType.EmbeddedMessage));
                disposeGuard.Success();
            }
            return(result);
        }
        public Attachment Open(AttachmentHandle handle, ICollection <PropertyDefinition> propertyDefinitions)
        {
            Util.ThrowOnNullArgument(handle, "handle");
            Attachment result = null;

            using (DisposeGuard disposeGuard = default(DisposeGuard))
            {
                CoreAttachment coreAttachment = this.CoreAttachmentCollection.Open(handle, propertyDefinitions);
                disposeGuard.Add <CoreAttachment>(coreAttachment);
                result = AttachmentCollection.CreateTypedAttachment(coreAttachment, null);
                disposeGuard.Success();
            }
            return(result);
        }
Beispiel #6
0
        internal static bool IsSmimeMessage(ICoreItem coreItem, out bool isMultipartSigned, out bool isOpaqueSigned, out StreamAttachment attachment)
        {
            isMultipartSigned = false;
            isOpaqueSigned    = false;
            attachment        = null;
            string valueOrDefault = coreItem.PropertyBag.GetValueOrDefault <string>(InternalSchema.ItemClass, string.Empty);

            if (!ObjectClass.IsSmime(valueOrDefault))
            {
                return(false);
            }
            IList <AttachmentHandle> allHandles = coreItem.AttachmentCollection.GetAllHandles();

            if (allHandles.Count != 1)
            {
                return(false);
            }
            using (DisposeGuard disposeGuard = default(DisposeGuard))
            {
                CoreAttachment coreAttachment = coreItem.AttachmentCollection.Open(allHandles[0]);
                disposeGuard.Add <CoreAttachment>(coreAttachment);
                StreamAttachment streamAttachment = (StreamAttachment)AttachmentCollection.CreateTypedAttachment(coreAttachment, new AttachmentType?(AttachmentType.Stream));
                if (streamAttachment == null)
                {
                    return(false);
                }
                disposeGuard.Add <StreamAttachment>(streamAttachment);
                if (ObjectClass.IsSmimeClearSigned(valueOrDefault))
                {
                    isMultipartSigned = ConvertUtils.IsMessageMultipartSigned(coreItem, streamAttachment);
                    if (isMultipartSigned)
                    {
                        attachment = streamAttachment;
                        disposeGuard.Success();
                        return(true);
                    }
                }
                isOpaqueSigned = ConvertUtils.IsMessageOpaqueSigned(coreItem, streamAttachment);
                if (isOpaqueSigned)
                {
                    attachment = streamAttachment;
                    disposeGuard.Success();
                    return(true);
                }
            }
            return(false);
        }
        public Attachment Create(AttachmentType?type, Attachment clone)
        {
            if (type != null)
            {
                EnumValidator.ThrowIfInvalid <AttachmentType>(type.Value, "type");
            }
            Util.ThrowOnNullArgument(clone, "clone");
            Attachment result = null;

            using (DisposeGuard disposeGuard = default(DisposeGuard))
            {
                CoreAttachment coreAttachment = this.CoreAttachmentCollection.InternalCreateCopy(type, clone.CoreAttachment);
                disposeGuard.Add <CoreAttachment>(coreAttachment);
                result = AttachmentCollection.CreateTypedAttachment(coreAttachment, type);
                disposeGuard.Success();
            }
            return(result);
        }
Beispiel #8
0
 internal void SaveAttachment()
 {
     this.CheckDisposed("SaveAttachment");
     if (this.attachMethod == null)
     {
         this.ResetAttachMethod(1);
     }
     using (DisposeGuard disposeGuard = default(DisposeGuard))
     {
         disposeGuard.Add <CoreAttachment>(this.coreAttachment);
         using (Attachment attachment = AttachmentCollection.CreateTypedAttachment(this.coreAttachment, null))
         {
             attachment.SaveFlags |= PropertyBagSaveFlags.IgnoreMapiComputedErrors;
             attachment.Save();
         }
         disposeGuard.Success();
     }
 }
 internal static void CloneAttachmentCollection(ICoreItem sourceItem, ICoreItem destinationItem)
 {
     foreach (AttachmentHandle handle in sourceItem.AttachmentCollection)
     {
         using (CoreAttachment coreAttachment = sourceItem.AttachmentCollection.Open(handle, CoreObjectSchema.AllPropertiesOnStore))
         {
             using (CoreAttachment coreAttachment2 = destinationItem.AttachmentCollection.CreateCopy(coreAttachment))
             {
                 using (Attachment attachment = AttachmentCollection.CreateTypedAttachment(coreAttachment2, new AttachmentType?(coreAttachment2.AttachmentType)))
                 {
                     attachment.SaveFlags |= (coreAttachment.PropertyBag.SaveFlags | PropertyBagSaveFlags.IgnoreMapiComputedErrors);
                     attachment.Save();
                 }
             }
         }
     }
     destinationItem.AttachmentCollection.IsClonedFromAnExistingAttachmentCollection = (sourceItem.AttachmentCollection.Count > 0 && !sourceItem.AttachmentCollection.IsDirty && sourceItem.AttachmentCollection.Count == destinationItem.AttachmentCollection.Count);
 }
Beispiel #10
0
        public StreamAttachment ConvertToImageAttachment(CoreAttachmentCollection collection, ImageFormat format)
        {
            base.CheckDisposed("ConvertToImageAttachment");
            Util.ThrowOnNullArgument(collection, "collection");
            EnumValidator.ThrowIfInvalid <ImageFormat>(format);
            StreamAttachment result;

            using (DisposeGuard disposeGuard = default(DisposeGuard))
            {
                CoreAttachment coreAttachment = collection.InternalCreateCopy(new AttachmentType?(AttachmentType.Stream), base.CoreAttachment);
                disposeGuard.Add <CoreAttachment>(coreAttachment);
                StreamAttachment streamAttachment = (StreamAttachment)AttachmentCollection.CreateTypedAttachment(coreAttachment, new AttachmentType?(AttachmentType.Stream));
                disposeGuard.Add <StreamAttachment>(streamAttachment);
                string text = streamAttachment.FileName;
                if (string.IsNullOrEmpty(text))
                {
                    text = Attachment.GenerateFilename();
                }
                string str = null;
                switch (format)
                {
                case ImageFormat.Jpeg:
                    str = ".jpg";
                    break;

                case ImageFormat.Png:
                    str = ".png";
                    break;
                }
                streamAttachment.FileName    = text + str;
                streamAttachment.ContentType = "image/jpeg";
                streamAttachment.IsInline    = true;
                using (Stream contentStream = streamAttachment.GetContentStream(PropertyOpenMode.Create))
                {
                    if (!this.TryConvertToImage(contentStream, ImageFormat.Jpeg))
                    {
                        ConvertUtils.SaveDefaultImage(contentStream);
                    }
                }
                disposeGuard.Success();
                result = streamAttachment;
            }
            return(result);
        }
        public Attachment Create(AttachmentType type)
        {
            EnumValidator.ThrowIfInvalid <AttachmentType>(type, new AttachmentType[]
            {
                AttachmentType.Stream,
                AttachmentType.EmbeddedMessage,
                AttachmentType.Ole,
                AttachmentType.Reference
            });
            Attachment result = null;

            using (DisposeGuard disposeGuard = default(DisposeGuard))
            {
                CoreAttachment coreAttachment = this.CoreAttachmentCollection.Create(type);
                disposeGuard.Add <CoreAttachment>(coreAttachment);
                result = AttachmentCollection.CreateTypedAttachment(coreAttachment, new AttachmentType?(type));
                disposeGuard.Success();
            }
            return(result);
        }
        internal Attachment Open(AttachmentHandle handle, AttachmentType?type, ICollection <PropertyDefinition> propertyDefinitions)
        {
            Util.ThrowOnNullArgument(handle, "handle");
            if (type != null)
            {
                EnumValidator.ThrowIfInvalid <AttachmentType>(type.Value, "type");
            }
            Attachment     attachment     = null;
            CoreAttachment coreAttachment = null;

            try
            {
                coreAttachment = this.CoreAttachmentCollection.Open(handle, propertyDefinitions);
                attachment     = AttachmentCollection.CreateTypedAttachment(coreAttachment, type);
            }
            finally
            {
                if (attachment == null && coreAttachment != null)
                {
                    coreAttachment.Dispose();
                }
            }
            return(attachment);
        }
 public Attachment Open(AttachmentId id, ICollection <PropertyDefinition> propertyDefinitions)
 {
     Util.ThrowOnNullArgument(id, "id");
     return(AttachmentCollection.CreateTypedAttachment(this.CoreAttachmentCollection.Open(id, propertyDefinitions), null));
 }