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);
        }
        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);
        }
        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 #4
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);
        }
        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);
        }