Beispiel #1
0
        /// <summary>
        /// Gets the folder for the specified namespace.
        /// </summary>
        /// <returns>The folder.</returns>
        /// <param name="namespace">The namespace.</param>
        /// <exception cref="System.ArgumentNullException">
        /// <paramref name="namespace"/> is <c>null</c>.
        /// </exception>
        /// <exception cref="System.ObjectDisposedException">
        /// The <see cref="ImapClient"/> has been disposed.
        /// </exception>
        /// <exception cref="System.InvalidOperationException">
        /// <para>The <see cref="ImapClient"/> is not connected.</para>
        /// <para>-or-</para>
        /// <para>The <see cref="ImapClient"/> is not authenticated.</para>
        /// </exception>
        /// <exception cref="FolderNotFoundException">
        /// The folder could not be found.
        /// </exception>
        public IFolder GetFolder(FolderNamespace @namespace)
        {
            if (@namespace == null)
            {
                throw new ArgumentNullException("namespace");
            }

            CheckDisposed();

            if (!IsConnected)
            {
                throw new InvalidOperationException("The ImapClient is not connected.");
            }

            if (engine.State != ImapEngineState.Authenticated && engine.State != ImapEngineState.Selected)
            {
                throw new InvalidOperationException("The ImapClient is not authenticated.");
            }

            var        encodedName = ImapEncoding.Encode(@namespace.Path);
            ImapFolder folder;

            if (engine.FolderCache.TryGetValue(encodedName, out folder))
            {
                return(folder);
            }

            throw new FolderNotFoundException(@namespace.Path);
        }
        private void ToSent(string login, string password, MailMessage message)
        {
            ImapClient imap = new ImapClient();

            imap.Connect("imap.yandex.ru", 993, true);                                      // imap server address
            imap.Authenticate(login, password);
            FolderNamespace folderNamespaceSent = new FolderNamespace('/', "Отправленные"); // getter sent folder
            IMailFolder     folderSent          = imap.GetFolder(folderNamespaceSent);

            folderSent.Open(FolderAccess.ReadOnly);
            MimeMessage mimeMessage = (MimeMessage)message;

            folderSent.Append(mimeMessage);                    // add to sent folder
        }
Beispiel #3
0
        /// <summary>
        /// Get the list of mailbox names in a specific namespace
        /// </summary>
        /// /// <param name="folderNamespace">The folderNamespace</param>
        /// <returns>A list of mailbox names in a specific namespace</returns>
        public virtual List <string> GetMailBoxNamesInNamespace(FolderNamespace folderNamespace)
        {
            return(GenericWait.WaitFor <List <string> >(() =>
            {
                List <string> mailBoxes = new List <string>();

                // Get all mailboxes in folderNamespace
                foreach (IMailFolder mailbox in EmailConnection.GetFolders(folderNamespace))
                {
                    mailBoxes.Add(mailbox.FullName);
                }

                return mailBoxes;
            }));
        }
        public void TestFolderNamespaceCollection()
        {
            var             namespaces = new FolderNamespaceCollection();
            FolderNamespace ns;
            int             i = 0;

            Assert.Throws <ArgumentNullException> (() => namespaces.Add(null));
            Assert.Throws <ArgumentNullException> (() => namespaces.Contains(null));
            Assert.Throws <ArgumentNullException> (() => namespaces.Remove(null));
            Assert.Throws <ArgumentOutOfRangeException> (() => ns             = namespaces[-1]);
            Assert.Throws <ArgumentOutOfRangeException> (() => namespaces[-1] = new FolderNamespace('.', ""));

            Assert.AreEqual(0, namespaces.Count);

            ns = new FolderNamespace('.', "");
            namespaces.Add(ns);
            Assert.AreEqual(1, namespaces.Count);
            Assert.IsTrue(namespaces.Contains(ns));
            Assert.Throws <ArgumentNullException> (() => namespaces[0] = null);

            ns            = new FolderNamespace('\\', "");
            namespaces[0] = ns;
            Assert.AreEqual(1, namespaces.Count);
            Assert.IsTrue(namespaces.Contains(ns));

            Assert.IsTrue(namespaces.Remove(ns));
            Assert.AreEqual(0, namespaces.Count);
            Assert.IsFalse(namespaces.Contains(ns));

            namespaces.Add(new FolderNamespace('.', ""));
            namespaces.Add(new FolderNamespace('\\', ""));
            foreach (var item in namespaces)
            {
                Assert.AreEqual(namespaces[i++], item);
            }
            i = 0;
            foreach (object item in (IEnumerable)namespaces)
            {
                Assert.AreEqual(namespaces[i++], item);
            }

            Assert.AreEqual("((\".\" \"\")(\"\\\\\" \"\"))", namespaces.ToString());
        }
