Example #1
0
        public DraftManager(MailBoxManager manager, ILogger log, DeliveryFailureMessageTranslates daemonLabels = null)
        {
            DaemonLabels = daemonLabels ?? DeliveryFailureMessageTranslates.Defauilt;
            this.manager = manager;
            this.log     = log;
            if (_signalrServiceClient != null)
            {
                return;
            }
            var enableSignalr = string.IsNullOrEmpty(ConfigurationManager.AppSettings["web.hub"]) ? "false" : "true";

            _signalrServiceClient = new SignalrServiceClient(enableSignalr);
        }
Example #2
0
        public DraftManager(MailBoxManager manager, ILogger log, DeliveryFailureMessageTranslates daemonLabels = null,
                            bool isAutoreply = false)
        {
            DaemonLabels          = daemonLabels ?? DeliveryFailureMessageTranslates.Defauilt;
            this.manager          = manager;
            this.log              = log;
            _isAutoreply          = isAutoreply;
            _sslCertificatePermit = ConfigurationManager.AppSettings["mail.certificate-permit"] != null &&
                                    Convert.ToBoolean(ConfigurationManager.AppSettings["mail.certificate-permit"]);

            if (_signalrServiceClient != null)
            {
                return;
            }
            _signalrServiceClient = new SignalrServiceClient("mail");
        }
        public ComposeEngineBase(int tenant, string user, DeliveryFailureMessageTranslates daemonLabels = null, ILog log = null)
        {
            Tenant = tenant;
            User   = user;

            Log = log ?? LogManager.GetLogger("ASC.Mail.ComposeEngineBase");

            DaemonLabels = daemonLabels ?? DeliveryFailureMessageTranslates.Defauilt;

            _sslCertificatePermit = Defines.SslCertificatesErrorPermit;

            if (_signalrServiceClient != null)
            {
                return;
            }
            _signalrServiceClient = new SignalrServiceClient("mail");
        }
        public virtual MailMessage Save(
            int id,
            string from,
            List <string> to,
            List <string> cc,
            List <string> bcc,
            string mimeReplyToId,
            bool importance,
            string subject,
            List <int> tags,
            string body,
            List <MailAttachmentData> attachments,
            string calendarIcs,
            DeliveryFailureMessageTranslates translates = null)
        {
            var mailAddress = new MailAddress(from);

            var engine = new EngineFactory(Tenant, User);

            var accounts = engine.AccountEngine.GetAccountInfoList().ToAccountData();

            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("Saving emails from a group address is forbidden");
            }

            var mbox = engine.MailboxEngine.GetMailboxData(
                new СoncreteUserMailboxExp(account.MailboxId, Tenant, User));

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

            string mimeMessageId, streamId;

            var previousMailboxId = mbox.MailBoxId;

            if (id > 0)
            {
                var message = engine.MessageEngine.GetMessage(id, new MailMessage.Options
                {
                    LoadImages    = false,
                    LoadBody      = true,
                    NeedProxyHttp = Defines.NeedProxyHttp,
                    NeedSanitizer = false
                });

                if (message.Folder != FolderType.Draft)
                {
                    throw new InvalidOperationException("Saving emails is permitted only in the Drafts folder");
                }

                if (message.HtmlBody.Length > Defines.MaximumMessageBodySize)
                {
                    throw new InvalidOperationException("Message body exceeded limit (" + Defines.MaximumMessageBodySize / 1024 + " KB)");
                }

                mimeMessageId = message.MimeMessageId;

                streamId = message.StreamId;

                previousMailboxId = message.MailboxId;
            }
            else
            {
                mimeMessageId = MailUtil.CreateMessageId();
                streamId      = MailUtil.CreateStreamId();
            }

            var fromAddress = MailUtil.CreateFullEmail(mbox.Name, mbox.EMail.Address);

            var compose = new MailDraftData(id, mbox, fromAddress, to, cc, bcc, subject, mimeMessageId, mimeReplyToId, importance,
                                            tags, body, streamId, attachments, calendarIcs)
            {
                PreviousMailboxId = previousMailboxId
            };

            DaemonLabels = translates ?? DeliveryFailureMessageTranslates.Defauilt;

            return(Save(compose));
        }
