private void UnprotectAllAttachments() { if (!(this.decodedItem.TryGetProperty(MessageItemSchema.DRMServerLicense) is string)) { return; } foreach (AttachmentHandle handle in this.decodedItem.AttachmentCollection) { using (Attachment attachment = this.decodedItem.AttachmentCollection.Open(handle)) { Stream stream = null; StreamAttachment streamAttachment = attachment as StreamAttachment; if (StreamAttachment.TryOpenRestrictedContent(streamAttachment, this.orgId, out stream)) { using (stream) { using (Stream contentStream = streamAttachment.GetContentStream(PropertyOpenMode.Create)) { Util.StreamHandler.CopyStreamData(stream, contentStream); } } attachment.Save(); } } } }
public static bool TryOpenRestrictedContent(StreamAttachment sourceAttachment, out Stream result) { return(StreamAttachment.TryOpenRestrictedContent(sourceAttachment, OrganizationId.ForestWideOrgId, out result)); }
private void DecryptMsg(MessageItem rightsProtectedMessage, string useLicense, SafeRightsManagementHandle decryptorHandle) { if (decryptorHandle == null) { throw new ArgumentNullException("decryptorHandle"); } if (decryptorHandle.IsInvalid) { throw new ArgumentException("decryptorHandle"); } this.originalItem = rightsProtectedMessage; DrmEmailMessage drmEmailMessage = null; bool flag = false; try { ExTraceGlobals.RightsManagementTracer.TraceDebug((long)this.GetHashCode(), "Found a use license for server. Decrypting message"); DrmEmailMessageBinding messageBinding = new DrmEmailMessageBinding(this.drmMsgContainer.PublishLicense, decryptorHandle); this.drmMsgContainer.Bind(messageBinding, new CreateStreamCallbackDelegate(this.BodyStreamCallback), new CreateStreamCallbackDelegate(this.AttachmentsStreamCallback)); drmEmailMessage = this.drmMsgContainer.EmailMessage; this.SaveAndCloseCurrentAttachment(); this.decryptedItem.SafeSetProperty(InternalSchema.DRMServerLicense, useLicense); this.decryptedItem.SafeSetProperty(InternalSchema.DrmPublishLicense, this.drmMsgContainer.PublishLicense); if (drmEmailMessage.Attachments.Count > 0) { ExTraceGlobals.RightsManagementTracer.TraceDebug <int>((long)this.GetHashCode(), "Number of attachments in the rights protected message : {0}", drmEmailMessage.Attachments.Count); int num = 0; foreach (DrmEmailAttachment drmEmailAttachment in drmEmailMessage.Attachments) { using (Attachment attachment = this.decryptedItem.AttachmentCollection.Open(this.messageAttachmentIds[num++], null)) { attachment.FileName = drmEmailAttachment.FileName; attachment[InternalSchema.DisplayName] = drmEmailAttachment.DisplayName; attachment.ContentId = drmEmailAttachment.ContentId; attachment[InternalSchema.AttachContentLocation] = drmEmailAttachment.ContentLocation; attachment[InternalSchema.AttachMhtmlFlags] = drmEmailAttachment.AttachFlags; attachment[InternalSchema.AttachCalendarHidden] = drmEmailAttachment.AttachHidden; if (drmEmailAttachment.AttachHidden) { attachment.IsInline = true; } if (drmEmailMessage.BodyFormat == BodyFormat.Rtf) { attachment.RenderingPosition = (int)drmEmailAttachment.CharacterPosition; if (attachment.AttachmentType == AttachmentType.EmbeddedMessage || attachment.AttachmentType == AttachmentType.Stream) { attachment[InternalSchema.AttachRendering] = drmEmailAttachment.AttachRendering; } } if (this.decryptAttachments) { Stream stream = null; StreamAttachment streamAttachment = attachment as StreamAttachment; if (streamAttachment != null && StreamAttachment.TryOpenRestrictedContent(streamAttachment, this.orgId, out stream)) { using (stream) { using (Stream contentStream = streamAttachment.GetContentStream(PropertyOpenMode.Create)) { Util.StreamHandler.CopyStreamData(stream, contentStream); } } } } attachment.Save(); } } } drmEmailMessage.Close(); drmEmailMessage = null; if (this.originalItem != null) { PersistablePropertyBag.CopyProperty(this.originalItem.PropertyBag, InternalSchema.TransportMessageHeaders, this.decryptedItem.PropertyBag); this.decryptedItem.Recipients.CopyRecipientsFrom(this.originalItem.Recipients); } this.decryptedItem.Save(SaveMode.NoConflictResolution); this.decryptedItem.Load(InternalSchema.ContentConversionProperties); flag = true; } finally { if (drmEmailMessage != null) { drmEmailMessage.Close(); drmEmailMessage = null; } if (!flag && this.decryptedItem != null) { this.decryptedItem.Dispose(); this.decryptedItem = null; } } }