Beispiel #5
0
        private void CopyNamespace(FolderNamespace ns)
        {
            //Console.WriteLine(ns);

            //Console.WriteLine(sourceClient.Inbox.FullName);

            var folders = sourceClient.GetFolders(ns, false);

            Copy(sourceClient.Inbox, false);

            var rootFolder = destinationClient.GetFolder(ns);

            var destFolders = rootFolder.GetSubfolders(false).ToList();

            var allFolders = destinationClient.GetFolders(ns, false);

            foreach (var folder in folders)
            {
                //Console.WriteLine(folder.FullName);
                Copy(folder, true, destFolders);
            }
        }
 /// <summary>
 /// Asynchronously get all of the folders within the specified namespace.
 /// </summary>
 /// <remarks>
 /// Gets all of the folders within the specified namespace.
 /// </remarks>
 /// <returns>The folders.</returns>
 /// <param name="namespace">The namespace.</param>
 /// <param name="items">The status items to pre-populate.</param>
 /// <param name="subscribedOnly">If set to <c>true</c>, only subscribed folders will be listed.</param>
 /// <param name="cancellationToken">The cancellation token.</param>
 /// <exception cref="System.ArgumentNullException">
 /// <paramref name="namespace"/> is <c>null</c>.
 /// </exception>
 /// <exception cref="System.ObjectDisposedException">
 /// The <see cref="ImapClient"/> has been disposed.
 /// </exception>
 /// <exception cref="ServiceNotConnectedException">
 /// The <see cref="ImapClient"/> is not connected.
 /// </exception>
 /// <exception cref="ServiceNotAuthenticatedException">
 /// The <see cref="ImapClient"/> is not authenticated.
 /// </exception>
 /// <exception cref="System.OperationCanceledException">
 /// The operation was canceled via the cancellation token.
 /// </exception>
 /// <exception cref="FolderNotFoundException">
 /// The namespace folder could not be found.
 /// </exception>
 /// <exception cref="System.IO.IOException">
 /// An I/O error occurred.
 /// </exception>
 /// <exception cref="ImapCommandException">
 /// The server replied to the LIST or LSUB command with a NO or BAD response.
 /// </exception>
 /// <exception cref="ImapProtocolException">
 /// The server responded with an unexpected token.
 /// </exception>
 public override Task <IList <IMailFolder> > GetFoldersAsync(FolderNamespace @namespace, StatusItems items = StatusItems.None, bool subscribedOnly = false, CancellationToken cancellationToken = default(CancellationToken))
 {
     return(GetFoldersAsync(@namespace, items, subscribedOnly, true, cancellationToken));
 }
