コード例 #1
0
        public MailAccountData UpdateAccount(string name,
                                             string email,
                                             string account,
                                             string password,
                                             int port,
                                             string server,
                                             string smtp_account,
                                             string smtp_password,
                                             int smtp_port,
                                             string smtp_server,
                                             bool smtp_auth,
                                             bool restrict,
                                             EncryptionType incoming_encryption_type,
                                             EncryptionType outcoming_encryption_type,
                                             SaslMechanism auth_type_in,
                                             SaslMechanism auth_type_smtp)
        {
            if (string.IsNullOrEmpty(email))
            {
                throw new ArgumentException();
            }

            var mbox = MailBoxManager.GetMailBox(TenantId, Username, new MailAddress(email));

            if (null == mbox)
            {
                throw new ArgumentException("Mailbox with specified email doesn't exist.");
            }

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

            if (string.IsNullOrEmpty(password))
            {
                password = mbox.Password;
            }
            if (string.IsNullOrEmpty(smtp_password))
            {
                smtp_password = mbox.SmtpPassword;
            }

            string errorText = null;

            mbox.Account      = account;
            mbox.Name         = name;
            mbox.Password     = password;
            mbox.SmtpAccount  = smtp_account;
            mbox.SmtpPassword = smtp_password;
            mbox.Port         = port;
            mbox.Server       = server;
            mbox.SmtpPort     = smtp_port;
            mbox.SmtpServer   = smtp_server;

            mbox.BeginDate = restrict
                ? DateTime.Now.Subtract(new TimeSpan(mbox.MailLimitedTimeDelta))
                : mbox.MailBeginTimestamp;
            mbox.Encryption         = incoming_encryption_type;
            mbox.SmtpEncryption     = outcoming_encryption_type;
            mbox.Authentication     = auth_type_in;
            mbox.SmtpAuthentication = smtp_auth ? auth_type_smtp : SaslMechanism.None;

            try
            {
                if (string.IsNullOrEmpty(mbox.OAuthToken))
                {
                    LoginResult loginResult;

                    using (var client = new MailClient(mbox, CancellationToken.None, 5000, SslCertificatesErrorPermit, log: _log))
                    {
                        loginResult = client.TestLogin();
                    }

                    if (!loginResult.IngoingSuccess)
                    {
                        errorText = GetFormattedTextError(loginResult.IngoingException,
                                                          mbox.Imap ? MailServerType.Imap : MailServerType.Pop3, false); // exImap is ImapConnectionTimeoutException
                    }

                    if (!loginResult.OutgoingSuccess)
                    {
                        if (!string.IsNullOrEmpty(errorText))
                        {
                            errorText += "\r\n";
                        }

                        errorText += GetFormattedTextError(loginResult.OutgoingException, MailServerType.Smtp, false);
                        // exSmtp is SmtpConnectionTimeoutException);
                    }

                    if (!string.IsNullOrEmpty(errorText))
                    {
                        throw new Exception(errorText);
                    }
                }

                if (!MailBoxManager.SaveMailBox(mbox))
                {
                    throw new Exception("Failed to_addresses update account");
                }

                MailBoxManager.CachedAccounts.Clear(Username);

                var accountInfo = new AccountInfo(mbox.MailBoxId, mbox.EMailView, mbox.Name, mbox.Enabled, mbox.QuotaError,
                                                  MailBox.AuthProblemType.NoProblems, new MailSignature(mbox.MailBoxId, TenantId, "", false),
                                                  new MailAutoreply(mbox.MailBoxId, TenantId, false, false, false, DateTime.MinValue,
                                                                    DateTime.MinValue, String.Empty, String.Empty), false, mbox.EMailInFolder, false, false);

                return(accountInfo.ToAddressData().FirstOrDefault());
            }
            catch (Exception ex)
            {
                errorText = GetFormattedTextError(ex);
            }

            throw new Exception(errorText);
        }
コード例 #2
0
        public MailAccountData CreateAccount(string name,
                                             string email,
                                             string account,
                                             string password,
                                             int port,
                                             string server,
                                             string smtp_account,
                                             string smtp_password,
                                             int smtp_port,
                                             string smtp_server,
                                             bool smtp_auth,
                                             bool imap,
                                             bool restrict,
                                             EncryptionType incoming_encryption_type,
                                             EncryptionType outcoming_encryption_type,
                                             SaslMechanism auth_type_in,
                                             SaslMechanism auth_type_smtp)
        {
            string errorText = null;
            var    mbox      = new MailBox
            {
                Name         = name,
                EMail        = new MailAddress(email),
                Account      = account,
                Password     = password,
                Port         = port,
                Server       = server,
                SmtpAccount  = smtp_account,
                SmtpPassword = smtp_password,
                SmtpPort     = smtp_port,
                SmtpServer   = smtp_server,
                Imap         = imap,
                TenantId     = TenantId,
                UserId       = Username,
                BeginDate    = restrict ?
                               DateTime.Now.Subtract(new TimeSpan(MailBox.DefaultMailLimitedTimeDelta)) :
                               new DateTime(MailBox.DefaultMailBeginTimestamp),
                Encryption         = incoming_encryption_type,
                SmtpEncryption     = outcoming_encryption_type,
                Authentication     = auth_type_in,
                SmtpAuthentication = smtp_auth ? auth_type_smtp : SaslMechanism.None
            };

            LoginResult loginResult;

            using (var client = new MailClient(mbox, CancellationToken.None, 5000, SslCertificatesErrorPermit, log: _log))
            {
                loginResult = client.TestLogin();
            }

            if (!loginResult.IngoingSuccess)
            {
                errorText = GetFormattedTextError(loginResult.IngoingException,
                                                  mbox.Imap ? MailServerType.Imap : MailServerType.Pop3, false); // exImap is ImapConnectionTimeoutException
            }

            if (!loginResult.OutgoingSuccess)
            {
                if (!string.IsNullOrEmpty(errorText))
                {
                    errorText += "\r\n";
                }

                errorText += GetFormattedTextError(loginResult.OutgoingException, MailServerType.Smtp, false);
                // exSmtp is SmtpConnectionTimeoutException);
            }

            if (!string.IsNullOrEmpty(errorText))
            {
                throw new Exception(errorText);
            }

            try
            {
                MailBoxManager.SaveMailBox(mbox);
                MailBoxManager.CachedAccounts.Clear(Username);

                if (IsSignalRAvailable)
                {
                    MailBoxManager.UpdateUserActivity(TenantId, Username);
                }

                var accountInfo = new AccountInfo(mbox.MailBoxId, mbox.EMailView, mbox.Name, mbox.Enabled, mbox.QuotaError,
                                                  MailBox.AuthProblemType.NoProblems, new MailSignature(mbox.MailBoxId, TenantId, "", false),
                                                  new MailAutoreply(mbox.MailBoxId, TenantId, false, false, false, DateTime.MinValue,
                                                                    DateTime.MinValue, String.Empty, String.Empty), false, mbox.EMailInFolder, false, false);

                return(accountInfo.ToAddressData().FirstOrDefault());
            }
            catch (Exception ex)
            {
                //TODO: change AttachmentsUnknownError to common unknown error text
                errorText = GetFormattedTextError(ex, MailApiResource.AttachmentsUnknownError);
            }

            throw new Exception(errorText);
        }