Example #5
0
        public long Send(int id,
                         string from,
                         List <string> to,
                         List <string> cc,
                         List <string> bcc,
                         string mimeReplyToId,
                         bool importance,
                         string subject,
                         List <int> tags,
                         string body,
                         List <MailAttachmentData> attachments,
                         Files.Core.Security.FileShare fileLinksShareMode,
                         string calendarIcs,
                         bool isAutoreply,
                         bool requestReceipt,
                         bool requestRead,
                         DeliveryFailureMessageTranslates translates = null)
        {
            if (id < 1)
            {
                id = 0;
            }

            if (string.IsNullOrEmpty(from))
            {
                throw new ArgumentNullException("from");
            }

            if (!to.Any())
            {
                throw new ArgumentNullException("to");
            }

            var mailAddress = new MailAddress(from);

            var engine = new EngineFactory(Tenant, User);

            var accounts = engine.AccountEngine.GetAccountInfoList().ToAccountData();

            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");
            }

            var mbox = engine.MailboxEngine.GetMailboxData(
                new СoncreteUserMailboxExp(account.MailboxId, Tenant, User));

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

            if (!mbox.Enabled)
            {
                throw new InvalidOperationException("Sending emails from a disabled account is forbidden");
            }

            string mimeMessageId, streamId;

            var previousMailboxId = mbox.MailBoxId;

            if (id > 0)
            {
                var message = engine.MessageEngine.GetMessage(id, new MailMessage.Options
                {
                    LoadImages    = false,
                    LoadBody      = true,
                    NeedProxyHttp = Defines.NeedProxyHttp,
                    NeedSanitizer = false
                });

                if (message.Folder != FolderType.Draft && message.Folder != FolderType.Templates)
                {
                    throw new InvalidOperationException("Sending emails is permitted only in the Drafts folder");
                }

                if (message.HtmlBody.Length > Defines.MaximumMessageBodySize)
                {
                    throw new InvalidOperationException("Message body exceeded limit (" + Defines.MaximumMessageBodySize / 1024 + " KB)");
                }

                mimeMessageId = message.MimeMessageId;

                streamId = message.StreamId;

                foreach (var attachment in attachments)
                {
                    attachment.streamId = streamId;
                }

                previousMailboxId = message.MailboxId;
            }
            else
            {
                mimeMessageId = MailUtil.CreateMessageId();
                streamId      = MailUtil.CreateStreamId();
            }

            var fromAddress = MailUtil.CreateFullEmail(mbox.Name, mailAddress.Address);

            var draft = new MailDraftData(id, mbox, fromAddress, to, cc, bcc, subject, mimeMessageId, mimeReplyToId,
                                          importance, tags, body, streamId, attachments, calendarIcs)
            {
                FileLinksShareMode = fileLinksShareMode,
                PreviousMailboxId  = previousMailboxId,
                RequestReceipt     = requestReceipt,
                RequestRead        = requestRead,
                IsAutogenerated    = !string.IsNullOrEmpty(calendarIcs),
                IsAutoreplied      = isAutoreply
            };

            DaemonLabels = translates ?? DeliveryFailureMessageTranslates.Defauilt;

            return(Send(draft));
        }
Example #6
0
 public DraftEngine(int tenant, string user, DeliveryFailureMessageTranslates daemonLabels = null, ILog log = null)
     : base(tenant, user, daemonLabels, log)
 {
     Log = log ?? LogManager.GetLogger("ASC.Mail.DraftEngine");
 }
Example #7
0
        public bool SimpleSend(
            string from,
            List <string> to,
            string subject,
            string body,
            bool isReceipt,
            DeliveryFailureMessageTranslates translates = null)
        {
            if (!to.Any())
            {
                throw new ArgumentNullException("to");
            }

            if (string.IsNullOrEmpty(from))
            {
                throw new ArgumentNullException("from");
            }

            var mailAddress = new MailAddress(from);

            var engine = new EngineFactory(Tenant, User);

            var accounts = engine.AccountEngine.GetAccountInfoList().ToAccountData();

            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");
            }

            var mbox = engine.MailboxEngine.GetMailboxData(
                new СoncreteUserMailboxExp(account.MailboxId, Tenant, User));

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

            if (!mbox.Enabled)
            {
                throw new InvalidOperationException("Sending emails from a disabled account is forbidden");
            }

            var previousMailboxId = mbox.MailBoxId;

            var mimeMessageId = MailUtil.CreateMessageId();
            var streamId      = MailUtil.CreateStreamId();

            var fromAddress = MailUtil.CreateFullEmail(mbox.Name, mailAddress.Address);

            var draft = new MailDraftData(0, mbox, fromAddress, to, new List <string>(), new List <string>(), subject, mimeMessageId, string.Empty,
                                          false, new List <int>(), body, streamId, new List <MailAttachmentData>(), string.Empty)
            {
                FileLinksShareMode = (Files.Core.Security.FileShare)FileShare.None,
                PreviousMailboxId  = previousMailboxId,
                RequestReceipt     = false,
                RequestRead        = false,
                IsAutogenerated    = false,
                IsAutoreplied      = false,
                IsReceipt          = isReceipt
            };

            DaemonLabels = translates ?? DeliveryFailureMessageTranslates.Defauilt;

            var scheme = HttpContext.Current == null
                ? Uri.UriSchemeHttp
                : HttpContext.Current.Request.GetUrlRewriter().Scheme;

            var currentUrl = HttpContext.Current != null?HttpContext.Current.Request.GetUrlRewriter().ToString() : null;

            Task.Run(() =>
            {
                try
                {
                    if (HttpContext.Current == null && !WorkContext.IsMono)
                    {
                        HttpContext.Current = new HttpContext(
                            new HttpRequest("hack", currentUrl, string.Empty),
                            new HttpResponse(new StringWriter()));
                    }

                    CoreContext.TenantManager.SetCurrentTenant(draft.Mailbox.TenantId);

                    SecurityContext.CurrentUser = new Guid(draft.Mailbox.UserId);

                    draft.ChangeEmbeddedAttachmentLinks(Log);

                    var mimeMessage = draft.ToMimeMessage();

                    using (var mc = new MailClient(draft.Mailbox, CancellationToken.None,
                                                   certificatePermit: draft.Mailbox.IsTeamlab || _sslCertificatePermit, log: Log,
                                                   enableDsn: draft.RequestReceipt))
                    {
                        mc.Send(mimeMessage, false);
                    }

                    Log.Info($"User {draft.Mailbox.UserId} sent a read confirmation to {string.Join(",", draft.To)} from {draft.From}");

                    SendMailNotification(draft);
                }
                catch (Exception ex)
                {
                    Log.ErrorFormat("Mail->Send failed: Exception: {0}", ex.ToString());

                    SendMailErrorNotification(draft, MailNotificationState.SendReceiptError);
                }
            });

            return(true);
        }