public int Save(int tenantId, string username, MailSendItem item, int mailId)
        {
            string mime_message_id;
            string in_reply_to;

            return(Save(tenantId, username, item, mailId, out mime_message_id, out in_reply_to));
        }
Exemple #2
0
        public MailMessageItem SaveToDraft(int tenant_id, string username, MailSendItem item, int mail_id)
        {
            string mime_message_id;
            string in_reply_to;

            return(SaveToDraft(tenant_id, username, item, mail_id, out mime_message_id, out in_reply_to));
        }
Exemple #3
0
        public MailMessageItem SaveMessages(int id,
                                            IEnumerable <MailAttachment> attachments,
                                            IEnumerable <string> to,
                                            IEnumerable <string> bcc,
                                            IEnumerable <string> cc,
                                            int replyToId,
                                            string from,
                                            string body,
                                            bool importance,
                                            IEnumerable <int> tags,
                                            string streamId,
                                            string subject)
        {
            if (id < 1)
            {
                id = 0;
            }

            var item = new MailSendItem
            {
                Attachments = new List <MailAttachment>(attachments),
                Bcc         = new List <string>(bcc),
                Cc          = new List <string>(cc),
                ReplyToId   = replyToId,
                From        = from,
                HtmlBody    = body,
                Important   = importance,
                Labels      = new List <int>(tags),
                StreamId    = streamId,
                Subject     = subject,
                To          = new List <string>(to)
            };

            return(sendQueue.SaveToDraft(TenantId, Username, item, id));
        }
Exemple #4
0
        public int SendMessage(int id,
                               IEnumerable <MailAttachment> attachments,
                               IEnumerable <string> to,
                               IEnumerable <string> bcc,
                               IEnumerable <string> cc,
                               string mimeMessageId,
                               string mimeReplyToId,
                               string from,
                               string body,
                               bool importance,
                               IEnumerable <int> tags,
                               string streamId,
                               string subject,
                               FileShare fileLinksShareMode
                               )
        {
            var item = new MailSendItem
            {
                Attachments        = new List <MailAttachment>(attachments),
                Bcc                = new List <string>(bcc),
                Cc                 = new List <string>(cc),
                MimeMessageId      = string.IsNullOrEmpty(mimeMessageId) ? MailBoxManager.CreateMessageId() : mimeMessageId,
                MimeReplyToId      = mimeReplyToId,
                From               = from,
                HtmlBody           = body,
                Important          = importance,
                Labels             = new List <int>(tags),
                StreamId           = streamId,
                Subject            = subject,
                To                 = new List <string>(to),
                FileLinksShareMode = fileLinksShareMode
            };

            var accounts    = MailBoxManager.GetAccountInfo(TenantId, Username).ToAddressData();
            var mailAddress = new MailAddress(item.From);
            var account     = accounts.FirstOrDefault(a => a.Email.ToLower().Equals(mailAddress.Address));

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

            if (account.IsGroup)
            {
                throw new InvalidOperationException("Sending emails from a group address is forbidden");
            }

            if (id > 0)
            {
                var message = GetMessage(id, false, false, false);

                if (message.Folder != MailFolder.Ids.drafts)
                {
                    throw new InvalidOperationException("Sending emails is permitted only in the Drafts folder");
                }
            }

            return(SendQueue.Send(TenantId, Username, item, id, account.MailboxId));
        }
        private MailMessageItem SaveToDraft(MailSendItem original_message, int mail_id, MailBox mbox)
        {
            original_message.DisplayName = mbox.Name;
            var embeded_attachments_for_saving = original_message.ChangeEmbededAttachmentLinksForStoring(mbox.TenantId, mbox.UserId, mail_id, manager);
            var message_item = original_message.ToMailMessageItem(mbox.TenantId, mbox.UserId);

            message_item.IsNew  = false;
            message_item.Folder = MailFolder.Ids.drafts;

            message_item.ChainId = message_item.MimeMessageId;

            var need_to_restore_attachments_from_fck_location = mail_id == 0 && message_item.Attachments.Any();

            if (need_to_restore_attachments_from_fck_location)
            {
                message_item.Attachments.ForEach(attachment => manager.StoreAttachmentCopy(mbox.TenantId, mbox.UserId, attachment, original_message.StreamId));
            }

            manager.StoreMailBody(mbox.TenantId, mbox.UserId, message_item);

            var previous_mailbox_id = mail_id == 0
                                          ? mbox.MailBoxId
                                          : manager.GetMailInfo(mbox.TenantId, mbox.UserId, mail_id, false, false).MailboxId;

            mail_id         = manager.MailSave(mbox, message_item, mail_id, message_item.Folder, message_item.Folder, string.Empty, string.Empty, false);
            message_item.Id = mail_id;

            if (previous_mailbox_id != mbox.MailBoxId)
            {
                manager.UpdateChain(message_item.ChainId, message_item.Folder, previous_mailbox_id, mbox.TenantId, mbox.UserId);
            }

            if (mail_id > 0 && need_to_restore_attachments_from_fck_location)
            {
                foreach (var attachment in message_item.Attachments)
                {
                    var new_id = manager.SaveAttachment(mbox.TenantId, mail_id, attachment);
                    attachment.fileId = new_id;
                }
            }

            if (mail_id > 0 && embeded_attachments_for_saving.Any())
            {
                manager.SaveAttachments(mbox.TenantId, mail_id, embeded_attachments_for_saving);
            }

            manager.UpdateChain(message_item.ChainId, message_item.Folder, mbox.MailBoxId, mbox.TenantId, mbox.UserId);

            if (previous_mailbox_id != mbox.MailBoxId)
            {
                manager.UpdateCrmLinkedMailboxId(message_item.ChainId, mbox.TenantId, previous_mailbox_id, mbox.MailBoxId);
            }

            return(message_item);
        }