Beispiel #7
0
        public void TestArgumentExceptions()
        {
            var enumeratedRights = new [] { AccessRight.OpenFolder, AccessRight.CreateFolder };

            Assert.Throws <ArgumentNullException> (() => new AccessControl(null));
            Assert.Throws <ArgumentNullException> (() => new AccessControl(null, "rk"));
            Assert.Throws <ArgumentNullException> (() => new AccessControl(null, enumeratedRights));
            Assert.Throws <ArgumentNullException> (() => new AccessControl("name", (string)null));
            Assert.Throws <ArgumentNullException> (() => new AccessControl("name", (IEnumerable <AccessRight>)null));

            Assert.Throws <ArgumentNullException> (() => new AccessControlList(null));

            Assert.Throws <ArgumentNullException> (() => new AccessRights((IEnumerable <AccessRight>)null));
            Assert.Throws <ArgumentNullException> (() => new AccessRights((string)null));

            var rights = new AccessRights();

            Assert.Throws <ArgumentNullException> (() => rights.AddRange((string)null));
            Assert.Throws <ArgumentNullException> (() => rights.AddRange((IEnumerable <AccessRight>)null));

            Assert.Throws <ArgumentNullException> (() => new AlertEventArgs(null));

            Assert.Throws <ArgumentNullException> (() => new FolderNamespace('.', null));

            var             namespaces = new FolderNamespaceCollection();
            FolderNamespace ns;

            Assert.Throws <ArgumentNullException> (() => namespaces.Add(null));
            Assert.Throws <ArgumentNullException> (() => namespaces.Contains(null));
            Assert.Throws <ArgumentNullException> (() => namespaces.Remove(null));
            Assert.Throws <ArgumentOutOfRangeException> (() => ns             = namespaces[-1]);
            Assert.Throws <ArgumentOutOfRangeException> (() => namespaces[-1] = new FolderNamespace('.', ""));

            namespaces.Add(new FolderNamespace('.', ""));
            Assert.Throws <ArgumentNullException> (() => namespaces[0] = null);

            Assert.Throws <ArgumentNullException> (() => new FolderNotFoundException(null));
            Assert.Throws <ArgumentNullException> (() => new FolderNotFoundException("message", null));
            Assert.Throws <ArgumentNullException> (() => new FolderNotFoundException("message", null, new Exception("message")));

            Assert.Throws <ArgumentNullException> (() => new FolderNotOpenException(null, FolderAccess.ReadOnly));
            Assert.Throws <ArgumentNullException> (() => new FolderNotOpenException(null, FolderAccess.ReadOnly, "message"));
            Assert.Throws <ArgumentNullException> (() => new FolderNotOpenException(null, FolderAccess.ReadOnly, "message", new Exception("message")));

            Assert.Throws <ArgumentNullException> (() => new FolderRenamedEventArgs(null, "name"));
            Assert.Throws <ArgumentNullException> (() => new FolderRenamedEventArgs("name", null));

            Assert.Throws <ArgumentOutOfRangeException> (() => new MessageEventArgs(-1));

            Assert.Throws <ArgumentNullException> (() => new MessageFlagsChangedEventArgs(0, MessageFlags.Answered, null));
            Assert.Throws <ArgumentNullException> (() => new MessageFlagsChangedEventArgs(0, MessageFlags.Answered, null, 1));
            Assert.Throws <ArgumentNullException> (() => new MessageFlagsChangedEventArgs(0, UniqueId.MinValue, MessageFlags.Answered, null));
            Assert.Throws <ArgumentNullException> (() => new MessageFlagsChangedEventArgs(0, UniqueId.MinValue, MessageFlags.Answered, null, 1));

            Assert.Throws <ArgumentNullException> (() => new MessageLabelsChangedEventArgs(0, null));
            Assert.Throws <ArgumentNullException> (() => new MessageLabelsChangedEventArgs(0, null, 1));
            Assert.Throws <ArgumentNullException> (() => new MessageLabelsChangedEventArgs(0, UniqueId.MinValue, null));
            Assert.Throws <ArgumentNullException> (() => new MessageLabelsChangedEventArgs(0, UniqueId.MinValue, null, 1));

            Assert.Throws <ArgumentNullException> (() => new MessageSentEventArgs(null, "response"));
            Assert.Throws <ArgumentNullException> (() => new MessageSentEventArgs(new MimeMessage(), null));

            Assert.Throws <ArgumentNullException> (() => new MessageSummaryFetchedEventArgs(null));

            Assert.Throws <ArgumentNullException> (() => new MessagesVanishedEventArgs(null, false));

            Assert.Throws <ArgumentNullException> (() => new MetadataCollection(null));

            var metadataOptions = new MetadataOptions();

            Assert.Throws <ArgumentOutOfRangeException> (() => metadataOptions.Depth = 500);

            Assert.Throws <ArgumentOutOfRangeException> (() => new ModSeqChangedEventArgs(-1));
            Assert.Throws <ArgumentOutOfRangeException> (() => new ModSeqChangedEventArgs(-1, 1));
            Assert.Throws <ArgumentOutOfRangeException> (() => new ModSeqChangedEventArgs(-1, UniqueId.MinValue, 1));

            Assert.Throws <ArgumentOutOfRangeException> (() => new OrderBy(OrderByType.To, SortOrder.None));

            Assert.Throws <ArgumentNullException> (() => new ProtocolLogger((string)null));
            Assert.Throws <ArgumentNullException> (() => new ProtocolLogger((Stream)null));
            using (var logger = new ProtocolLogger(new MemoryStream())) {
                var buffer = new byte[1024];

                Assert.Throws <ArgumentNullException> (() => logger.LogConnect(null));
                Assert.Throws <ArgumentNullException> (() => logger.LogClient(null, 0, 0));
                Assert.Throws <ArgumentNullException> (() => logger.LogServer(null, 0, 0));
                Assert.Throws <ArgumentOutOfRangeException> (() => logger.LogClient(buffer, -1, 0));
                Assert.Throws <ArgumentOutOfRangeException> (() => logger.LogServer(buffer, -1, 0));
                Assert.Throws <ArgumentOutOfRangeException> (() => logger.LogClient(buffer, 0, -1));
                Assert.Throws <ArgumentOutOfRangeException> (() => logger.LogServer(buffer, 0, -1));
            }

            Assert.Throws <ArgumentNullException> (() => new UniqueIdMap(null, new [] { UniqueId.MinValue }));
            Assert.Throws <ArgumentNullException> (() => new UniqueIdMap(new [] { UniqueId.MinValue }, null));
        }
