Esempio n. 1
0
        public void UpdateDataFromInternalDatabase(string hostname, MailServerInfo mailServer)
        {
            DemandPermission();

            using var transaction = MailDbContext.Database.BeginTransaction();

            var mailboxProvider = new MailboxProvider
            {
                Id   = 0,
                Name = hostname
            };

            var pReq = MailDbContext.MailboxProvider.Add(mailboxProvider);

            MailDbContext.SaveChanges();
            mailboxProvider = pReq.Entity;

            var providerId = mailboxProvider.Id;

            var mailboxServer = new MailboxServer
            {
                Id             = 0,
                IdProvider     = providerId,
                Type           = "smtp",
                Hostname       = hostname,
                Port           = 587,
                SocketType     = "STARTTLS",
                UserName       = "******",
                Authentication = "",
                IsUserData     = false
            };

            var req = MailDbContext.MailboxServer.Add(mailboxServer);

            MailDbContext.SaveChanges();

            mailboxServer = req.Entity;

            var smtpServerId = mailboxServer.Id;

            mailboxServer = new MailboxServer
            {
                Id             = 0,
                IdProvider     = providerId,
                Type           = "imap",
                Hostname       = hostname,
                Port           = 143,
                SocketType     = "STARTTLS",
                UserName       = "******",
                Authentication = "",
                IsUserData     = false
            };

            req = MailDbContext.MailboxServer.Add(mailboxServer);
            MailDbContext.SaveChanges();

            mailboxServer = req.Entity;

            var imapServerId = mailboxServer.Id;

            var mailServerData = MailDbContext.ServerServer.FirstOrDefault();

            var connectionString = Newtonsoft.Json.JsonConvert.SerializeObject(mailServer);

            var server = new ServerServer
            {
                Id               = 0,
                MxRecord         = hostname,
                ConnectionString = connectionString,
                ServerType       = 2,
                SmtpSettingsId   = smtpServerId,
                ImapSettingsId   = imapServerId
            };

            MailDbContext.ServerServer.Add(server);
            MailDbContext.SaveChanges();

            if (mailServerData != null)
            {
                server = MailDbContext.ServerServer.Where(r => r.Id == mailServerData.Id).FirstOrDefault();
                MailDbContext.ServerServer.Remove(server);
                MailDbContext.SaveChanges();

                providerId = MailDbContext.MailboxServer
                             .Where(r => r.Id == mailServerData.SmtpSettingsId)
                             .Select(r => r.IdProvider)
                             .FirstOrDefault();

                var providers = MailDbContext.MailboxProvider.Where(r => r.Id == providerId).ToList();
                MailDbContext.MailboxProvider.RemoveRange(providers);
                MailDbContext.SaveChanges();

                var servers = MailDbContext.MailboxServer
                              .Where(r => new[] { mailServerData.SmtpSettingsId, mailServerData.ImapSettingsId }.Any(a => a == r.Id))
                              .ToList();

                MailDbContext.MailboxServer.RemoveRange(servers);
                MailDbContext.SaveChanges();

                var mailboxId = MailDbContext.Mailbox
                                .Where(r => r.IdSmtpServer == mailServerData.SmtpSettingsId)
                                .Where(r => r.IdInServer == mailServerData.ImapSettingsId)
                                .ToArray();

                foreach (var m in mailboxId)
                {
                    m.IdSmtpServer = smtpServerId;
                    m.IdInServer   = imapServerId;
                }
                MailDbContext.SaveChanges();
            }

            transaction.Commit();

            MailServiceHelperStorage.Remove();
        }
 public MailboxServerIdParameter(MailboxServer server) : base(server.Id)
 {
 }
Esempio n. 3
0
 public DatabaseCopyIdParameter(MailboxServer server) : base("*\\" + server.Identity)
 {
     this.Initialize(base.RawIdentity);
 }
Esempio n. 4
0
        private static int GetMailboxServerId(IMailboxServerDao daoMailboxServer, MailboxServer dbServer,
                                              MailboxServer newServer, List <MailboxServer> trustedServers)
        {
            int serverId;

            if (!dbServer.Equals(newServer))
            {
                // Server settings have been changed
                if (dbServer.IsUserData)
                {
                    if (trustedServers != null)
                    {
                        var foundInServer = trustedServers.FirstOrDefault(ts => ts.Equals(newServer));
                        if (foundInServer != null)
                        {
                            daoMailboxServer.DelteServer(dbServer.Id);
                            newServer.Id         = foundInServer.Id;
                            newServer.IsUserData = false;
                        }
                        else
                        {
                            newServer.Id = dbServer.Id;
                            newServer.Id = SaveMailboxServer(daoMailboxServer, newServer, dbServer.ProviderId);
                        }
                    }
                    else
                    {
                        newServer.Id = dbServer.Id;
                        newServer.Id = SaveMailboxServer(daoMailboxServer, newServer, dbServer.ProviderId);
                    }
                }
                else
                {
                    if (trustedServers != null)
                    {
                        var foundInServer = trustedServers.FirstOrDefault(ts => ts.Equals(newServer));
                        if (foundInServer != null)
                        {
                            newServer.Id         = foundInServer.Id;
                            newServer.IsUserData = false;
                        }
                        else
                        {
                            newServer.Id = SaveMailboxServer(daoMailboxServer, newServer, dbServer.ProviderId);
                        }
                    }
                    else
                    {
                        newServer.Id = SaveMailboxServer(daoMailboxServer, newServer, dbServer.ProviderId);
                    }
                }

                serverId = newServer.Id;
            }
            else
            {
                serverId = dbServer.Id;
            }

            return(serverId);
        }
