Ejemplo n.º 1
0
        private ImapClient GetImapClient(string mailServerUId, string mailboxName, string mailboxPassword)
        {
            var currentMailServer = new Terrasoft.Configuration.MailServer(UserConnection);

            if (!currentMailServer.FetchFromDB(mailServerUId) || !currentMailServer.GetTypedColumnValue <bool>("AllowEmailDownloading"))
            {
                return(null);
            }
            var imapServerCredentials = new MailCredentials {
                Host         = currentMailServer.GetTypedColumnValue <string>("Address"),
                Port         = currentMailServer.GetTypedColumnValue <int>("Port"),
                UseSsl       = currentMailServer.GetTypedColumnValue <bool>("UseSSL"),
                StartTls     = currentMailServer.GetTypedColumnValue <bool>("IsStartTls"),
                UserName     = mailboxName,
                UserPassword = mailboxPassword
            };
            ImapClient imapClient;
            SynchronizationErrorHelper helper = SynchronizationErrorHelper.GetInstance(UserConnection);

            try {
                imapClient = new ImapClient(imapServerCredentials, MailSynchronizer.GetImapErrorMessages(UserConnection), UserConnection, true);
            } catch (Exception ex) {
                helper.ProcessSynchronizationError(mailboxName, ex, true);
                throw;
            }
            return(imapClient);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Stops synchronization process for the specific sender email.
        /// </summary>
        /// <param name="senderEmailAddress">Sender email address.</param>
        /// <param name="serverTypeId">Mail server type identifier.</param>
        protected virtual void StopSyncProcess(string senderEmailAddress, Guid serverTypeId)
        {
            var parameters = new Dictionary <string, object> {
                { "SenderEmailAddress", senderEmailAddress },
                { "CurrentUserId", UserConnection.CurrentUser.Id }
            };

            MailSynchronizer.RemoveSyncJob(UserConnection, parameters);
        }
Ejemplo n.º 3
0
        public virtual bool ScriptTask1Execute(ProcessExecutingContext context)
        {
                        #if !NETSTANDARD2_0 // TODO CRM-42546
            var secureText          = UserConnection.DataValueTypeManager.GetInstanceByName("SecureText") as SecureTextDataValueType;
            var currentUserId       = CurrentUserId != Guid.Empty ? CurrentUserId : UserConnection.CurrentUser.Id;
            EntitySchemaQuery query = new EntitySchemaQuery(UserConnection.EntitySchemaManager,
                                                            "MailboxSyncSettings");
            query.AddColumn("UserName");
            query.AddColumn("UserPassword");
            query.AddColumn("MailServer.Address");
            query.AddColumn("MailServer.Port");
            query.AddColumn("MailServer.UseSSL");
            query.AddColumn("MailServer.IsStartTls");
            query.AddColumn("SenderEmailAddress");
            query.Filters.Add(query.CreateFilterWithParameters(FilterComparisonType.Equal,
                                                               "SysAdminUnit.Id", currentUserId));
            query.Filters.Add(query.CreateFilterWithParameters(FilterComparisonType.Equal,
                                                               "MailServer.AllowEmailDownloading", true));
            query.Filters.Add(query.CreateFilterWithParameters(FilterComparisonType.Equal,
                                                               "EnableMailSynhronization", true));
            query.Filters.Add(query.CreateFilterWithParameters(FilterComparisonType.Equal,
                                                               "SenderEmailAddress", SenderEmailAddress));
            var select = query.GetSelectQuery(UserConnection);

            MailCredentials credentials = new MailCredentials();
            using (var dbExecutor = UserConnection.EnsureDBConnection()) {
                using (var reader = select.ExecuteReader(dbExecutor)) {
                    if (!reader.Read())
                    {
                        var parameters = new Dictionary <string, object> {
                            { "SenderEmailAddress", SenderEmailAddress },
                            { "CurrentUserId", currentUserId }
                        };
                        MailSynchronizer.RemoveSyncJob(UserConnection, parameters);
                        throw new ArgumentException("user does not have mail account");
                    }
                    credentials.UserName = reader.GetColumnValue <string>(reader.GetName(0));
                    string rawPassword = reader.GetColumnValue <string>(reader.GetName(1));
                    if (!rawPassword.IsNullOrEmpty())
                    {
                        credentials.UserPassword = secureText.GetValueForLoad(UserConnection, rawPassword).ToString();
                    }
                    credentials.Host               = reader.GetColumnValue <string>(reader.GetName(2));
                    credentials.Port               = reader.GetColumnValue <int>(reader.GetName(3));
                    credentials.UseSsl             = reader.GetColumnValue <bool>(reader.GetName(4));
                    credentials.StartTls           = reader.GetColumnValue <bool>(reader.GetName(5));
                    credentials.SenderEmailAddress = reader.GetColumnValue <string>(reader.GetName(6));
                }
            }
            using (var mailSynchronizer = new MailSynchronizer(UserConnection, credentials)) {
                mailSynchronizer.ValidateImapClient();
                mailSynchronizer.SyncImapMail();
            }
                        #endif
            return(true);
        }
Ejemplo n.º 4
0
 public string CreateDeleteSyncJob(bool create, int interval)
 {
     try {
         if (create)
         {
             MailSynchronizer.CreateSyncJob(UserConnection, interval);
         }
         else
         {
             MailSynchronizer.RemoveSyncJob(UserConnection);
         }
     } catch (ImapException e) {
         return(e.Message);
     }
     return(string.Empty);
 }
Ejemplo n.º 5
0
 /// <summary>
 /// Stops synchronization process for the specific sender email.
 /// </summary>
 /// <param name="senderEmailAddress">Sender email address.</param>
 /// <param name="serverTypeId">Mail server type identifier.</param>
 protected override void StopSyncProcess(string senderEmailAddress, Guid serverTypeId)
 {
     if (serverTypeId == ExchangeConsts.ExchangeMailServerTypeId)
     {
         var exchangeUtility = ClassFactory.Get <IExchangeUtility>();
         exchangeUtility.RemoveAllSyncJob(UserConnection, senderEmailAddress, serverTypeId);
     }
     else
     {
         var parameters = new Dictionary <string, object> {
             { "SenderEmailAddress", senderEmailAddress },
             { "CurrentUserId", UserConnection.CurrentUser.Id }
         };
         MailSynchronizer.RemoveSyncJob(UserConnection, parameters);
     }
 }
Ejemplo n.º 6
0
        /// <summary>
        /// Creates (or deletes, if necessary) Imap synchronization job.
        /// </summary>
        /// <param name="userConnection">User connection.</param>
        /// <param name="interval">Synchronization interval.</param>
        /// <param name="create">Determines if synchronization job creation is necessary.</param>
        /// <param name="senderEmailAddress">Sender email address.</param>
        private string CreateDeleteImapJob(UserConnection userConnection, int interval, bool create,
                                           string senderEmailAddress)
        {
            var parameters = new Dictionary <string, object> {
                { "SenderEmailAddress", senderEmailAddress },
                { "CurrentUserId", userConnection.CurrentUser.Id }
            };

            if (create)
            {
                MailSynchronizer.CreateSyncJob(userConnection, interval, parameters);
            }
            else
            {
                MailSynchronizer.RemoveSyncJob(userConnection, parameters);
            }
            return(string.Empty);
        }
Ejemplo n.º 7
0
        public bool CreateBPMOnlineFolder(Guid id, string userName, string userPassword)
        {
            var currentMailServer = new MailServer(UserConnection);

            if (!currentMailServer.FetchFromDB(id))
            {
                return(false);
            }
            var imapServerCredentials = new MailCredentials {
                Host         = currentMailServer.GetTypedColumnValue <string>("Address"),
                Port         = currentMailServer.GetTypedColumnValue <int>("Port"),
                UseSsl       = currentMailServer.GetTypedColumnValue <bool>("UseSSL"),
                StartTls     = currentMailServer.GetTypedColumnValue <bool>("IsStartTls"),
                UserName     = userName,
                UserPassword = userPassword
            };
            var imapClient = new ImapClient(imapServerCredentials, MailSynchronizer.GetImapErrorMessages(UserConnection), UserConnection);

            imapClient.EnsureFolderExists("BPMonline");
            var mailboxFolderTypeId = new Guid("99c2351c-f0f8-e111-9dba-00155d051801");
            var emailFolderTypeId   = new Guid("b97a5836-1cd0-e111-90c6-00155d054c03");
            var mailboxFolder       = new ActivityFolder(UserConnection);

            if (mailboxFolder.FetchFromDB(
                    new Dictionary <string, object> {
                { "FolderType", mailboxFolderTypeId },
                { "Name", imapServerCredentials.UserName }
            }))
            {
                var activityFolderSchema = UserConnection.EntitySchemaManager.GetInstanceByName("ActivityFolder");
                var bpmonlineFolder      = activityFolderSchema.CreateEntity(UserConnection);
                var parentColumn         = activityFolderSchema.Columns.GetByName("Parent");
                var folderTypeColumn     = activityFolderSchema.Columns.GetByName("FolderType");
                bpmonlineFolder.SetDefColumnValues();
                bpmonlineFolder.SetColumnValue("Name", "BPMonline");
                bpmonlineFolder.SetColumnValue(parentColumn.ColumnValueName, mailboxFolder.PrimaryColumnValue);
                bpmonlineFolder.SetColumnValue(folderTypeColumn.ColumnValueName, emailFolderTypeId);
                bpmonlineFolder.Save();
                return(true);
            }
            return(false);
        }
        /// <inheritdoc cref="IMailboxValidator.ValidateSynchronization"/>
        public CredentialsValidationInfo ValidateSynchronization(Mailbox mailbox)
        {
            var answer = new CredentialsValidationInfo {
                IsValid = true
            };

            try {
                var imapClient = new ImapClient(new MailCredentials {
                    UserName           = mailbox.Login,
                    UserPassword       = mailbox.Password,
                    SenderEmailAddress = mailbox.SenderEmailAddress,
                    Host   = mailbox.GetServerAddress(),
                    Port   = mailbox.GetServerPort(),
                    UseSsl = mailbox.UseSsl
                }, MailSynchronizer.GetImapErrorMessages(UserConnection), UserConnection, true);
            }
            catch (ImapException exception) {
                answer.IsValid = false;
                answer.Message = ConnectToServerCaption + exception.Message;
            }
            return(answer);
        }
Ejemplo n.º 9
0
        private ImapClient GetImapClientByMailboxId(string mailboxId, out string mailboxName)
        {
            mailboxName = string.Empty;
            var mailbox = new Terrasoft.Configuration.MailboxSyncSettings(UserConnection);

            if (!mailbox.FetchFromDB(new Guid(mailboxId)))
            {
                return(null);
            }
            mailboxName = mailbox.GetTypedColumnValue <string>("UserName");
            var mailServerUId     = mailbox.GetTypedColumnValue <Guid>("MailServerId");
            var currentMailServer = new Terrasoft.Configuration.MailServer(UserConnection);

            if (!currentMailServer.FetchFromDB(mailServerUId) || !currentMailServer.GetTypedColumnValue <bool>("AllowEmailDownloading"))
            {
                return(null);
            }
            var imapServerCredentials = new MailCredentials {
                Host               = currentMailServer.GetTypedColumnValue <string>("Address"),
                Port               = currentMailServer.GetTypedColumnValue <int>("Port"),
                UseSsl             = currentMailServer.GetTypedColumnValue <bool>("UseSSL"),
                UserName           = mailbox.GetTypedColumnValue <string>("UserName"),
                UserPassword       = mailbox.GetTypedColumnValue <string>("UserPassword"),
                StartTls           = currentMailServer.GetTypedColumnValue <bool>("IsStartTls"),
                SenderEmailAddress = mailbox.GetTypedColumnValue <string>("SenderEmailAddress"),
            };
            SynchronizationErrorHelper helper = SynchronizationErrorHelper.GetInstance(UserConnection);
            ImapClient imapClient;

            try {
                imapClient = new ImapClient(imapServerCredentials, MailSynchronizer.GetImapErrorMessages(UserConnection), UserConnection, true);
            } catch (Exception ex) {
                helper.ProcessSynchronizationError(imapServerCredentials.SenderEmailAddress, ex, true);
                throw;
            }
            return(imapClient);
        }
Ejemplo n.º 10
0
        public MainViewModel(MailSynchronizer mailSynchronizer)
        {
            this.State = ClosedState;

            this.WhenChanged(model => model.IsSync, (model, b) => b)
            .Subscribe(b => { this.State = b ? OpenedState : ClosedState; });

            this.MailSynchronizer = mailSynchronizer;

            this.MailSynchronizer.WhenChanged(synchronizer => synchronizer.IsStarted, (synchronizer, b) => b)
            .Subscribe(b => this.IsSync = b);


            this.StartSyncMailCommand = ReactiveCommand.Create(
                () =>
            {
                this.IsSync = true;
                this.MailSynchronizer.Start();
                // Console.WriteLine("Zamanlayıcı başladı");
            }, this.WhenAnyValue(model => model.IsSync, b => !b));

            this.StopSyncMailCommand = ReactiveCommand.Create(
                () =>
            {
                this.IsSync = false;
                this.MailSynchronizer.Stop();
                // Console.WriteLine("Zamanlayıcı durdu");
            }, this.WhenAnyValue(model => model.IsSync, b => !!b));


            this.ShowSetting = ReactiveCommand.Create(() => HostScreen.Router
                                                      .Navigate.Execute(Locator.Current.GetService <SettingViewModel>()));
            this.ShowLogin = ReactiveCommand.Create(() => HostScreen.Router
                                                    .Navigate.Execute(Locator.Current.GetService <LoginViewModel>()));
            //this.TestLogin = ReactiveCommand.CreateFromTask(
            //    async (cancellationToken) =>
            //        {
            //            var token = ConfigurationManager.AppSettings[SettingConstants.Token];
            //            if (!string.IsNullOrEmpty(token))
            //            {
            //                using (var client = new HttpClient
            //                                        {
            //                                            BaseAddress = new Uri(
            //                                                ConfigurationManager.AppSettings[SettingConstants
            //                                                    .BaseAddress])
            //                                        })
            //                {
            //                    var response = await client.GetAsync("/api/users/current/permissions");
            //                    if (!response.IsSuccessStatusCode)
            //                    {
            //                        await this.ShowLogin.Execute();
            //                    }
            //                    else
            //                    {
            //                        // this.Confirm("Deneme", "dfgfdgdf");
            //                        //TODO Burada kullanıcıya geri dönüş verilecek
            //                    }
            //                }
            //            }
            //            else
            //            {
            //                var res = await this.ShowLogin.Execute();
            //            }
            //        });
        }
Ejemplo n.º 11
0
        public IsValidAnswer IsServerValid(Guid id, string userName, string userPassword, bool enableSync,
                                           bool sendEmail, string senderEmailAddress, bool isAnonymousAuthentication)
        {
            var currentMailServer = new MailServer(UserConnection);
            var isValidAnswer     = new IsValidAnswer()
            {
                IsValid = true,
                Message = string.Empty
            };

            if (!currentMailServer.FetchFromDB(id))
            {
                isValidAnswer.IsValid = false;
                return(isValidAnswer);
            }
            if (currentMailServer.AllowEmailDownloading && enableSync)
            {
                try {
                    var imapServerCredentials = new MailCredentials {
                        Host         = currentMailServer.Address,
                        Port         = currentMailServer.Port,
                        UseSsl       = currentMailServer.UseSSL,
                        StartTls     = currentMailServer.IsStartTls,
                        UserName     = userName,
                        UserPassword = userPassword
                    };
                    var mapClient = new ImapClient(imapServerCredentials, MailSynchronizer.GetImapErrorMessages(UserConnection), UserConnection, true);
                } catch (ImapException exception) {
                    isValidAnswer.IsValid = false;
                    isValidAnswer.Message = ConnectToImapServerCaption.ToString() + exception.Message;
                }
            }
            if (currentMailServer.AllowEmailSending && sendEmail)
            {
                var smtpServerCredentials = new MailCredentials();
                if (isAnonymousAuthentication)
                {
                    smtpServerCredentials.IsAnonymousAuthentication = true;
                }
                else
                {
                    smtpServerCredentials.UserName     = userName;
                    smtpServerCredentials.UserPassword = userPassword;
                }
                smtpServerCredentials.Host               = currentMailServer.SMTPServerAddress;
                smtpServerCredentials.Port               = currentMailServer.SMTPPort;
                smtpServerCredentials.UseSsl             = currentMailServer.UseSSLforSending;
                smtpServerCredentials.StartTls           = currentMailServer.IsStartTls;
                smtpServerCredentials.Timeout            = currentMailServer.SMTPServerTimeout * 1000;
                smtpServerCredentials.SenderEmailAddress = senderEmailAddress;

                var smtpClient = new SmtpClient(UserConnection, smtpServerCredentials);
                try {
                    smtpClient.SendTestMessage();
                } catch (SmtpException ex) {
                    if (!string.IsNullOrEmpty(isValidAnswer.Message))
                    {
                        isValidAnswer.Message += Environment.NewLine;
                    }
                    isValidAnswer.IsValid  = false;
                    isValidAnswer.Message += CanNotSendTestMessageCaption.ToString() + ex.Message;
                }
            }
            return(isValidAnswer);
        }
Ejemplo n.º 12
0
 public bool DoesSyncJobExist()
 {
     return(MailSynchronizer.DoesSyncJobExist(UserConnection));
 }