Beispiel #8
0
		/// <summary>
		/// Get all of the folders within the specified namespace.
		/// </summary>
		/// <remarks>
		/// Gets all of the folders within the specified namespace.
		/// </remarks>
		/// <returns>The folders.</returns>
		/// <param name="namespace">The namespace.</param>
		/// <param name="subscribedOnly">If set to <c>true</c>, only subscribed folders will be listed.</param>
		/// <param name="cancellationToken">The cancellation token.</param>
		/// <exception cref="System.ArgumentNullException">
		/// <paramref name="namespace"/> is <c>null</c>.
		/// </exception>
		/// <exception cref="System.ObjectDisposedException">
		/// The <see cref="ImapClient"/> has been disposed.
		/// </exception>
		/// <exception cref="ServiceNotConnectedException">
		/// The <see cref="ImapClient"/> is not connected.
		/// </exception>
		/// <exception cref="ServiceNotAuthenticatedException">
		/// The <see cref="ImapClient"/> is not authenticated.
		/// </exception>
		/// <exception cref="System.OperationCanceledException">
		/// The operation was canceled via the cancellation token.
		/// </exception>
		/// <exception cref="FolderNotFoundException">
		/// The namespace folder could not be found.
		/// </exception>
		/// <exception cref="System.IO.IOException">
		/// An I/O error occurred.
		/// </exception>
		/// <exception cref="ImapCommandException">
		/// The server replied to the IDLE command with a NO or BAD response.
		/// </exception>
		/// <exception cref="ImapProtocolException">
		/// The server responded with an unexpected token.
		/// </exception>
		public override IEnumerable<IMailFolder> GetFolders (FolderNamespace @namespace, bool subscribedOnly = false, CancellationToken cancellationToken = default (CancellationToken))
		{
			if (@namespace == null)
				throw new ArgumentNullException ("namespace");

			CheckDisposed ();
			CheckConnected ();
			CheckAuthenticated ();

			foreach (var folder in engine.GetFolders (@namespace, subscribedOnly, cancellationToken))
				yield return folder;

			yield break;
		}
Beispiel #9
0
		/// <summary>
		/// Get the folder for the specified namespace.
		/// </summary>
		/// <remarks>
		/// Gets the folder for the specified namespace.
		/// </remarks>
		/// <returns>The folder.</returns>
		/// <param name="namespace">The namespace.</param>
		/// <exception cref="System.ArgumentNullException">
		/// <paramref name="namespace"/> is <c>null</c>.
		/// </exception>
		/// <exception cref="System.ObjectDisposedException">
		/// The <see cref="ImapClient"/> has been disposed.
		/// </exception>
		/// <exception cref="ServiceNotConnectedException">
		/// The <see cref="ImapClient"/> is not connected.
		/// </exception>
		/// <exception cref="ServiceNotAuthenticatedException">
		/// The <see cref="ImapClient"/> is not authenticated.
		/// </exception>
		/// <exception cref="FolderNotFoundException">
		/// The folder could not be found.
		/// </exception>
		public override IMailFolder GetFolder (FolderNamespace @namespace)
		{
			if (@namespace == null)
				throw new ArgumentNullException ("namespace");

			CheckDisposed ();
			CheckConnected ();
			CheckAuthenticated ();

			var encodedName = engine.EncodeMailboxName (@namespace.Path);
			ImapFolder folder;

			if (engine.GetCachedFolder (encodedName, out folder))
				return folder;

			throw new FolderNotFoundException (@namespace.Path);
		}