Esempio n. 5
0
        public bool SaveMailBox(MailBoxData mailbox, AuthorizationServiceType authType = AuthorizationServiceType.None)
        {
            if (mailbox == null)
            {
                throw new ArgumentNullException("mailbox");
            }

            if (mailbox.IsTeamlab)
            {
                throw new ArgumentException("Mailbox with specified email can't be updated");
            }

            using (var daoFactory = new DaoFactory())
            {
                using (var tx = daoFactory.DbManager.BeginTransaction())
                {
                    var daoMailbox         = daoFactory.CreateMailboxDao();
                    var daoMailboxServer   = daoFactory.CreateMailboxServerDao();
                    var daoMailboxDomain   = daoFactory.CreateMailboxDomainDao();
                    var daoMailboxProvider = daoFactory.CreateMailboxProviderDao();

                    var existingMailbox = daoMailbox.GetMailBox(
                        new СoncreteUserMailboxExp(
                            mailbox.EMail,
                            mailbox.TenantId, mailbox.UserId));

                    int newInServerId, newOutServerId;

                    var mailboxId   = 0;
                    var dateCreated = DateTime.UtcNow;
                    var enabled     = true;
                    var host        = authType == AuthorizationServiceType.Google ? Defines.GOOGLE_HOST : mailbox.EMail.Host;

                    // Get new imap/pop3 server from MailBoxData
                    var newInServer = new MailboxServer
                    {
                        Hostname       = mailbox.Server,
                        Port           = mailbox.Port,
                        Type           = mailbox.Imap ? Defines.IMAP : Defines.POP3,
                        Username       = mailbox.EMail.ToLoginFormat(mailbox.Account) ?? mailbox.Account,
                        SocketType     = mailbox.Encryption.ToNameString(),
                        Authentication = mailbox.Authentication.ToNameString()
                    };

                    // Get new smtp server from MailBoxData
                    var newOutServer = new MailboxServer
                    {
                        Hostname = mailbox.SmtpServer,
                        Port     = mailbox.SmtpPort,
                        Type     = Defines.SMTP,
                        Username =
                            mailbox.SmtpAuthentication != SaslMechanism.None
                                ? mailbox.EMail.ToLoginFormat(mailbox.SmtpAccount) ?? mailbox.SmtpAccount
                                : "",
                        SocketType     = mailbox.SmtpEncryption.ToNameString(),
                        Authentication = mailbox.SmtpAuthentication.ToNameString()
                    };

                    if (existingMailbox != null)
                    {
                        mailboxId   = existingMailbox.Id;
                        enabled     = existingMailbox.Enabled;
                        dateCreated = existingMailbox.DateCreated;

                        // Get existing settings by existing ids
                        var dbInServer  = daoMailboxServer.GetServer(existingMailbox.ServerId);
                        var dbOutServer = daoMailboxServer.GetServer(existingMailbox.SmtpServerId);

                        // Compare existing settings with new
                        if (!dbInServer.Equals(newInServer) || !dbOutServer.Equals(newOutServer))
                        {
                            var domain = daoMailboxDomain.GetDomain(host);

                            List <MailboxServer> trustedServers = null;
                            if (domain != null)
                            {
                                trustedServers = daoMailboxServer.GetServers(domain.ProviderId);
                            }

                            newInServerId  = GetMailboxServerId(daoMailboxServer, dbInServer, newInServer, trustedServers);
                            newOutServerId = GetMailboxServerId(daoMailboxServer, dbOutServer, newOutServer,
                                                                trustedServers);
                        }
                        else
                        {
                            newInServerId  = existingMailbox.ServerId;
                            newOutServerId = existingMailbox.SmtpServerId;
                        }
                    }
                    else
                    {
                        //Find settings by host

                        var domain = daoMailboxDomain.GetDomain(host);

                        if (domain != null)
                        {
                            //Get existing servers with isUserData = 0
                            var trustedServers = daoMailboxServer.GetServers(domain.ProviderId);

                            //Compare existing settings with new

                            var foundInServer  = trustedServers.FirstOrDefault(ts => ts.Equals(newInServer));
                            var foundOutServer = trustedServers.FirstOrDefault(ts => ts.Equals(newOutServer));

                            //Use existing or save new servers
                            newInServerId = foundInServer != null
                                ? foundInServer.Id
                                : SaveMailboxServer(daoMailboxServer, newInServer, domain.ProviderId);

                            newOutServerId = foundOutServer != null
                                ? foundOutServer.Id
                                : SaveMailboxServer(daoMailboxServer, newOutServer, domain.ProviderId);
                        }
                        else
                        {
                            //Save new servers
                            var newProvider = new MailboxProvider
                            {
                                Id               = 0,
                                Name             = host,
                                DisplayShortName = "",
                                DisplayName      = "",
                                Url              = ""
                            };

                            newProvider.Id = daoMailboxProvider.SaveProvider(newProvider);

                            var newDomain = new MailboxDomain
                            {
                                Id         = 0,
                                Name       = host,
                                ProviderId = newProvider.Id
                            };

                            daoMailboxDomain.SaveDomain(newDomain);

                            newInServerId  = SaveMailboxServer(daoMailboxServer, newInServer, newProvider.Id);
                            newOutServerId = SaveMailboxServer(daoMailboxServer, newOutServer, newProvider.Id);
                        }
                    }

                    var loginDelayTime = GetLoginDelayTime(mailbox);

                    //Save Mailbox to DB
                    var mb = new Mailbox
                    {
                        Id           = mailboxId,
                        Tenant       = mailbox.TenantId,
                        User         = mailbox.UserId,
                        Address      = mailbox.EMail.Address.ToLowerInvariant(),
                        Name         = mailbox.Name,
                        Password     = mailbox.Password,
                        MsgCountLast = mailbox.MessagesCount,
                        SmtpPassword = mailbox.SmtpPassword,
                        SizeLast     = mailbox.Size,
                        LoginDelay   = loginDelayTime,
                        Enabled      = enabled,
                        Imap         = mailbox.Imap,
                        BeginDate    = mailbox.BeginDate,
                        OAuthType    = mailbox.OAuthType,
                        OAuthToken   = mailbox.OAuthToken,
                        ServerId     = newInServerId,
                        SmtpServerId = newOutServerId,
                        DateCreated  = dateCreated
                    };

                    var mailBoxId = daoMailbox.SaveMailBox(mb);

                    mailbox.MailBoxId = mailBoxId;

                    if (mailBoxId < 1)
                    {
                        tx.Rollback();
                        return(false);
                    }

                    tx.Commit();
                }
            }

            return(true);
        }
