Example #1
0
        public int CreateSampleMessage(
            int?folderId,
            int?mailboxId,
            List <string> to,
            List <string> cc,
            List <string> bcc,
            bool importance,
            bool unread,
            string subject,
            string body,
            string calendarUid   = null,
            DateTime?date        = null,
            List <int> tagIds    = null,
            string fromAddress   = null,
            bool add2Index       = false,
            string mimeMessageId = null,
            uint?userFolderId    = null)
        {
            var folder = folderId.HasValue ? (FolderType)folderId.Value : FolderType.Inbox;

            if (!MailFolder.IsIdOk(folder))
            {
                throw new ArgumentException(@"Invalid folder id", "folderId");
            }

            if (!mailboxId.HasValue)
            {
                throw new ArgumentException(@"Invalid mailbox id", "mailboxId");
            }

            var accounts = _engineFactory.AccountEngine.GetAccountInfoList().ToAccountData().ToList();

            var account = mailboxId.HasValue
                ? accounts.FirstOrDefault(a => a.MailboxId == mailboxId)
                : accounts.FirstOrDefault(a => a.IsDefault) ?? accounts.FirstOrDefault();

            if (account == null)
            {
                throw new ArgumentException("Mailbox not found");
            }

            var mbox = _engineFactory.MailboxEngine.GetMailboxData(
                new СoncreteUserMailboxExp(account.MailboxId, TenantId, Username));

            if (mbox == null)
            {
                throw new ArgumentException("no such mailbox");
            }

            var internalId = string.IsNullOrEmpty(mimeMessageId) ? MailUtil.CreateMessageId() : mimeMessageId;

            var restoreFolder = folder == FolderType.Spam || folder == FolderType.Trash
                ? FolderType.Inbox
                : folder;

            string sampleBody;
            string sampleIntro;

            if (!to.Any())
            {
                to = new List <string> {
                    mbox.EMail.Address
                };
            }

            if (!string.IsNullOrEmpty(body))
            {
                sampleBody  = body;
                sampleIntro = MailUtil.GetIntroduction(body);
            }
            else
            {
                sampleBody  = LOREM_IPSUM_BODY;
                sampleIntro = LOREM_IPSUM_INTRO;
            }

            var sampleMessage = new MailMessage
            {
                Date            = date ?? DateTime.UtcNow,
                MimeMessageId   = internalId,
                MimeReplyToId   = null,
                ChainId         = internalId,
                ReplyTo         = "",
                To              = string.Join(", ", to.ToArray()),
                Cc              = cc.Any() ? string.Join(", ", cc.ToArray()) : "",
                Bcc             = bcc.Any() ? string.Join(", ", bcc.ToArray()) : "",
                Subject         = string.IsNullOrEmpty(subject) ? LOREM_IPSUM_SUBJECT : subject,
                Important       = importance,
                TextBodyOnly    = false,
                Attachments     = new List <MailAttachmentData>(),
                Size            = sampleBody.Length,
                MailboxId       = mbox.MailBoxId,
                HtmlBody        = sampleBody,
                Introduction    = sampleIntro,
                Folder          = folder,
                RestoreFolderId = restoreFolder,
                IsNew           = unread,
                StreamId        = MailUtil.CreateStreamId(),
                CalendarUid     = calendarUid
            };

            if (!string.IsNullOrEmpty(fromAddress))
            {
                var from = Parser.ParseAddress(fromAddress);

                sampleMessage.From      = from.ToString();
                sampleMessage.FromEmail = from.Email;
            }
            else
            {
                sampleMessage.From      = MailUtil.CreateFullEmail(mbox.Name, mbox.EMail.Address);
                sampleMessage.FromEmail = mbox.EMail.Address;
            }

            if (tagIds != null && tagIds.Any())
            {
                sampleMessage.TagIds = tagIds;
            }

            MessageEngine.StoreMailBody(mbox, sampleMessage, Log);

            var id = _engineFactory.MessageEngine.MailSave(mbox, sampleMessage, 0, folder, restoreFolder, userFolderId,
                                                           SAMPLE_UIDL, "", false);

            if (!add2Index)
            {
                return(id);
            }

            var message = _engineFactory.MessageEngine.GetMessage(id, new MailMessageData.Options());

            message.IsNew = unread;

            var wrapper = message.ToMailWrapper(mbox.TenantId, new Guid(mbox.UserId));

            _engineFactory.IndexEngine.Add(wrapper);

            return(id);
        }