Exemple #6
0
        private MailMessageItem SaveToDraft(int tenant_id, string username, MailSendItem item, int mail_id,
                                            out string mime_message_id, out string in_reply_to)
        {
            var mbox = _manager.GetMailBox(tenant_id, username, new MailAddress(item.From));

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

            return(SaveToDraft(tenant_id, username, item, mail_id, out mime_message_id, out in_reply_to, mbox));
        }
Exemple #7
0
        public MailMessageItem SaveToDraft(int tenant, string user, MailSendItem originalMessage, int mailId, int mailboxId)
        {
            var mbox = manager.GetUnremovedMailBox(mailboxId);

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

            originalMessage.MailboxId = mbox.MailBoxId;

            return(SaveToDraft(originalMessage, mailId, mbox));
        }
Exemple #8
0
 public int SaveMessage(string id, MailSendItem item)
 {
     try
     {
         CheckPermission();
         return(DB_SaveMessage(int.Parse(id), item));
     }
     catch (WebProtocolException)
     {
         throw;
     }
     catch (Exception e)
     {
         throw GenerateException(e);
     }
 }
        public MailMessageItem SaveMessages(int id,
                                            IEnumerable <MailAttachment> attachments,
                                            IEnumerable <string> to,
                                            IEnumerable <string> bcc,
                                            IEnumerable <string> cc,
                                            string mimeMessageId,
                                            string mimeReplyToId,
                                            string from,
                                            string body,
                                            bool importance,
                                            IEnumerable <int> tags,
                                            string streamId,
                                            string subject)
        {
            if (id < 1)
            {
                id = 0;
            }

            var item = new MailSendItem
            {
                Attachments   = new List <MailAttachment>(attachments),
                Bcc           = new List <string>(bcc),
                Cc            = new List <string>(cc),
                MimeReplyToId = mimeReplyToId,
                MimeMessageId = string.IsNullOrEmpty(mimeMessageId) ? MailBoxManager.CreateMessageId() : mimeMessageId,
                From          = from,
                HtmlBody      = body,
                Important     = importance,
                Labels        = new List <int>(tags),
                StreamId      = streamId,
                Subject       = subject,
                To            = new List <string>(to)
            };

            var accounts     = MailBoxManager.GetAccountInfo(TenantId, Username).ToAddressData();
            var mail_address = new MailAddress(item.From);
            var account      = accounts.FirstOrDefault(a => a.Email.ToLower().Equals(mail_address.Address));

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

            return(SendQueue.SaveToDraft(TenantId, Username, item, id, account.MailboxId));
        }
        public int Save(int tenantId, string username, MailSendItem item, int mailId, out string mime_message_id, out string in_reply_to)
        {
            MailBox mbox = _manager.GetMailBox(tenantId, username, new MailAddress(item.From));

            item.DisplayName = mbox.Name != "" ? mbox.Name : "";

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

            MailMessageItem message_item = item.ToMailMessageItem(tenantId, username, false);

            #region Mime ids determination
            mime_message_id = mailId == 0 ?
                              MailSendItem.CreateMessageID() :
                              _manager.GetMimeMessageIdByMessageId(mailId);

            in_reply_to = item.ReplyToId != 0 ? _manager.GetMimeMessageIdByMessageId(item.ReplyToId) : "";

            if (!string.IsNullOrEmpty(mime_message_id))
            {
                message_item.MessageId = mime_message_id;
            }
            if (!string.IsNullOrEmpty(in_reply_to))
            {
                message_item.InReplyTo = in_reply_to;
            }
            #endregion

            _manager.DetectChain(message_item, mbox);

            mailId = _manager.MailSave(mbox, message_item, mailId, MailFolder.Drafts.Id, string.Empty, string.Empty, item.NeedReSaveAttachments);

            return(mailId);
        }
        public int Send(int tenant_id, string username, MailSendItem original_message, int mail_id, int mailbox_id)
        {
            var mbox = manager.GetMailBox(mailbox_id);

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

            if (mbox.Name != "")
            {
                original_message.DisplayName = mbox.Name;
            }

            if (string.IsNullOrEmpty(original_message.HtmlBody))
            {
                original_message.HtmlBody = EmptyHtmlBody;
            }

            var message_item = SaveToDraft(original_message, mail_id, mbox);

            if (message_item.Id > 0)
            {
                var user_culture    = Thread.CurrentThread.CurrentCulture;
                var user_ui_culture = Thread.CurrentThread.CurrentUICulture;
                ThreadPool.QueueUserWorkItem(delegate
                {
                    try
                    {
                        Thread.CurrentThread.CurrentCulture   = user_culture;
                        Thread.CurrentThread.CurrentUICulture = user_ui_culture;
                        original_message.ChangeEmbededAttachmentLinks(tenant_id, username);
                        original_message.ChangeSmileLinks();
                        var mime_message = original_message.ToMimeMessage(tenant_id, username, true);

                        if (mbox.RefreshToken != null)
                        {
                            ActiveUp.Net.Mail.SmtpClient.SendSsl(mime_message, mbox.SmtpServer, mbox.SmtpPort,
                                                                 mbox.SmtpAccount, GetAccessToken(mbox),
                                                                 SaslMechanism.OAuth2);
                        }
                        else if (mbox.OutcomingEncryptionType == EncryptionType.None)
                        {
                            if (mbox.AuthenticationTypeSmtp == SaslMechanism.None)
                            {
                                ActiveUp.Net.Mail.SmtpClient.Send(mime_message, mbox.SmtpServer, mbox.SmtpPort);
                            }
                            else
                            {
                                ActiveUp.Net.Mail.SmtpClient.Send(mime_message, mbox.SmtpServer, mbox.SmtpPort,
                                                                  mbox.SmtpAccount, mbox.SmtpPassword,
                                                                  mbox.AuthenticationTypeSmtp);
                            }
                        }
                        else
                        {
                            if (mbox.AuthenticationTypeSmtp == SaslMechanism.None)
                            {
                                ActiveUp.Net.Mail.SmtpClient.SendSsl(mime_message, mbox.SmtpServer, mbox.SmtpPort,
                                                                     mbox.OutcomingEncryptionType);
                            }
                            else
                            {
                                ActiveUp.Net.Mail.SmtpClient.SendSsl(mime_message, mbox.SmtpServer, mbox.SmtpPort,
                                                                     mbox.SmtpAccount, mbox.SmtpPassword,
                                                                     mbox.AuthenticationTypeSmtp,
                                                                     mbox.OutcomingEncryptionType);
                            }
                        }

                        // message was correctly send - lets update its chains id
                        var draft_chain_id = message_item.ChainId;
                        // before moving message from draft to sent folder - lets recalculate its correct chain id
                        message_item.ChainId = manager.DetectChainId(mbox, message_item);
                        // push new message correct chain id to db
                        manager.UpdateMessageChainId(mbox, message_item.Id, MailFolder.Ids.drafts, draft_chain_id,
                                                     message_item.ChainId);

                        manager.UpdateCrmLinkedChainId(mbox.MailBoxId, tenant_id, draft_chain_id,
                                                       message_item.ChainId);

                        //Move to_addresses sent
                        manager.SetConversationsFolder(tenant_id, username, MailFolder.Ids.sent,
                                                       new List <int> {
                            (Int32)message_item.Id
                        });
                        manager.SetMessageFolderRestore(tenant_id, username, MailFolder.Ids.sent,
                                                        (int)message_item.Id);

                        manager.AddRelationshipEventForLinkedAccounts(mbox, message_item, log);

                        ExecuteHandledAssemblies(message_item, mime_message, mbox);

                        StoreMessageToImapSentFolder(mbox, mime_message);

                        StoreEml(mbox, message_item.StreamId, mime_message);
                    }
                    catch (Exception ex)
                    {
                        AddNotificationAlertToMailbox(original_message, (Int32)message_item.Id, ex, mbox);
                    }
                });
            }
            else
            {
                throw new ArgumentException("Failed to_addresses save draft");
            }

            return(message_item.Id > 0 ? (Int32)message_item.Id : 1); // Callback in api will be raised if value > 0
        }