Beispiel #10
0
        /// <summary>
        /// Gets the folder for the specified namespace.
        /// </summary>
        /// <returns>The folder.</returns>
        /// <param name="namespace">The namespace.</param>
        /// <exception cref="System.ArgumentNullException">
        /// <paramref name="namespace"/> is <c>null</c>.
        /// </exception>
        /// <exception cref="System.ObjectDisposedException">
        /// The <see cref="ImapClient"/> has been disposed.
        /// </exception>
        /// <exception cref="System.InvalidOperationException">
        /// <para>The <see cref="ImapClient"/> is not connected.</para>
        /// <para>-or-</para>
        /// <para>The <see cref="ImapClient"/> is not authenticated.</para>
        /// </exception>
        /// <exception cref="FolderNotFoundException">
        /// The folder could not be found.
        /// </exception>
        public IFolder GetFolder(FolderNamespace @namespace)
        {
            if (@namespace == null)
                throw new ArgumentNullException ("namespace");

            CheckDisposed ();

            if (!IsConnected)
                throw new InvalidOperationException ("The ImapClient is not connected.");

            if (engine.State != ImapEngineState.Authenticated && engine.State != ImapEngineState.Selected)
                throw new InvalidOperationException ("The ImapClient is not authenticated.");

            var encodedName = ImapEncoding.Encode (@namespace.Path);
            ImapFolder folder;

            if (engine.FolderCache.TryGetValue (encodedName, out folder))
                return folder;

            throw new FolderNotFoundException (@namespace.Path);
        }
Beispiel #11
0
 public IList <IMailFolder> GetFolders(FolderNamespace rootFolderNamespace)
 {
     throw new NotImplementedException();
 }
Beispiel #12
0
        /// <summary>
        /// Gets the folder for the specified namespace.
        /// </summary>
        /// <returns>The folder if available; otherwise <c>null</c>.</returns>
        /// <param name="namespace">The namespace.</param>
        /// <exception cref="System.ArgumentNullException">
        /// <paramref name="namespace"/> is <c>null</c>.
        /// </exception>
        public IFolder GetFolder(FolderNamespace @namespace)
        {
            if (@namespace == null)
                throw new ArgumentNullException ("namespace");

            var encodedName = ImapEncoding.Encode (@namespace.Path);
            ImapFolder folder;

            if (engine.FolderCache.TryGetValue (encodedName, out folder))
                return folder;

            return null;
        }