Example #2
0
        public static MailMessageData ToMailMessage(this MailComposeBase draft)
        {
            MailboxAddress fromVerified;

            if (string.IsNullOrEmpty(draft.From))
            {
                throw new DraftException(DraftException.ErrorTypes.EmptyField, "Empty email address in {0} field",
                                         DraftFieldTypes.From);
            }

            if (!MailboxAddress.TryParse(ParserOptions.Default, draft.From, out fromVerified))
            {
                throw new DraftException(DraftException.ErrorTypes.IncorrectField, "Incorrect email address",
                                         DraftFieldTypes.From);
            }

            if (string.IsNullOrEmpty(fromVerified.Name))
            {
                fromVerified.Name = draft.Mailbox.Name;
            }

            if (string.IsNullOrEmpty(draft.MimeMessageId))
            {
                throw new ArgumentException("MimeMessageId");
            }

            var messageItem = new MailMessageData
            {
                From                   = fromVerified.ToString(),
                FromEmail              = fromVerified.Address,
                To                     = string.Join(", ", draft.To.ToArray()),
                Cc                     = draft.Cc != null?string.Join(", ", draft.Cc.ToArray()) : "",
                                   Bcc = draft.Bcc != null?string.Join(", ", draft.Bcc.ToArray()) : "",
                                             Subject          = draft.Subject,
                                             Date             = DateTime.UtcNow,
                                             Important        = draft.Important,
                                             HtmlBody         = draft.HtmlBody,
                                             Introduction     = MailUtil.GetIntroduction(draft.HtmlBody),
                                             StreamId         = draft.StreamId,
                                             TagIds           = draft.Labels != null && draft.Labels.Count != 0 ? new List <int>(draft.Labels) : null,
                                             Size             = draft.HtmlBody.Length,
                                             MimeReplyToId    = draft.MimeReplyToId,
                                             MimeMessageId    = draft.MimeMessageId,
                                             IsNew            = false,
                                             Folder           = draft.Folder,
                                             ChainId          = draft.MimeMessageId,
                                             CalendarUid      = draft.CalendarEventUid,
                                             CalendarEventIcs = draft.CalendarIcs,
                                             MailboxId        = draft.Mailbox.MailBoxId
            };

            if (messageItem.Attachments == null)
            {
                messageItem.Attachments = new List <MailAttachmentData>();
            }

            draft.Attachments.ForEach(attachment =>
            {
                attachment.tenant = draft.Mailbox.TenantId;
                attachment.user   = draft.Mailbox.UserId;
            });

            messageItem.Attachments.AddRange(draft.Attachments);

            messageItem.HasAttachments = messageItem.Attachments.Count > 0;

            return(messageItem);
        }
Example #3
0
        public int CreateSampleMessage(
            int?folderId,
            int?mailboxId,
            List <string> to,
            List <string> cc,
            List <string> bcc,
            bool importance,
            bool unread,
            string subject,
            string body)
        {
            if (!folderId.HasValue)
            {
                folderId = MailFolder.Ids.inbox;
            }

            if (folderId < MailFolder.Ids.inbox || folderId > MailFolder.Ids.spam)
            {
                throw new ArgumentException(@"Invalid folder id", "folderId");
            }

            if (!mailboxId.HasValue)
            {
                throw new ArgumentException(@"Invalid mailbox id", "mailboxId");
            }

            var accounts = MailBoxManager.GetAccountInfo(TenantId, Username).ToAddressData();

            var account = mailboxId.HasValue
                ? accounts.FirstOrDefault(a => a.MailboxId == mailboxId)
                : accounts.FirstOrDefault(a => a.IsDefault) ?? accounts.FirstOrDefault();

            if (account == null)
            {
                throw new ArgumentException("Mailbox not found");
            }

            var mbox = MailBoxManager.GetUnremovedMailBox(account.MailboxId);

            if (mbox == null)
            {
                throw new ArgumentException("no such mailbox");
            }

            var mimeMessageId = MailUtil.CreateMessageId();

            var restoreFolderId = folderId.Value == MailFolder.Ids.spam || folderId.Value == MailFolder.Ids.trash
                ? MailFolder.Ids.inbox
                : folderId.Value;

            string sampleBody;
            string sampleIntro;

            if (!to.Any())
            {
                to = new List <string> {
                    mbox.EMail.Address
                };
            }

            if (!string.IsNullOrEmpty(body))
            {
                sampleBody  = body;
                sampleIntro = MailUtil.GetIntroduction(body);
            }
            else
            {
                sampleBody  = LOREM_IPSUM_BODY;
                sampleIntro = LOREM_IPSUM_INTRO;
            }

            var sampleMessage = new MailMessage
            {
                Date            = DateTime.UtcNow,
                MimeMessageId   = mimeMessageId,
                MimeReplyToId   = null,
                ChainId         = mimeMessageId,
                ReplyTo         = "",
                From            = MailUtil.CreateFullEmail(mbox.Name, mbox.EMail.Address),
                FromEmail       = mbox.EMail.Address,
                To              = string.Join(", ", to.ToArray()),
                Cc              = cc.Any() ? string.Join(", ", cc.ToArray()) : "",
                Bcc             = bcc.Any() ? string.Join(", ", bcc.ToArray()) : "",
                Subject         = string.IsNullOrEmpty(subject) ? LOREM_IPSUM_SUBJECT : subject,
                Important       = importance,
                TextBodyOnly    = false,
                Attachments     = new List <MailAttachment>(),
                Size            = sampleBody.Length,
                MailboxId       = mbox.MailBoxId,
                HtmlBody        = sampleBody,
                Introduction    = sampleIntro,
                Folder          = folderId.Value,
                RestoreFolderId = restoreFolderId,
                IsNew           = unread,
                StreamId        = MailUtil.CreateStreamId()
            };

            MailBoxManager.StoreMailBody(mbox, sampleMessage);

            var id = MailBoxManager.MailSave(mbox, sampleMessage, 0, folderId.Value, restoreFolderId, SAMPLE_UIDL, "",
                                             false);

            return(id);
        }