public static ChannelMessage From(Stream stream, ChannelMessageHeader header) { stream.Seek(0, SeekOrigin.Begin); var mimeMessage = Mime.Parse(stream); var message = new ChannelMessage(); if (header != null) { message.MessageNumber = header.MessageNumber; message.Size = header.Size; message.SourceFolder = header.SourceFolder; message.IsRead = header.IsRead; message.IsStarred = header.IsStarred; message.DateReceived = header.DateReceived; } else { message.SourceFolder = "INBOX"; message.DateReceived = DateTime.Now; } message.Context = mimeMessage.MainEntity.Subject; message.MessageIdentifier = mimeMessage.MainEntity.MessageID; message.InReplyTo = mimeMessage.MainEntity.InReplyTo; message.BodyHtml = mimeMessage.BodyHtml.ToStream(); if (!String.IsNullOrEmpty(mimeMessage.BodyText)) { message.BodyText = mimeMessage.BodyText.ToStream(); } message.From = new SourceAddress(mimeMessage.MainEntity.From.ToAddressListString()); if (mimeMessage.MainEntity.To == null) { var deliveredTo = mimeMessage.MainEntity.Header.GetFirst("Delivered-To:"); if (deliveredTo != null) { message.To = new SourceAddressCollection(deliveredTo.Value); } } else { message.To = new SourceAddressCollection(mimeMessage.MainEntity.To.ToAddressListString()); } if (mimeMessage.MainEntity.Cc != null) { message.CC = new SourceAddressCollection(mimeMessage.MainEntity.Cc.ToAddressListString()); } if (mimeMessage.MainEntity.Bcc != null) { message.BCC = new SourceAddressCollection(mimeMessage.MainEntity.Bcc.ToAddressListString()); } foreach (var att in mimeMessage.Attachments) { var attachment = new ChannelAttachment { Filename = att.ContentDisposition == ContentDisposition_enum.Attachment ? att.ContentDisposition_FileName : att.ContentType_Name }; if (!String.IsNullOrEmpty(att.ContentID)) { attachment.ContentId = att.ContentID.Trim('<', '>'); } attachment.ContentType = String.IsNullOrEmpty(att.ContentID) ? ContentType.Attachment : ContentType.Inline; attachment.ContentStream = new MemoryStream(att.Data); message.Attachments.Add(attachment); } return(message); }
protected override void ExecuteCore() { var access = new ClientMessageAccess(message); var group = new ProgressGroup { Status = Strings.SendingMessage }; ProgressManager.Current.Register(group); var msg = message.DuckCopy <ChannelMessage>(); try { msg.BodyText = access.BodyText.ToStream(); msg.BodyHtml = access.BodyHtml.ToStream(); // Handle attachments foreach (var document in message.Documents) { var attachment = new ChannelAttachment { Filename = document.Filename, ContentType = document.ContentType, ContentStream = new MemoryStream() }; attachment.ContentStream = File.OpenRead( ClientState.Current.Storage.ResolvePhysicalFilename(".", document.StreamName)); msg.Attachments.Add(attachment); } Logger.Debug("Message has {0} attachments", LogSource.BackgroundTask, msg.Attachments.Count); var recipients = BuildRecipients(); try { group.SourceChannelId = message.TargetChannelId; var channel = ChannelsManager.GetChannelObject(message.TargetChannelId); // The messageid itself is not used by the send channel, so inject our friendlyrowkey into that field. // The field is added to the headers collection which in turn is used again to determine if its a sent // item that is allready in your inbox2 sent items folder. msg.MessageIdentifier = message.MessageKey; msg.ConversationId = message.ConversationIdentifier; // Filter only the recipients that belong to this channel msg.To = recipients[message.TargetChannelId].To; msg.CC = recipients[message.TargetChannelId].CC; msg.BCC = recipients[message.TargetChannelId].BCC; Logger.Debug("Sending message. Channel = {0}", LogSource.BackgroundTask, channel.Configuration.DisplayName); if (channel.Configuration.IsConnected) { SendCloudMessage(channel.Configuration, msg); } else { channel.OutputChannel.Send(msg); } Logger.Debug("Send was successfull. Channel = {0}", LogSource.BackgroundTask, channel.Configuration.DisplayName); } catch (Exception ex) { ClientState.Current.ShowMessage( new AppMessage(String.Concat(Strings.UnableToSendMessage, ", ", String.Format(Strings.ServerSaid, ex.Message))) { SourceChannelId = message.TargetChannelId }, MessageType.Error); throw; } EventBroker.Publish(AppEvents.SendMessageFinished); Thread.CurrentThread.ExecuteOnUIThread(() => ClientState.Current.ShowMessage( new AppMessage(Strings.MessageSentSuccessfully) { EntityId = message.MessageId.Value, EntityType = EntityType.Message }, MessageType.Success)); } finally { if (msg.BodyText != null) { msg.BodyText.Dispose(); } if (msg.BodyHtml != null) { msg.BodyHtml.Dispose(); } group.IsCompleted = true; // Close attachment streams foreach (var channelAttachment in msg.Attachments) { channelAttachment.ContentStream.Dispose(); channelAttachment.ContentStream = null; } } }