// ExStart: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 { // The root folder (which will be created on disk) consists of host and username string rootFolder = client.Host + "-" + client.Username; // Create the root folder and List all the folders from IMAP server Directory.CreateDirectory(rootFolder); ImapFolderInfoCollection folderInfoCollection = client.ListFolders(); foreach (ImapFolderInfo folderInfo in folderInfoCollection) { // Call the recursive method to read messages and get sub-folders ListMessagesInFolder(folderInfo, rootFolder, client); } // Disconnect to the remote IMAP server client.Dispose(); } catch (Exception ex) { Console.Write(Environment.NewLine + ex); } Console.WriteLine(Environment.NewLine + "Downloaded messages recursively from IMAP server."); }
public override void PopulateFoldersList(ref Repeater repater) { try { ImapFolderInfoCollection folderInfoColl = client.ListFolders(); List <MailFolder> foldersList = new List <MailFolder>(); List <MailFolder> foldersList2 = new List <MailFolder>(); // inbox // drafts // sent items // deleted items AddFolderToList(ref foldersList, ref folderInfoColl, "inbox"); AddFolderToList(ref foldersList, ref folderInfoColl, "drafts"); AddFolderToList(ref foldersList, ref folderInfoColl, "sent"); AddFolderToList(ref foldersList, ref folderInfoColl, "deleted"); AddFolderToList(ref foldersList, ref folderInfoColl, "trash"); foldersList2 = (from list in folderInfoColl select new MailFolder() { FolderName = list.Name, FolderUri = list.Name }).ToList(); foldersList.AddRange(foldersList2); repater.DataSource = foldersList; repater.DataBind(); } catch (System.Exception) { } }
public override void PopulateFoldersList(ref TreeView tvFolders) { try { //ImapFolderInfoCollection folderInfoColl = client.ListFolders(); //List<MailFolder> foldersList = new List<MailFolder>(); //List<MailFolder> foldersList2 = new List<MailFolder>(); //// inbox //// drafts //// sent items //// deleted items //AddFolderToList(ref foldersList, ref folderInfoColl, "inbox"); //AddFolderToList(ref foldersList, ref folderInfoColl, "drafts"); //AddFolderToList(ref foldersList, ref folderInfoColl, "sent"); //AddFolderToList(ref foldersList, ref folderInfoColl, "deleted"); //AddFolderToList(ref foldersList, ref folderInfoColl, "trash"); //foldersList2 = (from list in folderInfoColl // select new MailFolder() // { // FolderName = list.Name, // FolderUri = list.Name // }).ToList(); //foldersList.AddRange(foldersList2); //tvFolders.DataSource = foldersList; //tvFolders.DataBind(); ImapFolderInfoCollection folderInfoColl = client.ListFolders(); PopulateImapFolders(client, folderInfoColl, tvFolders.Nodes); } catch (System.Exception) { } }
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 }
/// <summary> /// Recursive method to get messages from folders and sub-folders /// </summary> /// <param name="folderInfo"></param> /// <param name="rootFolder"></param> private static void ListMessagesInFolder(ImapFolderInfo folderInfo, string rootFolder, ImapClient client) { // Create the folder in disk (same name as on IMAP server) string currentFolder = Path.Combine(Path.GetFullPath("../../../Data/"), folderInfo.Name); Directory.CreateDirectory(currentFolder); // Read the messages from the current folder, if it is selectable if (folderInfo.Selectable == true) { // Send status command to get folder info ImapFolderInfo folderInfoStatus = client.ListFolder(folderInfo.Name); Console.WriteLine(folderInfoStatus.Name + " folder selected. New messages: " + folderInfoStatus.NewMessageCount + ", Total messages: " + folderInfoStatus.TotalMessageCount); // Select the current folder client.SelectFolder(folderInfo.Name); // List messages 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 string fileName = msgInfo.Subject.Replace(":", " ").Replace("?", " "); // Save the message in MSG format MailMessage msg = client.FetchMessage(msgInfo.SequenceNumber); msg.Save(currentFolder + "\\" + fileName + "-" + msgInfo.SequenceNumber + ".msg", Aspose.Email.Mail.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) { } }
private void PopulateImapFolders(ImapClient client, ImapFolderInfoCollection folderInfoColl, TreeNodeCollection nodes) { // Add the current collection to the node foreach (ImapFolderInfo folderInfo in folderInfoColl) { TreeNode node = new TreeNode(Common.formatImapFolderName(folderInfo.Name), folderInfo.Name); nodes.Add(node); // If this folder has children, add them as well if (folderInfo.Selectable == true) { PopulateImapFolders(client, client.ListFolders(folderInfo.Name), node.ChildNodes); } } }
public static void Run() { // The path to the documents directory. string dataDir = RunExamples.GetDataDir_IMAP(); string dstEmail = dataDir + "1234.eml"; //Create an instance of the ImapClient class ImapClient client = new ImapClient(); //Specify host, username and password for your client client.Host = "imap.gmail.com"; // Set username client.Username = "******"; // Set password client.Password = "******"; // Set the port to 993. This is the SSL port of IMAP server client.Port = 993; // Enable SSL client.SecurityOptions = SecurityOptions.Auto; try { // The root folder (which will be created on disk) consists of host and username string rootFolder = client.Host + "-" + client.Username; // Create the root folder Directory.CreateDirectory(rootFolder); // List all the folders from IMAP server ImapFolderInfoCollection folderInfoCollection = client.ListFolders(); foreach (ImapFolderInfo folderInfo in folderInfoCollection) { // Call the recursive method to read messages and get sub-folders ListMessagesInFolder(folderInfo, rootFolder, client); } //Disconnect to the remote IMAP server client.Disconnect(); } catch (Exception ex) { System.Console.Write(Environment.NewLine + ex.ToString()); } Console.WriteLine(Environment.NewLine + "Downloaded messages recursively from IMAP server."); }
private void AddFolderToList(ref List <MailFolder> foldersList, ref ImapFolderInfoCollection folderInfoColl, string folderName) { try { var folList = (from obj in folderInfoColl where obj.Name.ToLower().Contains(folderName) select obj); if (folList != null) { var folder = folList.FirstOrDefault(); if (folder != null) { foldersList.Add(new MailFolder(folder.Name, folder.Name)); folderInfoColl.Remove(folder); } } } catch (Exception) { } }
public static void Run() { //ExStart:IMAP4ExtendedListCommand using (ImapClient client = new ImapClient("imap.gmail.com", 993, "username", "password")) { ImapFolderInfoCollection folderInfoCol = client.ListFolders("*"); Console.WriteLine("Extended List Supported: " + client.ExtendedListSupported); foreach (ImapFolderInfo folderInfo in folderInfoCol) { switch (folderInfo.Name) { case "[Gmail]/All Mail": Console.WriteLine("Has Children: " + folderInfo.HasChildren); break; case "[Gmail]/Bin": Console.WriteLine("Bin has children? " + folderInfo.HasChildren); break; case "[Gmail]/Drafts": Console.WriteLine("Drafts has children? " + folderInfo.HasChildren); break; case "[Gmail]/Important": Console.WriteLine("Important has Children? " + folderInfo.HasChildren); break; case "[Gmail]/Sent Mail": Console.WriteLine("Sent Mail has Children? " + folderInfo.HasChildren); break; case "[Gmail]/Spam": Console.WriteLine("Spam has Children? " + folderInfo.HasChildren); break; case "[Gmail]/Starred": Console.WriteLine("Starred has Children? " + folderInfo.HasChildren); break; } } } //ExEnd:IMAP4ExtendedListCommand }
private void PopulateImapFolders(ImapClient client, ImapFolderInfoCollection folderInfoColl, TreeNodeCollection nodes) { // Add the current collection to the node foreach (ImapFolderInfo folderInfo in folderInfoColl) { int msgCount = 0; if (folderInfo.Name.ToUpper().Contains("AIB")) { client.SelectFolder(folderInfo.Name); msgCount = client.ListMessages(10).Count; } TreeNode node = new TreeNode(Common.formatImapFolderName(folderInfo.Name) + "(" + msgCount + ")", folderInfo.Name); nodes.Add(node); // If this folder has children, add them as well if (folderInfo.Selectable == true) { PopulateImapFolders(client, client.ListFolders(folderInfo.Name), node.ChildNodes); } } }
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."); }
private void AddFolders(Imap imap, TreeNode parent, ImapFolderInfoCollection folders) { var delimiter = imap.Delimiter; foreach (ImapFolderInfo info in folders) { var index = info.Name.LastIndexOf(delimiter); TreeNode node = new TreeNode((index > 0) ? info.Name.Substring(index + 1) : info.Name); node.Tag = info.Name; if (parent == null) { treeViewFolders.Nodes.Add(node); } else { parent.Nodes.Add(node); } AddFolders(imap, node, imap.ListFolders(info.Name, (parent != null))); } }