Beispiel #13
0
		/// <summary>
		/// Get all of the folders within the specified namespace.
		/// </summary>
		/// <remarks>
		/// Gets all of the folders within the specified namespace.
		/// </remarks>
		/// <returns>The folders.</returns>
		/// <param name="namespace">The namespace.</param>
		/// <param name="items">The status items to pre-populate.</param>
		/// <param name="subscribedOnly">If set to <c>true</c>, only subscribed folders will be listed.</param>
		/// <param name="cancellationToken">The cancellation token.</param>
		/// <exception cref="System.ArgumentNullException">
		/// <paramref name="namespace"/> is <c>null</c>.
		/// </exception>
		/// <exception cref="System.ObjectDisposedException">
		/// The <see cref="ImapClient"/> has been disposed.
		/// </exception>
		/// <exception cref="ServiceNotConnectedException">
		/// The <see cref="ImapClient"/> is not connected.
		/// </exception>
		/// <exception cref="ServiceNotAuthenticatedException">
		/// The <see cref="ImapClient"/> is not authenticated.
		/// </exception>
		/// <exception cref="System.OperationCanceledException">
		/// The operation was canceled via the cancellation token.
		/// </exception>
		/// <exception cref="FolderNotFoundException">
		/// The namespace folder could not be found.
		/// </exception>
		/// <exception cref="System.IO.IOException">
		/// An I/O error occurred.
		/// </exception>
		/// <exception cref="ImapCommandException">
		/// The server replied to the LIST or LSUB command with a NO or BAD response.
		/// </exception>
		/// <exception cref="ImapProtocolException">
		/// The server responded with an unexpected token.
		/// </exception>
		public override IList<IMailFolder> GetFolders (FolderNamespace @namespace, StatusItems items = StatusItems.None, bool subscribedOnly = false, CancellationToken cancellationToken = default (CancellationToken))
		{
			if (@namespace == null)
				throw new ArgumentNullException (nameof (@namespace));

			CheckDisposed ();
			CheckConnected ();
			CheckAuthenticated ();

			var folders = engine.GetFolders (@namespace, items, subscribedOnly, cancellationToken);
			var list = new IMailFolder[folders.Count];

			for (int i = 0; i < list.Length; i++)
				list[i] = (IMailFolder) folders[i];

			return list;
		}
Beispiel #14
0
        public void ExecutePostConnectionTask()
        {
            if (File.Exists(lastMailTimestampFilename))
            {
                DateTime lastTime      = new DateTime(Convert.ToInt64(File.ReadAllText(lastMailTimestampFilename)));
                TimeSpan sinceLastTime = DateTime.Now - lastTime;
                if (sinceLastTime.TotalMinutes < 11)
                {
                    return;
                }
            }


            AzusaContext context = AzusaContext.GetInstance();

            if (!context.Ini.ContainsKey(iniCategoryName))
            {
                return;
            }

            if (context.Ini[iniCategoryName].ContainsKey("enabled"))
            {
                bool isEnabled = Convert.ToInt32(context.Ini[iniCategoryName]["enabled"]) > 0;
                if (!isEnabled)
                {
                    return;
                }
            }

            bool   allcerts = Convert.ToInt32(context.Ini[iniCategoryName]["acceptAllCerts"]) > 0;
            string server   = context.Ini[iniCategoryName]["server"];
            int    port     = context.Ini[iniCategoryName].ContainsKey(portName) ? Convert.ToUInt16(context.Ini[iniCategoryName][portName]) : 143;
            bool   useSsl   = Convert.ToInt32(context.Ini[iniCategoryName]["ssl"]) > 0;
            string username = context.Ini[iniCategoryName]["username"];
            string password = context.Ini[iniCategoryName].ContainsKey(passwordName) ? context.Ini[iniCategoryName][passwordName] : "";

            if (string.IsNullOrEmpty(password))
            {
                password = PasswordManagement.Boundary.PasswordManagement.TryGetPassword(server, username);
            }

            if (string.IsNullOrEmpty(password))
            {
                context.Splash.Invoke((MethodInvoker) delegate { password = TextInputForm.PromptPassword(String.Format("Passwort für {0} auf {1}?", username, server), context.Splash); });
                if (string.IsNullOrEmpty(password))
                {
                    return;
                }
            }


            ImapClient client = new ImapClient(new MailProtocolLogger());

            if (allcerts)
            {
                client.ServerCertificateValidationCallback = (sender, certificate, chain, errors) => true;
            }
            client.SslProtocols = System.Security.Authentication.SslProtocols.Tls12;
            client.Connect(server, port, useSsl);
            client.AuthenticationMechanisms.Remove("XOAUTH2");
            client.Authenticate(username, password);
            FolderNamespace     rootFolderNamespace = client.PersonalNamespaces[0];
            IList <IMailFolder> folders             = client.GetFolders(rootFolderNamespace);

            foreach (IMailFolder folder in folders)
            {
                Folder child = new Folder();
                child.id       = MakeId(folder);
                child.name     = folder.Name;
                child.parentId = MakeId(folder.ParentFolder);
                bool created = FolderService.CreateIfNotExists(child);
                CopyFolder(folder, child, !created, password);
            }
            client.Disconnect(true);

            File.WriteAllText(lastMailTimestampFilename, DateTime.Now.Ticks.ToString());
        }