Exemple #12
0
 protected abstract int DB_SendMessage(int id, MailSendItem item);
        public MailMessageItem SaveToDraft(int tenant_id, string username, MailSendItem original_message, int mail_id, int mailbox_id)
        {
            var mbox = manager.GetMailBox(mailbox_id);

            return(SaveToDraft(original_message, mail_id, mbox));
        }
        private void AddNotificationAlertToMailbox(MailSendItem original_message, int mail_id, Exception ex_on_sanding, MailBox mbox)
        {
            try
            {
                var sb_message       = new StringBuilder(1024);
                var message_delivery = new MailSendItem {
                    Subject = MailApiResource.DeliveryFailureSubject
                };
                message_delivery.To.Add(original_message.From);
                message_delivery.From      = MailBoxManager.MAIL_DAEMON_EMAIL;
                message_delivery.Important = true;
                message_delivery.StreamId  = manager.CreateNewStreamId();
                sb_message.Append(@"<style>
                                            .button.blue:hover {
                                            color: white;
                                            background: #57A7D3;
                                            background: linear-gradient(top, #78BFE8, #57A7D3 50%, #57A7D3 51%, #3F96C3);
                                            background: -o-linear-gradient(top, #78BFE8, #57A7D3 50%, #57A7D3 51%, #3F96C3);
                                            background: -moz-linear-gradient(top, #78BFE8, #57A7D3 50%, #57A7D3 51%, #3F96C3);
                                            background: -webkit-linear-gradient(top, #78BFE8, #57A7D3 50%, #57A7D3 51%, #3F96C3);
                                            border: 1px solid #5EAAD5;
                                            }
                                            .button.blue {
                                            color: white;
                                            background: #3D96C6;
                                            background: linear-gradient(top, #59B1E2, #3D96C6 50%, #3D96C6 51%, #1A76A6);
                                            background: -o-linear-gradient(top, #59B1E2, #3D96C6 50%, #3D96C6 51%, #1A76A6);
                                            background: -moz-linear-gradient(top, #59B1E2, #3D96C6 50%, #3D96C6 51%, #1A76A6);
                                            background: -webkit-linear-gradient(top, #59B1E2, #3D96C6 50%, #3D96C6 51%, #1A76A6);
                                            border-width: 1px;
                                            border-style: solid;
                                            border-color: #4DA9DC #4098C9 #2D7399 #4098C9;
                                            }
                                            .button, .button:visited, .button:hover, .button:active {
                                            display: inline-block;
                                            font-weight: normal;
                                            text-align: center;
                                            text-decoration: none;
                                            vertical-align: middle;
                                            cursor: pointer;
                                            border-radius: 3px;
                                            -moz-border-radius: 3px;
                                            -webkit-border-radius: 3px;
                                            touch-callout: none;
                                            -o-touch-callout: none;
                                            -moz-touch-callout: none;
                                            -webkit-touch-callout: none;
                                            user-select: none;
                                            -o-user-select: none;
                                            -moz-user-select: none;
                                            -webkit-user-select: none;
                                            font-size: 12px;
                                            line-height: 14px;
                                            padding: 2px 12px 3px;
                                            color: white;
                                            background: #3D96C6;
                                            background: linear-gradient(top, #59B1E2, #3D96C6 50%, #3D96C6 51%, #1A76A6);
                                            background: -o-linear-gradient(top, #59B1E2, #3D96C6 50%, #3D96C6 51%, #1A76A6);
                                            background: -moz-linear-gradient(top, #59B1E2, #3D96C6 50%, #3D96C6 51%, #1A76A6);
                                            background: -webkit-linear-gradient(top, #59B1E2, #3D96C6 50%, #3D96C6 51%, #1A76A6);
                                            border-width: 1px;
                                            border-style: solid;
                                            border-color: #4DA9DC #4098C9 #2D7399 #4098C9;
                                            }
                                            body {
                                            color: #333;
                                            font: normal 12px Arial, Tahoma,sans-serif;
                                            }
                                            </style>");

                sb_message.AppendFormat("<div style=\"max-width:500px;\"><p style=\"color:gray;\">{0}</p>",
                                        MailApiResource.DeliveryFailureAutomaticMessage);

                sb_message.AppendFormat("<p>{0}</p>",
                                        MailApiResource.DeliveryFailureMessageIdentificator
                                        .Replace("{subject}", original_message.Subject)
                                        .Replace("{date}", DateTime.Now.ToString(CultureInfo.InvariantCulture)));

                sb_message.AppendFormat("<div><p>{0}:</p><ul style=\"color:#333;\">",
                                        MailApiResource.DeliveryFailureRecipients);

                original_message.To.ForEach(rcpt => sb_message.AppendFormat("<li>{0}</li>", HttpUtility.HtmlEncode(rcpt)));
                original_message.Cc.ForEach(rcpt => sb_message.AppendFormat("<li>{0}</li>", HttpUtility.HtmlEncode(rcpt)));
                original_message.Bcc.ForEach(rcpt => sb_message.AppendFormat("<li>{0}</li>", HttpUtility.HtmlEncode(rcpt)));

                sb_message.AppendFormat("</ul>");

                sb_message.AppendFormat("<p>{0}</p>",
                                        MailApiResource.DeliveryFailureRecommendations
                                        .Replace("{account_name}", "<b>" + original_message.From + "</b>"));

                sb_message.AppendFormat(
                    "<a id=\"delivery_failure_button\" mailid={0} class=\"button blue\" style=\"margin-right:8px;\">",
                    mail_id);
                sb_message.Append(MailApiResource.DeliveryFailureBtn + "</a></div>");

                sb_message.AppendFormat("<p>{0}</p>",
                                        MailApiResource.DeliveryFailureFAQInformation
                                        .Replace("{url_begin}",
                                                 "<a id=\"delivery_failure_faq_link\" target=\"blank\" href=\"#gmail\">")
                                        .Replace("{url_end}", "</a>"));

                var last_dot_index = ex_on_sanding.Message.LastIndexOf('.');
                var smtp_response  = ex_on_sanding.Message;

                if (last_dot_index != -1 && last_dot_index != smtp_response.Length)
                {
                    try
                    {
                        smtp_response = smtp_response.Remove(last_dot_index + 1, smtp_response.Length - last_dot_index - 1);
                    }
                    catch (Exception)
                    {
                    }
                }

                sb_message.AppendFormat(
                    "<p style=\"color:gray;\">" + MailApiResource.DeliveryFailureReason + ": \"{0}\"</p></div>", smtp_response);

                message_delivery.HtmlBody = sb_message.ToString();
                // SaveToDraft To Inbox
                var notify_message_item = message_delivery.ToMailMessageItem(mbox.TenantId, mbox.UserId);
                notify_message_item.ChainId   = notify_message_item.MimeMessageId;
                notify_message_item.IsNew     = true;
                notify_message_item.IsFromCRM = false;
                notify_message_item.IsFromTL  = false;

                manager.StoreMailBody(mbox.TenantId, mbox.UserId, notify_message_item);

// ReSharper disable UnusedVariable
                var id_mail = manager.MailSave(mbox, notify_message_item, 0, MailFolder.Ids.inbox, MailFolder.Ids.inbox,
                                               string.Empty, string.Empty, false);
// ReSharper restore UnusedVariable

                manager.CreateDeliveryFailureAlert(mbox.TenantId,
                                                   mbox.UserId,
                                                   original_message.Subject,
                                                   original_message.From,
                                                   mail_id);
            }
            catch (Exception ex_error)
            {
                log.Error("AddNotificationAlertToMailbox() in MailboxId={0} failed with exception:\r\n{1}",
                          mbox.MailBoxId, ex_error.ToString());
            }
        }
Exemple #15
0
        public int Send(int tenant_id, string username, MailSendItem item, int mail_id)
        {
            var mbox = _manager.GetMailBox(tenant_id, username, new MailAddress(item.From));

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

            if (mbox.Name != "")
            {
                item.DisplayName = mbox.Name;
            }

            string mime_message_id, in_reply_to;
            var    result_message = SaveToDraft(tenant_id, username, item, mail_id, out mime_message_id, out in_reply_to, mbox);

            if (result_message.Id > 0)
            {
                var user_culture    = Thread.CurrentThread.CurrentCulture;
                var user_ui_culture = Thread.CurrentThread.CurrentUICulture;
                ThreadPool.QueueUserWorkItem(delegate
                {
                    Message message = null;
                    try
                    {
                        Thread.CurrentThread.CurrentCulture   = user_culture;
                        Thread.CurrentThread.CurrentUICulture = user_ui_culture;
                        item.ChangeEmbededAttachmentLinks(tenant_id, username);
                        item.ChangeSmileLinks();
                        message           = item.ToMimeMessage(tenant_id, username, true);
                        message.MessageId = mime_message_id;

                        in_reply_to = in_reply_to.Trim();
                        if (!string.IsNullOrEmpty(in_reply_to))
                        {
                            message.InReplyTo = in_reply_to;
                        }

                        if (mbox.RefreshToken != null)
                        {
                            ActiveUp.Net.Mail.SmtpClient.SendSsl(message, mbox.SmtpServer, mbox.SmtpPort, mbox.SmtpAccount, GetAccessToken(mbox), SaslMechanism.OAuth2);
                        }
                        else if (mbox.OutcomingEncryptionType == EncryptionType.None)
                        {
                            if (mbox.AuthenticationTypeSmtp == SaslMechanism.None)
                            {
                                ActiveUp.Net.Mail.SmtpClient.Send(message, mbox.SmtpServer, mbox.SmtpPort);
                            }
                            else
                            {
                                ActiveUp.Net.Mail.SmtpClient.Send(message, mbox.SmtpServer, mbox.SmtpPort, mbox.SmtpAccount, mbox.SmtpPassword, mbox.AuthenticationTypeSmtp);
                            }
                        }
                        else
                        {
                            if (mbox.AuthenticationTypeSmtp == SaslMechanism.None)
                            {
                                ActiveUp.Net.Mail.SmtpClient.SendSsl(message, mbox.SmtpServer, mbox.SmtpPort, mbox.OutcomingEncryptionType);
                            }
                            else
                            {
                                ActiveUp.Net.Mail.SmtpClient.SendSsl(message, mbox.SmtpServer, mbox.SmtpPort, mbox.SmtpAccount, mbox.SmtpPassword, mbox.AuthenticationTypeSmtp, mbox.OutcomingEncryptionType);
                            }
                        }

                        //Move to_addresses sent
                        _manager.SetConversationsFolder(tenant_id, username, MailFolder.Ids.sent, new List <int> {
                            (Int32)result_message.Id
                        });
                        _manager.SetMessageFolderRestore(tenant_id, username, MailFolder.Ids.sent, (int)result_message.Id);

                        _manager.AddRelationshipEventForLinkedAccounts(mbox, result_message);
                    }
                    catch (Exception ex)
                    {
                        AddNotificationAlertToMailbox(tenant_id, username, item, result_message, ex, mbox, message);
                    }
                });
            }
            else
            {
                throw new ArgumentException("Failed to_addresses save draft");
            }

            return(result_message.Id > 0 ? (Int32)result_message.Id : 1); // Callback in api will be raised if value > 0
        }
Exemple #16
0
        private MailMessageItem SaveToDraft(int tenant_id, string username, MailSendItem item, int mail_id,
                                            out string mime_message_id, out string in_reply_to, MailBox mbox)
        {
            item.DisplayName = mbox.Name;
            var embeded_attachments_for_saving = item.ChangeEmbededAttachmentLinksForStoring(tenant_id, username, mail_id, _manager);
            var message_item = item.ToMailMessageItem(tenant_id, username);

            message_item.IsNew  = false;
            message_item.Folder = MailFolder.Ids.drafts;

            mime_message_id = mail_id == 0 ? _manager.CreateMessageId() : _manager.GetMimeMessageIdByMessageId(mail_id);
            in_reply_to     = item.ReplyToId != 0 ? _manager.GetMimeMessageIdByMessageId(item.ReplyToId) : "";

            if (!string.IsNullOrEmpty(mime_message_id))
            {
                message_item.MessageId = mime_message_id;
            }
            if (!string.IsNullOrEmpty(in_reply_to))
            {
                message_item.InReplyTo = in_reply_to;
            }

            message_item.ChainId = _manager.DetectChainId(mbox, message_item);

            var need_to_restore_attachments_from_fck_location = mail_id == 0 && message_item.Attachments.Any();

            if (need_to_restore_attachments_from_fck_location)
            {
                message_item.Attachments.ForEach(attachment => _manager.StoreAttachmentCopy(tenant_id, username, attachment, item.StreamId));
            }

            _manager.StoreMailBody(mbox.TenantId, mbox.UserId, message_item);

            var previous_mailbox_id = mail_id == 0
                                          ? mbox.MailBoxId
                                          : _manager.GetMailInfo(tenant_id, username, mail_id, false, false).MailboxId;

            var previous_chain_id = message_item.ChainId;

            if (previous_mailbox_id != mbox.MailBoxId)
            {
                var prev_mbox = new MailBox();
                prev_mbox.TenantId  = tenant_id;
                prev_mbox.UserId    = username;
                prev_mbox.MailBoxId = previous_mailbox_id;
                previous_chain_id   = _manager.DetectChainId(prev_mbox, message_item);
            }

            mail_id         = _manager.MailSave(mbox, message_item, mail_id, message_item.Folder, message_item.Folder, string.Empty, string.Empty, false);
            message_item.Id = mail_id;

            if (previous_mailbox_id != mbox.MailBoxId)
            {
                _manager.UpdateChain(previous_chain_id, message_item.Folder, previous_mailbox_id, tenant_id, username);
            }

            if (mail_id > 0 && need_to_restore_attachments_from_fck_location)
            {
                foreach (var attachment in message_item.Attachments)
                {
                    var new_id = _manager.SaveAttachment(tenant_id, mail_id, attachment);
                    attachment.fileId = new_id;
                }
            }

            if (mail_id > 0 && embeded_attachments_for_saving.Any())
            {
                _manager.SaveAttachments(tenant_id, mail_id, embeded_attachments_for_saving);
            }

            _manager.UpdateChain(message_item.ChainId, message_item.Folder, mbox.MailBoxId, mbox.TenantId, mbox.UserId);

            return(message_item);
        }
Exemple #17
0
        public int Send(int tenant, string username, MailSendItem originalMessage, int mailId, int mailboxId)
        {
            var mbox = manager.GetUnremovedMailBox(mailboxId);

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

            originalMessage.MailboxId = mbox.MailBoxId;

            if (mbox.Name != "")
            {
                originalMessage.DisplayName = mbox.Name;
            }

            if (string.IsNullOrEmpty(originalMessage.HtmlBody))
            {
                originalMessage.HtmlBody = EMPTY_HTML_BODY;
            }

            var messageItem = SaveToDraft(originalMessage, mailId, mbox);

            if (messageItem.Id > 0)
            {
                var userCulture   = Thread.CurrentThread.CurrentCulture;
                var userUiCulture = Thread.CurrentThread.CurrentUICulture;
                var scheme        = HttpContext.Current.Request.GetUrlRewriter().Scheme;
                // move to_addresses temp
                manager.SetConversationsFolder(tenant, username, MailFolder.Ids.temp,
                                               new List <int> {
                    (Int32)messageItem.Id
                });
                manager.SetMessageFolderRestore(tenant, username, MailFolder.Ids.drafts,
                                                (int)messageItem.Id);
                ThreadPool.QueueUserWorkItem(delegate
                {
                    Message mimeMessage;
                    try
                    {
                        Thread.CurrentThread.CurrentCulture   = userCulture;
                        Thread.CurrentThread.CurrentUICulture = userUiCulture;

                        CoreContext.TenantManager.SetCurrentTenant(tenant);
                        SecurityContext.AuthenticateMe(new Guid(username));

                        ApiHelper.SetupScheme(scheme);

                        originalMessage.ChangeEmbededAttachmentLinks(tenant, username);
                        originalMessage.ChangeSmileLinks();

                        originalMessage.ChangeAttachedFileLinksAddresses(tenant);
                        originalMessage.ChangeAttachedFileLinksImages();

                        mimeMessage = originalMessage.ToMimeMessage(tenant, username, true);

                        var smptClient = MailClientBuilder.Smtp();

                        if (mbox.RefreshToken != null)
                        {
                            smptClient.SendSsl(mimeMessage, mbox.SmtpServer, mbox.SmtpPort,
                                               mbox.SmtpAccount, GetAccessToken(mbox),
                                               SaslMechanism.OAuth2);
                        }
                        else if (mbox.OutcomingEncryptionType == EncryptionType.None)
                        {
                            if (mbox.AuthenticationTypeSmtp == SaslMechanism.None)
                            {
                                smptClient.Send(mimeMessage, mbox.SmtpServer, mbox.SmtpPort);
                            }
                            else
                            {
                                smptClient.Send(mimeMessage, mbox.SmtpServer, mbox.SmtpPort,
                                                mbox.SmtpAccount, mbox.SmtpPassword,
                                                mbox.AuthenticationTypeSmtp);
                            }
                        }
                        else
                        {
                            if (mbox.AuthenticationTypeSmtp == SaslMechanism.None)
                            {
                                smptClient.SendSsl(mimeMessage, mbox.SmtpServer, mbox.SmtpPort,
                                                   mbox.OutcomingEncryptionType);
                            }
                            else
                            {
                                smptClient.SendSsl(mimeMessage, mbox.SmtpServer, mbox.SmtpPort,
                                                   mbox.SmtpAccount, mbox.SmtpPassword,
                                                   mbox.AuthenticationTypeSmtp,
                                                   mbox.OutcomingEncryptionType);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        AddNotificationAlertToMailbox(originalMessage, (Int32)messageItem.Id, ex, mbox);

                        // move to_addresses drafts
                        manager.SetConversationsFolder(tenant, username, MailFolder.Ids.drafts,
                                                       new List <int> {
                            (Int32)messageItem.Id
                        });
                        manager.SetMessageFolderRestore(tenant, username, MailFolder.Ids.drafts,
                                                        (int)messageItem.Id);

                        // send unsuccess notification
                        SendMailNotification(tenant, username, 1);

                        return;
                    }

                    SendMailNotification(tenant, username, 0);

                    try
                    {
                        // message was correctly send - lets update its chains id
                        var draftChainId = messageItem.ChainId;
                        // before moving message from draft to sent folder - lets recalculate its correct chain id
                        messageItem.ChainId = manager.DetectChainId(mbox, messageItem);
                        // push new message correct chain id to db
                        manager.UpdateMessageChainId(mbox, messageItem.Id, MailFolder.Ids.temp, draftChainId,
                                                     messageItem.ChainId);

                        manager.UpdateCrmLinkedChainId(mbox.MailBoxId, tenant, draftChainId,
                                                       messageItem.ChainId);

                        // move to_addresses sent
                        manager.SetConversationsFolder(tenant, username, MailFolder.Ids.sent,
                                                       new List <int> {
                            (Int32)messageItem.Id
                        });
                        manager.SetMessageFolderRestore(tenant, username, MailFolder.Ids.sent,
                                                        (int)messageItem.Id);

                        manager.AddRelationshipEventForLinkedAccounts(mbox, messageItem, log);

                        manager.SaveEmailInData(mbox, messageItem, log);

                        manager.SaveMailContacts(mbox.TenantId, mbox.UserId, mimeMessage);

                        StoreMessageToImapSentFolder(mbox, mimeMessage);
                    }
                    catch (Exception ex)
                    {
                        log.Error("Unexpected Error in Send(), message_item.Id = {0}, {1}, {2}",
                                  messageItem.Id, ex.ToString(), ex.StackTrace);
                    }
                });
            }
            else
            {
                throw new ArgumentException("Failed to_addresses save draft");
            }

            return(messageItem.Id > 0 ? (Int32)messageItem.Id : 1); // Callback in api will be raised if value > 0
        }
        public int Send(int tenantId, string username, MailSendItem item, int mailId)
        {
            var mbox = _manager.GetMailBox(tenantId, username, new MailAddress(item.From));

            if (mbox.Name != "")
            {
                item.DisplayName = (mbox.Name != "") ? mbox.Name : null;
            }

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

            string mime_message_id, in_reply_to;

            mailId = Save(tenantId, username, item, mailId, out mime_message_id, out in_reply_to);

            if (mailId != -1)
            {
                ThreadPool.QueueUserWorkItem(delegate
                {
                    ActiveUp.Net.Mail.Message message = null;
                    try
                    {
                        message           = item.CreateMimeMessage(tenantId, username, true);
                        message.MessageId = mime_message_id;
                        message.InReplyTo = in_reply_to;

                        if (!mbox.UseSsl)
                        {
                            if (!mbox.SmtpAuth)
                            {
                                ActiveUp.Net.Mail.SmtpClient.Send(message, mbox.SmtpServer, mbox.SmtpPort);
                            }
                            else
                            {
                                ActiveUp.Net.Mail.SmtpClient.Send(message, mbox.SmtpServer, mbox.SmtpPort, mbox.SmtpAccount, mbox.SmtpPassword, SaslMechanism.Login);
                            }
                        }
                        else
                        {
                            if (!mbox.SmtpAuth)
                            {
                                ActiveUp.Net.Mail.SmtpClient.SendSsl(message, mbox.SmtpServer, mbox.SmtpPort);
                            }
                            else
                            {
                                ActiveUp.Net.Mail.SmtpClient.SendSsl(message, mbox.SmtpServer, mbox.SmtpPort, mbox.SmtpAccount, mbox.SmtpPassword, SaslMechanism.Login);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        #region Error notification message
                        StringBuilder sbMessage      = new StringBuilder(1024);
                        MailSendItem messageDelivery = new MailSendItem();
                        messageDelivery.Subject      = "Message Delivery Failure";
                        messageDelivery.To           = new ASC.Mail.Aggregator.Collection.ItemList <string>()
                        {
                            item.From
                        };
                        messageDelivery.From      = MailBoxManager.MAIL_DAEMON_EMAIL;
                        messageDelivery.Important = true;
                        messageDelivery.StreamId  = Guid.NewGuid().ToString("N").ToLower();
                        sbMessage.Append("<span style=\"font-size: 15px;color:red;\">Message could not be delivered to_addresses recipient(s):</span><br/><ul>");
                        messageDelivery.NeedReSaveAttachments = false;
                        messageDelivery.Labels = new ASC.Mail.Aggregator.Collection.ItemList <int>();

                        for (int i = 0; i < item.To.Count; i++)
                        {
                            sbMessage.Append("<li>" + item.To[i] + "</li>");
                        }

                        sbMessage.AppendFormat("</ul><span style=\"font-size: 15px;color:red;\">Reason: {0}</span><br/>", ex.Message);
                        sbMessage.AppendFormat("<a id=\"id_delivery_failure_button\" href=\"#draftitem/{0}/\" class=\"baseLinkButton\" target=\"_self\" style=\"margin-top:10px;margin-bottom: 10px; color:white; text-decoration: none;\" style=\"margin-right:8px;\">Change your message and try again</a>", mailId.ToString());

                        if (message != null)
                        {
                            sbMessage.Append("<br/><span>Message headers follow:</span><br/><ul>");
                            var headers = message.HeaderFields;
                            for (int i = 0; i < headers.Count; i++)
                            {
                                var head = headers[i].ToString();
                                if (!String.IsNullOrEmpty(head))
                                {
                                    sbMessage.AppendFormat("<li>{0}</li>", head);
                                }
                            }
                            sbMessage.Append("</ul>");
                        }

                        messageDelivery.HtmlBody = sbMessage.ToString();
                        // Save To Inbox
                        MailMessageItem notify_message_item = messageDelivery.ToMailMessageItem(tenantId, username, false);
                        notify_message_item.IsNew           = true;
                        notify_message_item.IsFromCRM       = false;
                        notify_message_item.IsFromTL        = false;

                        _manager.MailSave(mbox, notify_message_item, 0, MailFolder.Inbox.Id, string.Empty, string.Empty, item.NeedReSaveAttachments);
                        #endregion
                    }
                });

                //Move to_addresses sent
                _manager.SetMessageFolder(tenantId, username, MailFolder.Sent.Id, new[] { mailId });
            }
            else
            {
                throw new ArgumentException("Failed to_addresses save draft");
            }

            return(mailId);
        }
        public int Send(int tenant_id, string username, MailSendItem item, int mail_id)
        {
            var mbox = manager.GetMailBox(tenant_id, username, new MailAddress(item.From));

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

            if (mbox.Name != "")
            {
                item.DisplayName = mbox.Name;
            }

            if (string.IsNullOrEmpty(item.HtmlBody))
            {
                item.HtmlBody = EmptyHtmlBody;
            }

            string mime_message_id, in_reply_to;
            var    message_item = SaveToDraft(tenant_id, username, item, mail_id, out mime_message_id, out in_reply_to, mbox);

            if (message_item.Id > 0)
            {
                var user_culture    = Thread.CurrentThread.CurrentCulture;
                var user_ui_culture = Thread.CurrentThread.CurrentUICulture;
                ThreadPool.QueueUserWorkItem(delegate
                {
                    try
                    {
                        Thread.CurrentThread.CurrentCulture   = user_culture;
                        Thread.CurrentThread.CurrentUICulture = user_ui_culture;
                        item.ChangeEmbededAttachmentLinks(tenant_id, username);
                        item.ChangeSmileLinks();
                        var message       = item.ToMimeMessage(tenant_id, username, true);
                        message.MessageId = mime_message_id;

                        in_reply_to = in_reply_to.Trim();
                        if (!string.IsNullOrEmpty(in_reply_to))
                        {
                            message.InReplyTo = in_reply_to;
                        }

                        if (mbox.RefreshToken != null)
                        {
                            ActiveUp.Net.Mail.SmtpClient.SendSsl(message, mbox.SmtpServer, mbox.SmtpPort, mbox.SmtpAccount, GetAccessToken(mbox), SaslMechanism.OAuth2);
                        }
                        else if (mbox.OutcomingEncryptionType == EncryptionType.None)
                        {
                            if (mbox.AuthenticationTypeSmtp == SaslMechanism.None)
                            {
                                ActiveUp.Net.Mail.SmtpClient.Send(message, mbox.SmtpServer, mbox.SmtpPort);
                            }
                            else
                            {
                                ActiveUp.Net.Mail.SmtpClient.Send(message, mbox.SmtpServer, mbox.SmtpPort, mbox.SmtpAccount, mbox.SmtpPassword, mbox.AuthenticationTypeSmtp);
                            }
                        }
                        else
                        {
                            if (mbox.AuthenticationTypeSmtp == SaslMechanism.None)
                            {
                                ActiveUp.Net.Mail.SmtpClient.SendSsl(message, mbox.SmtpServer, mbox.SmtpPort, mbox.OutcomingEncryptionType);
                            }
                            else
                            {
                                ActiveUp.Net.Mail.SmtpClient.SendSsl(message, mbox.SmtpServer, mbox.SmtpPort, mbox.SmtpAccount, mbox.SmtpPassword, mbox.AuthenticationTypeSmtp, mbox.OutcomingEncryptionType);
                            }
                        }

                        // message was correctly send - lets update its chains id
                        var draft_chain_id = message_item.ChainId;
                        // before moving message from draft to sent folder - lets recalculate its correct chain id
                        message_item.ChainId = manager.DetectChainId(mbox, message_item);
                        // push new message correct chain id to db
                        manager.UpdateMessageChainId(mbox, message_item.Id, MailFolder.Ids.drafts, draft_chain_id, message_item.ChainId);

                        manager.UpdateCrmLinkedChainId(mbox.MailBoxId, tenant_id, draft_chain_id, message_item.ChainId);

                        //Move to_addresses sent
                        manager.SetConversationsFolder(tenant_id, username, MailFolder.Ids.sent, new List <int> {
                            (Int32)message_item.Id
                        });
                        manager.SetMessageFolderRestore(tenant_id, username, MailFolder.Ids.sent, (int)message_item.Id);

                        manager.AddRelationshipEventForLinkedAccounts(mbox, message_item);

                        foreach (var handler in message_handlers)
                        {
                            try
                            {
                                handler.HandleRetrievedMessage(mbox,
                                                               message,
                                                               message_item,
                                                               MailFolder.Ids.sent,
                                                               string.Empty,
                                                               string.Empty,
                                                               message_item.IsNew,
                                                               message_item.TagIds != null ? message_item.TagIds.ToArray() : new int[0]);
                            }
                            catch (Exception ex)
                            {
                                log.Error(ex, "MailSendQueue::Send");
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        AddNotificationAlertToMailbox(tenant_id, username, item, message_item, ex, mbox);
                    }
                });
            }
            else
            {
                throw new ArgumentException("Failed to_addresses save draft");
            }

            return(message_item.Id > 0 ? (Int32)message_item.Id : 1); // Callback in api will be raised if value > 0
        }
 protected override int DB_SendMessage(int id, MailSendItem item)
 {
     return(mailSendQueue.Send(TenantId, Username, item, id));
 }