Beispiel #1
0
        public static void Run()
        {
            //ExStart:1
            // The path to the File directory.
            string dataDir = RunExamples.GetDataDir_IMAP();
            // Create an instance of the ImapClient class
            ImapClient imapClient = new ImapClient();

            // Specify host, username and password, and set port for your client
            imapClient.Host            = "imap.gmail.com";
            imapClient.Username        = "******";
            imapClient.Password        = "******";
            imapClient.Port            = 993;
            imapClient.SecurityOptions = SecurityOptions.Auto;

            imapClient.UseMultiConnection = MultiConnectionMode.Enable;

            ImapMailboxInfo mailboxInfo = imapClient.MailboxInfo;

            ImapFolderInfo           info  = imapClient.GetFolderInfo(mailboxInfo.Inbox.Name);
            ImapFolderInfoCollection infos = new ImapFolderInfoCollection();

            infos.Add(info);

            imapClient.Backup(infos, dataDir + @"\ImapBackup.pst", BackupOptions.Recursive);
            //ExEnd:1
        }
        /// Recursive method to get messages from folders and sub-folders
        private static void ListMessagesInFolder(ImapFolderInfo folderInfo, string rootFolder, ImapClient client)
        {
            // Create the folder in disk (same name as on IMAP server)
            string currentFolder = RunExamples.GetDataDir_IMAP();

            Directory.CreateDirectory(currentFolder);

            // Read the messages from the current folder, if it is selectable
            if (folderInfo.Selectable)
            {
                // Send status command to get folder info
                ImapFolderInfo folderInfoStatus = client.GetFolderInfo(folderInfo.Name);
                Console.WriteLine(folderInfoStatus.Name + " folder selected. New messages: " + folderInfoStatus.NewMessageCount + ", Total messages: " + folderInfoStatus.TotalMessageCount);

                // Select the current folder and List messages
                client.SelectFolder(folderInfo.Name);
                ImapMessageInfoCollection msgInfoColl = client.ListMessages();
                Console.WriteLine("Listing messages....");
                foreach (ImapMessageInfo msgInfo in msgInfoColl)
                {
                    // Get subject and other properties of the message
                    Console.WriteLine("Subject: " + msgInfo.Subject);
                    Console.WriteLine("Read: " + msgInfo.IsRead + ", Recent: " + msgInfo.Recent + ", Answered: " + msgInfo.Answered);

                    // Get rid of characters like ? and :, which should not be included in a file name and Save the message in MSG format
                    string      fileName = msgInfo.Subject.Replace(":", " ").Replace("?", " ");
                    MailMessage msg      = client.FetchMessage(msgInfo.SequenceNumber);
                    msg.Save(currentFolder + "\\" + fileName + "-" + msgInfo.SequenceNumber + ".msg", SaveOptions.DefaultMsgUnicode);
                }
                Console.WriteLine("============================\n");
            }
            else
            {
                Console.WriteLine(folderInfo.Name + " is not selectable.");
            }

            try
            {
                // If this folder has sub-folders, call this method recursively to get messages
                ImapFolderInfoCollection folderInfoCollection = client.ListFolders(folderInfo.Name);
                foreach (ImapFolderInfo subfolderInfo in folderInfoCollection)
                {
                    ListMessagesInFolder(subfolderInfo, rootFolder, client);
                }
            }
            catch (Exception) { }
            // ExEnd:ReadMessagesRecursively
        }
        /// Recursive method to get messages from folders and sub-folders
        private static void ListMessagesInFolder(ImapFolderInfo folderInfo, string rootFolder, ImapClient client)
        {
            // Create the folder in disk (same name as on IMAP server)
            string currentFolder = RunExamples.GetDataDir_IMAP();
            Directory.CreateDirectory(currentFolder);

            // Read the messages from the current folder, if it is selectable
            if (folderInfo.Selectable)
            {
                // Send status command to get folder info
                ImapFolderInfo folderInfoStatus = client.GetFolderInfo(folderInfo.Name);
                Console.WriteLine(folderInfoStatus.Name + " folder selected. New messages: " + folderInfoStatus.NewMessageCount + ", Total messages: " + folderInfoStatus.TotalMessageCount);

                // Select the current folder and List messages
                client.SelectFolder(folderInfo.Name);
                ImapMessageInfoCollection msgInfoColl = client.ListMessages();
                Console.WriteLine("Listing messages....");
                foreach (ImapMessageInfo msgInfo in msgInfoColl)
                {
                    // Get subject and other properties of the message
                    Console.WriteLine("Subject: " + msgInfo.Subject);
                    Console.WriteLine("Read: " + msgInfo.IsRead + ", Recent: " + msgInfo.Recent + ", Answered: " + msgInfo.Answered);

                    // Get rid of characters like ? and :, which should not be included in a file name and Save the message in MSG format
                    string fileName = msgInfo.Subject.Replace(":", " ").Replace("?", " ");
                    MailMessage msg = client.FetchMessage(msgInfo.SequenceNumber);
                    msg.Save(currentFolder + "\\" + fileName + "-" + msgInfo.SequenceNumber + ".msg", SaveOptions.DefaultMsgUnicode);
                }
                Console.WriteLine("============================\n");
            }
            else
            {
                Console.WriteLine(folderInfo.Name + " is not selectable.");
            }

            try
            {
                // If this folder has sub-folders, call this method recursively to get messages
                ImapFolderInfoCollection folderInfoCollection = client.ListFolders(folderInfo.Name);
                foreach (ImapFolderInfo subfolderInfo in folderInfoCollection)
                {
                    ListMessagesInFolder(subfolderInfo, rootFolder, client);
                }
            }
            catch (Exception) { }
            // ExEnd:ReadMessagesRecursively
        }
        public static void Run()
        {
            // Create an instance of the ImapClient class
            ImapClient client = new ImapClient();

            // Specify host, username, password, Port and SecurityOptions for your client
            client.Host            = "imap.gmail.com";
            client.Username        = "******";
            client.Password        = "******";
            client.Port            = 993;
            client.SecurityOptions = SecurityOptions.Auto;

            try
            {
                // ExStart:GettingFoldersInformation
                // Get all folders in the currently subscribed folder
                ImapFolderInfoCollection folderInfoColl = client.ListFolders();

                // Iterate through the collection to get folder info one by one
                foreach (ImapFolderInfo folderInfo in folderInfoColl)
                {
                    // Folder name and get New messages in the folder
                    Console.WriteLine("Folder name is " + folderInfo.Name);
                    ImapFolderInfo folderExtInfo = client.GetFolderInfo(folderInfo.Name);
                    Console.WriteLine("New message count: " + folderExtInfo.NewMessageCount);
                    Console.WriteLine("Is it readonly? " + folderExtInfo.ReadOnly);
                    Console.WriteLine("Total number of messages " + folderExtInfo.TotalMessageCount);
                }
                // ExEnd:GettingFoldersInformation

                // Disconnect to the remote IMAP server
                client.Dispose();
            }
            catch (Exception ex)
            {
                Console.Write(Environment.NewLine + ex);
            }
            Console.WriteLine(Environment.NewLine + "Getting folders information from IMAP server.");
        }
        public static void Run()
        {
            // ExStart:ConnectingWithIMAPServer
            // Create an instance of the ImapClient class
            ImapClient client = new ImapClient();

            // Specify host, username, password, Port and SecurityOptions for your client
            client.Host = "imap.gmail.com";
            client.Username = "******";
            client.Password = "******";
            client.Port = 993;
            client.SecurityOptions = SecurityOptions.Auto;

            try
            {
                // Get all folders in the currently subscribed folder
                ImapFolderInfoCollection folderInfoColl = client.ListFolders();

                // Iterate through the collection to get folder info one by one
                foreach (ImapFolderInfo folderInfo in folderInfoColl)
                {
                    // Folder name and get New messages in the folder
                    Console.WriteLine("Folder name is " + folderInfo.Name);
                    ImapFolderInfo folderExtInfo = client.GetFolderInfo(folderInfo.Name);
                    Console.WriteLine("New message count: " + folderExtInfo.NewMessageCount);
                    Console.WriteLine("Is it readonly? " + folderExtInfo.ReadOnly);
                    Console.WriteLine("Total number of messages " + folderExtInfo.TotalMessageCount);
                }
                // Disconnect to the remote IMAP server
                client.Dispose();
            }
            catch (Exception ex)
            {
                Console.Write(Environment.NewLine + ex);
            }
            // ExEnd:ConnectingWithIMAPServer
            Console.WriteLine(Environment.NewLine + "Getting folders information from IMAP server.");
        }