static void Main() { // If using Professional version, put your serial key below. ComponentInfo.SetLicense("FREE-LIMITED-KEY"); using (ImapClient imap = new ImapClient("<ADDRESS> (e.g. imap.gmail.com)")) { imap.Connect(); imap.Authenticate("<USERNAME>", "<PASSWORD>"); // Create new folder. imap.CreateFolder("GemBox"); // List all folders on the server and display their information. // Notice the presence of new "GemBox" folder. IList <ImapFolderInfo> folders = imap.ListFolders(); foreach (ImapFolderInfo folder in folders) { Console.WriteLine($"{folder.Name,-18}: {string.Join(", ", folder.Flags)}"); } // Delete newly created folder. imap.DeleteFolder("GemBox"); Console.WriteLine(new string('-', 40)); // Again, list folders and display their information. // Notice the absence of new "GemBox" folder. folders = imap.ListFolders(); foreach (ImapFolderInfo folder in folders) { Console.WriteLine($"{folder.Name,-18}: {string.Join(", ", folder.Flags)}"); } } }
// 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 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 { // Get all folders in the currently subscribed folder Aspose.Email.Imap.ImapFolderInfoCollection folderInfoColl = client.ListFolders(); // Iterate through the collection to get folder info one by one foreach (Aspose.Email.Imap.ImapFolderInfo folderInfo in folderInfoColl) { // Folder name Console.WriteLine("Folder name is " + folderInfo.Name); ImapFolderInfo folderExtInfo = client.ListFolder(folderInfo.Name); // New messages in the folder Console.WriteLine("New message count: " + folderExtInfo.NewMessageCount); // Check whether its readonly Console.WriteLine("Is it readonly? " + folderExtInfo.ReadOnly); // Total number of messages Console.WriteLine("Total number of messages " + folderExtInfo.TotalMessageCount); } //Disconnect to the remote IMAP server client.Disconnect(); } catch (Exception ex) { System.Console.Write(Environment.NewLine + ex.ToString()); } Console.WriteLine(Environment.NewLine + "Getting folders information from IMAP server."); }
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."); }
/// <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); } } }
/// 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() { //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); } } }
static void Main() { // If using Professional version, put your serial key below. ComponentInfo.SetLicense("FREE-LIMITED-KEY"); using (ImapClient imap = new ImapClient("<ADDRESS> (e.g. imap.gmail.com)")) { imap.Connect(); imap.Authenticate("<USERNAME>", "<PASSWORD>"); // Select INBOX folder. imap.SelectInbox(); // List INBOX folder flags. IList <ImapFolderInfo> folders = imap.ListFolders(); foreach (string flag in imap.SelectedFolder.Flags) { Console.WriteLine(flag); } } }
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."); }
protected void brnSendEmail_Click(object sender, EventArgs e) { lblMessage.Text = ""; try { // initialize imap client ImapClient client = new ImapClient(); client.Host = txtHost.Text; client.Port = int.Parse(txtPort.Text); client.Username = txtUsername.Text; client.Password = txtPassword.Text; // SSL Settings if (chSSL.Checked == true) { client.EnableSsl = true; client.SecurityMode = ImapSslSecurityMode.Implicit; } // connect to imap server and login client.Connect(true); lblMessage.Text = "Successfully connected to IMAP Mail server.<br><hr>"; // get folders information ImapFolderInfoCollection folderInfoColl = client.ListFolders(); gvFolders.DataSource = folderInfoColl; gvFolders.DataBind(); // disconnect from imap server client.Disconnect(); } catch (Exception ex) { lblMessage.ForeColor = Color.Red; lblMessage.Text = "Error: " + ex.Message; } }