Esempio n. 6
0
        public static MailBoxData ToMailBoxData(Mailbox mailbox, MailboxServer inServer, MailboxServer outServer,
                                                MailboxAutoreply autoreply)
        {
            var address = new MailAddress(mailbox.Address);

            var mailAutoReply = autoreply != null
                ? new MailAutoreplyData(autoreply.MailboxId, autoreply.Tenant, autoreply.TurnOn, autoreply.OnlyContacts,
                                        autoreply.TurnOnToDate, autoreply.FromDate, autoreply.ToDate, autoreply.Subject, autoreply.Html)
                : null;

            var inServerOldFormat  = string.Format("{0}:{1}", inServer.Hostname, inServer.Port);
            var outServerOldFormat = string.Format("{0}:{1}", outServer.Hostname, outServer.Port);

            var mailboxData = new MailBoxData(mailbox.Tenant, mailbox.User, mailbox.Id, mailbox.Name, address,
                                              address.ToLogin(inServer.Username), mailbox.Password, inServerOldFormat,
                                              inServer.SocketType.ToEncryptionType(), inServer.Authentication.ToSaslMechanism(), mailbox.Imap,
                                              address.ToLogin(outServer.Username), mailbox.SmtpPassword, outServerOldFormat,
                                              outServer.SocketType.ToEncryptionType(), outServer.Authentication.ToSaslMechanism(),
                                              Convert.ToByte(mailbox.OAuthType), mailbox.OAuthToken)
            {
                Size                 = mailbox.SizeLast,
                MessagesCount        = mailbox.MsgCountLast,
                ServerLoginDelay     = mailbox.LoginDelay,
                BeginDate            = mailbox.BeginDate,
                QuotaError           = mailbox.QuotaError,
                AuthErrorDate        = mailbox.DateAuthError,
                ImapIntervalsJson    = mailbox.ImapIntervals,
                SmtpServerId         = mailbox.SmtpServerId,
                InServerId           = mailbox.ServerId,
                EMailInFolder        = mailbox.EmailInFolder,
                MailAutoreply        = mailAutoReply,
                AccessTokenRefreshed = false, //TODO: ???

                Enabled   = mailbox.Enabled,
                IsRemoved = mailbox.IsRemoved,
                IsTeamlab = mailbox.IsTeamlabMailbox
            };

            return(mailboxData);
        }