public static void Run() { //ExStart: 1 ImapClient imapClient = new ImapClient(); imapClient.Host = "<HOST>"; imapClient.Port = 993; imapClient.Username = "******"; imapClient.Password = "******"; imapClient.SupportedEncryption = EncryptionProtocols.Tls; imapClient.SecurityOptions = SecurityOptions.SSLImplicit; imapClient.SelectFolder("Inbox"); imapClient.ConnectionsQuantity = 5; imapClient.UseMultiConnection = MultiConnectionMode.Enable; DateTime multiConnectionModeStartTime = DateTime.Now; ImapMessageInfoCollection messageInfoCol1 = imapClient.ListMessages(true); TimeSpan multiConnectionModeTimeSpan = DateTime.Now - multiConnectionModeStartTime; imapClient.UseMultiConnection = MultiConnectionMode.Disable; DateTime singleConnectionModeStartTime = DateTime.Now; ImapMessageInfoCollection messageInfoCol2 = imapClient.ListMessages(true); TimeSpan singleConnectionModeTimeSpan = DateTime.Now - singleConnectionModeStartTime; double performanceRelation = singleConnectionModeTimeSpan.TotalMilliseconds / multiConnectionModeTimeSpan.TotalMilliseconds; Console.WriteLine("Performance Relation: " + performanceRelation); //ExEnd: 1 Console.WriteLine("ImapListMessagesWithMultiConnection executed successfully."); }
public static void Run() { //ExStart:CopyMultipleMessagesFromOneFoldertoAnother using (ImapClient client = new ImapClient("exchange.aspose.com", "username", "password")) { // Create the destination folder string folderName = "EMAILNET-35242"; if (!client.ExistFolder(folderName)) { client.CreateFolder(folderName); } try { // Append a couple of messages to the server MailMessage message1 = new MailMessage( "*****@*****.**", "*****@*****.**", "EMAILNET-35242 - " + Guid.NewGuid(), "EMAILNET-35242 Improvement of copy method.Add ability to 'copy' multiple messages per invocation."); string uniqueId1 = client.AppendMessage(message1); MailMessage message2 = new MailMessage( "*****@*****.**", "*****@*****.**", "EMAILNET-35242 - " + Guid.NewGuid(), "EMAILNET-35242 Improvement of copy method.Add ability to 'copy' multiple messages per invocation."); string uniqueId2 = client.AppendMessage(message2); // Verify that the messages have been added to the mailbox client.SelectFolder(ImapFolderInfo.InBox); ImapMessageInfoCollection msgsColl = client.ListMessages(); foreach (ImapMessageInfo msgInfo in msgsColl) { Console.WriteLine(msgInfo.Subject); } // Copy multiple messages using the CopyMessages command and Verify that messages are copied to the destination folder client.CopyMessages(new[] { uniqueId1, uniqueId2 }, folderName); client.SelectFolder(folderName); msgsColl = client.ListMessages(); foreach (ImapMessageInfo msgInfo in msgsColl) { Console.WriteLine(msgInfo.Subject); } } finally { try { client.DeleteFolder(folderName); } catch { } } } //ExEnd:CopyMultipleMessagesFromOneFoldertoAnother }
static void Run() { // ExStart: MoveMessage ///<summary> /// This example shows how to move a message from one folder of a mailbox to another one using the ImapClient API of Aspose.Email for .NET /// Available from Aspose.Email for .NET 6.4.0 onwards /// -------------- Available API Overload Members -------------- /// Void ImapClient.MoveMessage(IConnection iConnection, int sequenceNumber, string folderName, bool commitDeletions) /// Void ImapClient.MoveMessage(IConnection iConnection, string uniqueId, string folderName, bool commitDeletions) /// Void ImapClient.MoveMessage(int sequenceNumber, string folderName, bool commitDeletions) /// Void ImapClient.MoveMessage(string uniqueId, string folderName, bool commitDeletions) /// Void ImapClient.MoveMessage(IConnection iConnection, int sequenceNumber, string folderName) /// Void ImapClient.MoveMessage(IConnection iConnection, string uniqueId, string folderName) /// Void ImapClient.MoveMessage(int sequenceNumber, string folderName) /// Void ImapClient.MoveMessage(string uniqueId, string folderName) ///</summary> using (ImapClient client = new ImapClient("host.domain.com", 993, "username", "password")) { string folderName = "EMAILNET-35151"; if (!client.ExistFolder(folderName)) { client.CreateFolder(folderName); } try { MailMessage message = new MailMessage( "*****@*****.**", "*****@*****.**", "EMAILNET-35151 - " + Guid.NewGuid(), "EMAILNET-35151 ImapClient: Provide option to Move Message"); client.SelectFolder(ImapFolderInfo.InBox); // Append the new message to Inbox folder string uniqueId = client.AppendMessage(ImapFolderInfo.InBox, message); ImapMessageInfoCollection messageInfoCol1 = client.ListMessages(); Console.WriteLine(messageInfoCol1.Count); // Now move the message to the folder EMAILNET-35151 client.MoveMessage(uniqueId, folderName); client.CommitDeletes(); // Verify that the message was moved to the new folder client.SelectFolder(folderName); messageInfoCol1 = client.ListMessages(); Console.WriteLine(messageInfoCol1.Count); // Verify that the message was moved from the Inbox client.SelectFolder(ImapFolderInfo.InBox); messageInfoCol1 = client.ListMessages(); Console.WriteLine(messageInfoCol1.Count); } finally { try { client.DeleteFolder(folderName); } catch { } } } // ExEnd: MoveMessage }
public static void Run() { try { // ExStart:SendIMAPasynchronousEmail // Create an imapclient with host, user and password ImapClient client = new ImapClient(); client.Host = "domain.com"; client.Username = "******"; client.Password = "******"; client.SelectFolder("InBox"); ImapMessageInfoCollection messages = client.ListMessages(); IAsyncResult res1 = client.BeginFetchMessage(messages[0].UniqueId); IAsyncResult res2 = client.BeginFetchMessage(messages[1].UniqueId); MailMessage msg1 = client.EndFetchMessage(res1); MailMessage msg2 = client.EndFetchMessage(res2); // ExEnd:SendIMAPasynchronousEmail // ExStart:SendIMAPasynchronousEmailThreadPool List <MailMessage> List = new List <MailMessage>(); ThreadPool.QueueUserWorkItem(delegate(object o) { client.SelectFolder("folderName"); ImapMessageInfoCollection messageInfoCol = client.ListMessages(); foreach (ImapMessageInfo messageInfo in messageInfoCol) { List.Add(client.FetchMessage(messageInfo.UniqueId)); } }); // ExEnd:SendIMAPasynchronousEmailThreadPool // ExStart:SendIMAPasynchronousEmailThreadPool1 List <MailMessage> List1 = new List <MailMessage>(); ThreadPool.QueueUserWorkItem(delegate(object o) { using (IDisposable connection = client.CreateConnection()) { client.SelectFolder("FolderName"); ImapMessageInfoCollection messageInfoCol = client.ListMessages(); foreach (ImapMessageInfo messageInfo in messageInfoCol) { List1.Add(client.FetchMessage(messageInfo.UniqueId)); } } }); // ExEnd:SendIMAPasynchronousEmailThreadPool1 } catch (Exception ex) { Console.Write(ex.Message); throw; } }
public static void Run() { try { // ExStart:SendIMAPasynchronousEmail // Create an imapclient with host, user and password ImapClient client = new ImapClient(); client.Host = "domain.com"; client.Username = "******"; client.Password = "******"; client.SelectFolder("InBox"); ImapMessageInfoCollection messages = client.ListMessages(); IAsyncResult res1 = client.BeginFetchMessage(messages[0].UniqueId); IAsyncResult res2 = client.BeginFetchMessage(messages[1].UniqueId); MailMessage msg1 = client.EndFetchMessage(res1); MailMessage msg2 = client.EndFetchMessage(res2); // ExEnd:SendIMAPasynchronousEmail // ExStart:SendIMAPasynchronousEmailThreadPool List<MailMessage> List = new List<MailMessage>(); ThreadPool.QueueUserWorkItem(delegate(object o) { client.SelectFolder("folderName"); ImapMessageInfoCollection messageInfoCol = client.ListMessages(); foreach (ImapMessageInfo messageInfo in messageInfoCol) { List.Add(client.FetchMessage(messageInfo.UniqueId)); } }); // ExEnd:SendIMAPasynchronousEmailThreadPool // ExStart:SendIMAPasynchronousEmailThreadPool1 List<MailMessage> List1 = new List<MailMessage>(); ThreadPool.QueueUserWorkItem(delegate(object o) { using (IDisposable connection = client.CreateConnection()) { client.SelectFolder("FolderName"); ImapMessageInfoCollection messageInfoCol = client.ListMessages(); foreach (ImapMessageInfo messageInfo in messageInfoCol) List1.Add(client.FetchMessage(messageInfo.UniqueId)); } }); // ExEnd:SendIMAPasynchronousEmailThreadPool1 } catch (Exception ex) { Console.Write(ex.Message); throw; } }
static void Run() { // ExStart: MoveMessage ///<summary> /// This example shows how to move a message from one folder of a mailbox to another one using the ImapClient API of Aspose.Email for .NET /// Available from Aspose.Email for .NET 6.4.0 onwards /// -------------- Available API Overload Members -------------- /// Void ImapClient.MoveMessage(IConnection iConnection, int sequenceNumber, string folderName, bool commitDeletions) /// Void ImapClient.MoveMessage(IConnection iConnection, string uniqueId, string folderName, bool commitDeletions) /// Void ImapClient.MoveMessage(int sequenceNumber, string folderName, bool commitDeletions) /// Void ImapClient.MoveMessage(string uniqueId, string folderName, bool commitDeletions) /// Void ImapClient.MoveMessage(IConnection iConnection, int sequenceNumber, string folderName) /// Void ImapClient.MoveMessage(IConnection iConnection, string uniqueId, string folderName) /// Void ImapClient.MoveMessage(int sequenceNumber, string folderName) /// Void ImapClient.MoveMessage(string uniqueId, string folderName) ///</summary> using (ImapClient client = new ImapClient("host.domain.com", 993, "username", "password")) { string folderName = "EMAILNET-35151"; if (!client.ExistFolder(folderName)) client.CreateFolder(folderName); try { MailMessage message = new MailMessage( "*****@*****.**", "*****@*****.**", "EMAILNET-35151 - " + Guid.NewGuid(), "EMAILNET-35151 ImapClient: Provide option to Move Message"); client.SelectFolder(ImapFolderInfo.InBox); // Append the new message to Inbox folder string uniqueId = client.AppendMessage(ImapFolderInfo.InBox, message); ImapMessageInfoCollection messageInfoCol1 = client.ListMessages(); Console.WriteLine(messageInfoCol1.Count); // Now move the message to the folder EMAILNET-35151 client.MoveMessage(uniqueId, folderName); client.CommitDeletes(); // Verify that the message was moved to the new folder client.SelectFolder(folderName); messageInfoCol1 = client.ListMessages(); Console.WriteLine(messageInfoCol1.Count); // Verify that the message was moved from the Inbox client.SelectFolder(ImapFolderInfo.InBox); messageInfoCol1 = client.ListMessages(); Console.WriteLine(messageInfoCol1.Count); } finally { try { client.DeleteFolder(folderName); } catch { } } } // ExEnd: MoveMessage }
public static void Run() { // ExStart:InternalDateFilter // Connect and log in to IMAP const string host = "host"; const int port = 143; const string username = "******"; const string password = "******"; ImapClient client = new ImapClient(host, port, username, password); client.SelectFolder("Inbox"); // Set conditions, Subject contains "Newsletter", Emails that arrived today ImapQueryBuilder builder = new ImapQueryBuilder(); builder.Subject.Contains("Newsletter"); builder.InternalDate.On(DateTime.Now); // Build the query and Get list of messages MailQuery query = builder.GetQuery(); ImapMessageInfoCollection messages = client.ListMessages(query); foreach (ImapMessageInfo info in messages) { Console.WriteLine("Internal Date: " + info.InternalDate); } // Disconnect from IMAP client.Dispose(); // ExEnd:InternalDateFilter }
public static void Run() { // Connect and log in to IMAP const string host = "host"; const int port = 143; const string username = "******"; const string password = "******"; ImapClient client = new ImapClient(host, port, username, password); try { client.SelectFolder("Inbox"); // ExStart:SpecifyEncodingForQueryBuilder // Set conditions ImapQueryBuilder builder = new ImapQueryBuilder(Encoding.UTF8); builder.Subject.Contains("ğüşıöç", true); MailQuery query = builder.GetQuery(); // ExEnd:SpecifyEncodingForQueryBuilder // Get list of messages ImapMessageInfoCollection messages = client.ListMessages(query); foreach (ImapMessageInfo info in messages) { Console.WriteLine("Message Id: " + info.MessageId); } // Disconnect from IMAP client.Dispose(); } catch (Exception ex) { Console.WriteLine(ex.Message); } }
public static void Run() { string dataDir = RunExamples.GetDataDir_IMAP(); // ExStart:ImapClientActivityLogging ImapClient client = new ImapClient("imap.gmail.com", 993, "*****@*****.**", "password"); // Set security mode client.SecurityOptions = SecurityOptions.Auto; try { // Get the message info collection ImapMessageInfoCollection list = client.ListMessages(); // Download each message for (int i = 0; i < list.Count; i++) { // Save the EML file locally client.SaveMessage(list[i].UniqueId, dataDir + list[i].UniqueId + ".eml"); } } catch (Exception ex) { Console.WriteLine(ex.Message); } // ExEnd:ImapClientActivityLogging }
public static void Run() { // ExStart:FilteringMessagesFromIMAPMailbox // Connect and log in to IMAP const string host = "host"; const int port = 143; const string username = "******"; const string password = "******"; ImapClient client = new ImapClient(host, port, username, password); client.SelectFolder("Inbox"); // Set conditions, Subject contains "Newsletter", Emails that arrived today ImapQueryBuilder builder = new ImapQueryBuilder(); builder.Subject.Contains("Newsletter"); builder.InternalDate.On(DateTime.Now); // Build the query and Get list of messages MailQuery query = builder.GetQuery(); ImapMessageInfoCollection messages = client.ListMessages(query); Console.WriteLine("Imap: " + messages.Count + " message(s) found."); // Disconnect from IMAP client.Dispose(); // ExEnd:FilteringMessagesFromIMAPMailbox }
public static void Run() { string dataDir = RunExamples.GetDataDir_Exchange(); try { ImapClient imapClient = new ImapClient("ex07sp1", "Administrator", "Evaluation1"); imapClient.SecurityOptions = SecurityOptions.Auto; // ExStart:SaveMessagesUsingIMAP // Select the Inbox folder imapClient.SelectFolder(ImapFolderInfo.InBox); // Get the list of messages ImapMessageInfoCollection msgCollection = imapClient.ListMessages(); foreach (ImapMessageInfo msgInfo in msgCollection) { // Fetch the message from inbox using its SequenceNumber from msgInfo MailMessage message = imapClient.FetchMessage(msgInfo.SequenceNumber); // Save the message to disc now message.Save(dataDir + msgInfo.SequenceNumber + "_out.msg", SaveOptions.DefaultMsgUnicode); } // ExEnd:SaveMessagesUsingIMAP } catch (Exception ex) { Console.WriteLine(ex.Message); } }
public static void Run() { //ExStart: 1 ImapClient imapClient = new ImapClient(); imapClient.Host = "<HOST>"; imapClient.Port = 993; imapClient.Username = "******"; imapClient.Password = "******"; imapClient.SupportedEncryption = EncryptionProtocols.Tls; imapClient.SecurityOptions = SecurityOptions.SSLImplicit; ImapMessageInfoCollection messageInfoCol = imapClient.ListMessages(); Console.WriteLine("ListMessages Count: " + messageInfoCol.Count); int[] sequenceNumberAr = messageInfoCol.Select((ImapMessageInfo mi) => mi.SequenceNumber).ToArray(); string[] uniqueIdAr = messageInfoCol.Select((ImapMessageInfo mi) => mi.UniqueId).ToArray(); IList <MailMessage> fetchedMessagesBySNumMC = imapClient.FetchMessages(sequenceNumberAr); Console.WriteLine("FetchMessages-sequenceNumberAr Count: " + fetchedMessagesBySNumMC.Count); IList <MailMessage> fetchedMessagesByUidMC = imapClient.FetchMessages(uniqueIdAr); Console.WriteLine("FetchMessages-uniqueIdAr Count: " + fetchedMessagesByUidMC.Count); //ExEnd: 1 Console.WriteLine("ImapFetchGroupMessages executed successfully."); }
public static void Run() { //ExStart:DeleteSingleMessage using (ImapClient client = new ImapClient("exchange.aspose.com", "username", "password")) { try { Console.WriteLine(client.UidPlusSupported.ToString()); // Append some test messages client.SelectFolder(ImapFolderInfo.InBox); MailMessage message = new MailMessage("*****@*****.**", "*****@*****.**", "EMAILNET-35227 - " + Guid.NewGuid(), "EMAILNET-35227 Add ability in ImapClient to delete message"); string emailId = client.AppendMessage(message); // Now verify that all the messages have been appended to the mailbox ImapMessageInfoCollection messageInfoCol = null; messageInfoCol = client.ListMessages(); Console.WriteLine(messageInfoCol.Count); // Select the inbox folder and Delete message client.SelectFolder(ImapFolderInfo.InBox); client.DeleteMessage(emailId); client.CommitDeletes(); } finally { } } //ExEnd:DeleteSingleMessage }
static void AccessIMAPServer(string accessToken) { using (ImapClient client = new ImapClient("imap.gmail.com", 993, "*****@*****.**", accessToken, true)) { client.SecurityOptions = SecurityOptions.SSLImplicit; client.SelectFolder("Inbox"); ImapMessageInfoCollection messageInfoCol = client.ListMessages(); } }
static void Run() { // ExStart: ListingMessagesWithPagingSupport ///<summary> /// This example shows the paging support of ImapClient for listing messages from the server /// Available in Aspose.Email for .NET 6.4.0 and onwards ///</summary> using (ImapClient client = new ImapClient("host.domain.com", 993, "username", "password")) { try { int messagesNum = 12; int itemsPerPage = 5; MailMessage message = null; // Create some test messages and append these to server's inbox for (int i = 0; i < messagesNum; i++) { message = new MailMessage( "*****@*****.**", "*****@*****.**", "EMAILNET-35157 - " + Guid.NewGuid(), "EMAILNET-35157 Move paging parameters to separate class"); client.AppendMessage(ImapFolderInfo.InBox, message); } // List messages from inbox client.SelectFolder(ImapFolderInfo.InBox); ImapMessageInfoCollection totalMessageInfoCol = client.ListMessages(); // Verify the number of messages added Console.WriteLine(totalMessageInfoCol.Count); ////////////////// RETREIVE THE MESSAGES USING PAGING SUPPORT//////////////////////////////////// List <ImapPageInfo> pages = new List <ImapPageInfo>(); PageSettings pageSettings = new PageSettings(); ImapPageInfo pageInfo = client.ListMessagesByPage(itemsPerPage, 0, pageSettings); Console.WriteLine(pageInfo.TotalCount); pages.Add(pageInfo); while (!pageInfo.LastPage) { pageInfo = client.ListMessagesByPage(itemsPerPage, pageInfo.NextPage.PageOffset, pageSettings); pages.Add(pageInfo); } int retrievedItems = 0; foreach (ImapPageInfo folderCol in pages) { retrievedItems += folderCol.Items.Count; } Console.WriteLine(retrievedItems); } finally { } } // ExEnd: ListingMessagesWithPagingSupport }
public static void Run() { // Connect and log in to POP3 const string host = "host"; const int port = 143; const string username = "******"; const string password = "******"; ImapClient client = new ImapClient(host, port, username, password); try { MailQueryBuilder builder = new MailQueryBuilder(); // ExStart:CombineQueriesWithAND // Emails from specific host, get all emails that arrived before today and all emails that arrived since 7 days ago builder.From.Contains("SpecificHost.com"); builder.InternalDate.Before(DateTime.Now); builder.InternalDate.Since(DateTime.Now.AddDays(-7)); // ExEnd:CombineQueriesWithAND // Build the query and Get list of messages MailQuery query = builder.GetQuery(); ImapMessageInfoCollection messages = client.ListMessages(query); Console.WriteLine("Imap: " + messages.Count + " message(s) found."); builder = new MailQueryBuilder(); // ExStart:CombiningQueriesWithOR // Specify OR condition builder.Or(builder.Subject.Contains("test"), builder.From.Contains("*****@*****.**")); // ExEnd:CombiningQueriesWithOR // Build the query and Get list of messages query = builder.GetQuery(); messages = client.ListMessages(query); Console.WriteLine("Imap: " + messages.Count + " message(s) found."); } catch (Exception ex) { Console.WriteLine(ex.Message); } }
public static void Run() { //ExStart:DeleteMultipleMessages using (ImapClient client = new ImapClient("exchange.aspose.com", "username", "password")) { try { Console.WriteLine(client.UidPlusSupported.ToString()); // Append some test messages client.SelectFolder(ImapFolderInfo.InBox); List<string> uidList = new List<string>(); const int messageNumber = 5; for (int i = 0; i < messageNumber; i++) { MailMessage message = new MailMessage( "*****@*****.**", "*****@*****.**", "EMAILNET-35226 - " + Guid.NewGuid(), "EMAILNET-35226 Add ability in ImapClient to delete messages and change flags for set of messages"); string uid = client.AppendMessage(message); uidList.Add(uid); } // Now verify that all the messages have been appended to the mailbox ImapMessageInfoCollection messageInfoCol = null; messageInfoCol = client.ListMessages(); Console.WriteLine(messageInfoCol.Count); // Bulk Delete Messages and Verify that the messages are deleted client.DeleteMessages(uidList, true); client.CommitDeletes(); messageInfoCol = null; messageInfoCol = client.ListMessages(); Console.WriteLine(messageInfoCol.Count); } finally { } } //ExStart:DeleteMultipleMessages }
public static void Run() { //ExStart:DeleteMultipleMessages using (ImapClient client = new ImapClient("exchange.aspose.com", "username", "password")) { try { Console.WriteLine(client.UidPlusSupported.ToString()); // Append some test messages client.SelectFolder(ImapFolderInfo.InBox); List <string> uidList = new List <string>(); const int messageNumber = 5; for (int i = 0; i < messageNumber; i++) { MailMessage message = new MailMessage( "*****@*****.**", "*****@*****.**", "EMAILNET-35226 - " + Guid.NewGuid(), "EMAILNET-35226 Add ability in ImapClient to delete messages and change flags for set of messages"); string uid = client.AppendMessage(message); uidList.Add(uid); } // Now verify that all the messages have been appended to the mailbox ImapMessageInfoCollection messageInfoCol = null; messageInfoCol = client.ListMessages(); Console.WriteLine(messageInfoCol.Count); // Bulk Delete Messages and Verify that the messages are deleted client.DeleteMessages(uidList, true); client.CommitDeletes(); messageInfoCol = null; messageInfoCol = client.ListMessages(); Console.WriteLine(messageInfoCol.Count); } finally { } } //ExStart:DeleteMultipleMessages }
static void Run() { // ExStart: ListingMessagesWithPagingSupport ///<summary> /// This example shows the paging support of ImapClient for listing messages from the server /// Available in Aspose.Email for .NET 6.4.0 and onwards ///</summary> using (ImapClient client = new ImapClient("host.domain.com", 993, "username", "password")) { try { int messagesNum = 12; int itemsPerPage = 5; MailMessage message = null; // Create some test messages and append these to server's inbox for (int i = 0; i < messagesNum; i++) { message = new MailMessage( "*****@*****.**", "*****@*****.**", "EMAILNET-35157 - " + Guid.NewGuid(), "EMAILNET-35157 Move paging parameters to separate class"); client.AppendMessage(ImapFolderInfo.InBox, message); } // List messages from inbox client.SelectFolder(ImapFolderInfo.InBox); ImapMessageInfoCollection totalMessageInfoCol = client.ListMessages(); // Verify the number of messages added Console.WriteLine(totalMessageInfoCol.Count); ////////////////// RETREIVE THE MESSAGES USING PAGING SUPPORT//////////////////////////////////// List<ImapPageInfo> pages = new List<ImapPageInfo>(); ImapPageInfo pageInfo = client.ListMessagesByPage(itemsPerPage); Console.WriteLine(pageInfo.TotalCount); pages.Add(pageInfo); while (!pageInfo.LastPage) { pageInfo = client.ListMessagesByPage(pageInfo.NextPage); pages.Add(pageInfo); } int retrievedItems = 0; foreach (ImapPageInfo folderCol in pages) retrievedItems += folderCol.Items.Count; Console.WriteLine(retrievedItems); } finally { } } // ExEnd: ListingMessagesWithPagingSupport }
public static void Run() { // ExStart:GetMessageIdUsingImapMessageInfo // Create an imapclient with host, user and password ImapClient client = new ImapClient(); client.Host = "domain.com"; client.Username = "******"; client.Password = "******"; client.SelectFolder("InBox"); ImapMessageInfoCollection msgsColl = client.ListMessages(true); Console.WriteLine("Total Messages: " + msgsColl.Count); // ExEnd:GetMessageIdUsingImapMessageInfo }
/// <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) { } }
public static void Run() { //ExStart: 1 ImapClient imapClient = new ImapClient(); imapClient.Host = "<HOST>"; imapClient.Port = 993; imapClient.Username = "******"; imapClient.Password = "******"; imapClient.SupportedEncryption = EncryptionProtocols.Tls; imapClient.SecurityOptions = SecurityOptions.SSLImplicit; ImapQueryBuilder imapQueryBuilder = new ImapQueryBuilder(); imapQueryBuilder.HasNoFlags(ImapMessageFlags.IsRead); /* get unread messages. */ MailQuery query = imapQueryBuilder.GetQuery(); imapClient.ReadOnly = true; imapClient.SelectFolder("Inbox"); ImapMessageInfoCollection messageInfoCol = imapClient.ListMessages(query); Console.WriteLine("Initial Unread Count: " + messageInfoCol.Count()); if (messageInfoCol.Count() > 0) { imapClient.FetchMessage(messageInfoCol[0].SequenceNumber); messageInfoCol = imapClient.ListMessages(query); // This count will be equal to the initial count Console.WriteLine("Updated Unread Count: " + messageInfoCol.Count()); } else { Console.WriteLine("No unread messages found"); } //ExEnd: 1 Console.WriteLine("ImapReadOnlyMode executed successfully."); }
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 { // Select the inbox folder client.SelectFolder(ImapFolderInfo.InBox); // Get the message info collection ImapMessageInfoCollection list = client.ListMessages(); // Download each message for (int i = 0; i < list.Count; i++) { //Save the EML file locally client.SaveMessage(list[i].UniqueId, dataDir + list[i].UniqueId + ".eml"); } //Disconnect to the remote IMAP server client.Disconnect(); } catch (Exception ex) { System.Console.Write(Environment.NewLine + ex.ToString()); } Console.WriteLine(Environment.NewLine + "Downloaded messages from IMAP server."); }
public static void Run() { // ExStart:ListingMessagesRecursively // Create an imapclient with host, user and password ImapClient client = new ImapClient(); client.Host = "domain.com"; client.Username = "******"; client.Password = "******"; client.SelectFolder("InBox"); ImapMessageInfoCollection msgsColl = client.ListMessages(true); Console.WriteLine("Total Messages: " + msgsColl.Count); // ExEnd:ListingMessagesRecursively }
public static void Run() { try { // ExStart:ConnectToGmailUsingIMAP // Connect to the Gmail server ImapClient imap = new ImapClient("imap.gmail.com", 993, "*****@*****.**", "pwd"); // ExEnd:ConnectToGmailUsingIMAP Console.WriteLine(imap.ListMessages().Count); } catch (Exception ex) { Console.WriteLine(ex.Message); } }
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:SetCustomFlag // Create a message MailMessage message = new MailMessage("*****@*****.**", "*****@*****.**", "subject", "message"); //Append the message to mailbox string uid = client.AppendMessage(ImapFolderInfo.InBox, message); //Add custom flags to the added messge client.AddMessageFlags(uid, ImapMessageFlags.Keyword("custom1") | ImapMessageFlags.Keyword("custom1_0")); //Retreive the messages for checking the presence of custom flag client.SelectFolder(ImapFolderInfo.InBox); ImapMessageInfoCollection messageInfos = client.ListMessages(); foreach (var inf in messageInfos) { ImapMessageFlags[] flags = inf.Flags.Split(); if (inf.ContainsKeyword("custom1")) { Console.WriteLine("Keyword found"); } } //ExEnd:SetCustomFlag Console.WriteLine("Setting Custom Flag to Message example executed successfully!"); client.Dispose(); } catch (Exception ex) { Console.Write(Environment.NewLine + ex); } }
/// 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 }
// ExStart:ExchangeServerUsesSSL public static void Run() { // Connect to Exchange Server using ImapClient class ImapClient imapClient = new ImapClient("ex07sp1", 993, "Administrator", "Evaluation1", new RemoteCertificateValidationCallback(RemoteCertificateValidationHandler)); imapClient.SecurityOptions = SecurityOptions.SSLExplicit; // Select the Inbox folder imapClient.SelectFolder(ImapFolderInfo.InBox); // Get the list of messages ImapMessageInfoCollection msgCollection = imapClient.ListMessages(); foreach (ImapMessageInfo msgInfo in msgCollection) { Console.WriteLine(msgInfo.Subject); } // Disconnect from the server imapClient.Dispose(); }
public static void Run() { try { // ExStart:RetrieveExtraParametersAsSummaryInformation using (ImapClient client = new ImapClient("host.domain.com", "username", "password")) { MailMessage message = new MailMessage("*****@*****.**", "*****@*****.**", "EMAILNET-38466 - " + Guid.NewGuid().ToString(), "EMAILNET-38466 Add extra parameters for UID FETCH command"); // append the message to the server string uid = client.AppendMessage(message); // wait for the message to be appended Thread.Sleep(5000); // Define properties to be fetched from server along with the message string[] messageExtraFields = new string[] { "X-GM-MSGID", "X-GM-THRID" }; // retreive the message summary information using it's UID ImapMessageInfo messageInfoUID = client.ListMessage(uid, messageExtraFields); // retreive the message summary information using it's sequence number ImapMessageInfo messageInfoSeqNum = client.ListMessage(1, messageExtraFields); // List messages in general from the server based on the defined properties ImapMessageInfoCollection messageInfoCol = client.ListMessages(messageExtraFields); ImapMessageInfo messageInfoFromList = messageInfoCol[0]; // verify that the parameters are fetched in the summary information foreach (string paramName in messageExtraFields) { Console.WriteLine(messageInfoFromList.ExtraParameters.ContainsKey(paramName)); Console.WriteLine(messageInfoUID.ExtraParameters.ContainsKey(paramName)); Console.WriteLine(messageInfoSeqNum.ExtraParameters.ContainsKey(paramName)); } } // ExEnd:RetrieveExtraParametersAsSummaryInformation } catch (Exception ex) { Console.Write(ex.Message); } }
public static void Run() { // ExStart:RetrievingMessagesAsynchronously // Connect and log in to IMAP using (ImapClient client = new ImapClient("host", "username", "password")) { client.SelectFolder("Issues/SubFolder"); ImapMessageInfoCollection messages = client.ListMessages(); AutoResetEvent evnt = new AutoResetEvent(false); MailMessage message = null; AsyncCallback callback = delegate(IAsyncResult ar) { message = client.EndFetchMessage(ar); evnt.Set(); }; client.BeginFetchMessage(messages[0].SequenceNumber, callback, null); evnt.WaitOne(); } // ExEnd:RetrievingMessagesAsynchronously }
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() { // ExStart:ConnectExchangeServerUsingIMAP // Connect to Exchange Server using ImapClient class ImapClient imapClient = new ImapClient("ex07sp1", "Administrator", "Evaluation1"); imapClient.SecurityOptions = SecurityOptions.Auto; // Select the Inbox folder imapClient.SelectFolder(ImapFolderInfo.InBox); // Get the list of messages ImapMessageInfoCollection msgCollection = imapClient.ListMessages(); foreach (ImapMessageInfo msgInfo in msgCollection) { Console.WriteLine(msgInfo.Subject); } // Disconnect from the server imapClient.Dispose(); // ExEnd:ConnectExchangeServerUsingIMAP }
public static void Run() { // ExStart:SavingMessagesFromIMAPServer // The path to the file directory. string dataDir = RunExamples.GetDataDir_IMAP(); // Create an imapclient with host, user and password ImapClient client = new ImapClient("localhost", "user", "password"); // Select the inbox folder and Get the message info collection client.SelectFolder(ImapFolderInfo.InBox); ImapMessageInfoCollection list = client.ListMessages(); // Download each message for (int i = 0; i < list.Count; i++) { // Save the message in MSG format MailMessage message = client.FetchMessage(list[i].UniqueId); message.Save(dataDir + list[i].UniqueId + "_out.msg", SaveOptions.DefaultMsgUnicode); } // ExEnd:SavingMessagesFromIMAPServer }
public static void Run() { // ExStart:ListMessagesWithMaximumNumberOfMessages // Create an imapclient with host, user and password ImapClient client = new ImapClient("localhost", "user", "password"); // Select the inbox folder and Get the message info collection ImapQueryBuilder builder = new ImapQueryBuilder(); MailQuery query = builder.Or( builder.Or( builder.Or( builder.Or( builder.Subject.Contains(" (1) "), builder.Subject.Contains(" (2) ")), builder.Subject.Contains(" (3) ")), builder.Subject.Contains(" (4) ")), builder.Subject.Contains(" (5) ")); ImapMessageInfoCollection messageInfoCol4 = client.ListMessages(query, 4); Console.WriteLine((messageInfoCol4.Count == 4) ? "Success" : "Failure"); // ExEnd:ListMessagesWithMaximumNumberOfMessages }
public static void Run() { try { // Connect to the Gmail server ImapClient imap = new ImapClient("imap.gmail.com", 993, "*****@*****.**", "pwd"); // ExStart:SaveGmailMessages // Gets the message info collection and Download each message ImapMessageInfoCollection list = imap.ListMessages(); for (int i = 0; i < list.Count; i++) { // Save the message as an EML file imap.SaveMessage(list[i].UniqueId, list[i].UniqueId + "_out.eml"); } // ExEnd:SaveGmailMessages } catch (Exception ex) { Console.WriteLine(ex.Message); } }
private void ListMessages() { 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>"; client.SelectFolder("inbox"); // get list of messages ImapMessageInfoCollection msgCollection = client.ListMessages(); gvMessages.DataSource = msgCollection; gvMessages.DataBind(); client.Disconnect(); } catch (Exception ex) { lblMessage.ForeColor = Color.Red; lblMessage.Text = "Error: " + ex.Message; } }
public static void Run() { //ExStart: 1 ImapClient imapClient = new ImapClient(); imapClient.Host = "<HOST>"; imapClient.Port = 993; imapClient.Username = "******"; imapClient.Password = "******"; imapClient.SupportedEncryption = EncryptionProtocols.Tls; imapClient.SecurityOptions = SecurityOptions.SSLImplicit; ImapMessageInfoCollection messageInfoCol = imapClient.ListMessages(); foreach (ImapMessageInfo imapMessageInfo in messageInfoCol) { Console.WriteLine("ListUnsubscribe Header: " + imapMessageInfo.ListUnsubscribe); } //ExEnd: 1 Console.WriteLine("GetListUnsubscribeHeader executed successfully."); }
public static void Run() { // The path to the File directory. string dataDir = RunExamples.GetDataDir_IMAP(); // 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:FetchEmailMessagesFromIMAPServer // Select the inbox folder and Get the message info collection client.SelectFolder(ImapFolderInfo.InBox); ImapMessageInfoCollection list = client.ListMessages(); // Download each message for (int i = 0; i < list.Count; i++) { // Save the EML file locally client.SaveMessage(list[i].UniqueId, dataDir + list[i].UniqueId + ".eml"); } // ExEnd:FetchEmailMessagesFromIMAPServer // Disconnect to the remote IMAP server client.Dispose(); } catch (Exception ex) { Console.Write(Environment.NewLine + ex); } }
public static void Run() { // ExStart:MessagesFromIMAPServerToDisk // The path to the File directory. string dataDir = RunExamples.GetDataDir_IMAP(); // 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 { // Select the inbox folder and Get the message info collection client.SelectFolder(ImapFolderInfo.InBox); ImapMessageInfoCollection list = client.ListMessages(); // Download each message for (int i = 0; i < list.Count; i++) { // Save the EML file locally client.SaveMessage(list[i].UniqueId, dataDir + list[i].UniqueId + ".eml"); } // Disconnect to the remote IMAP server client.Dispose(); } catch (Exception ex) { Console.Write(Environment.NewLine + ex); } // ExStart:MessagesFromIMAPServerToDisk Console.WriteLine(Environment.NewLine + "Downloaded messages from IMAP server."); }
static void Run() { // ExStart:SearchWithPagingSupport ///<summary> /// This example shows how to search for messages using ImapClient of the API with paging support /// Introduced in Aspose.Email for .NET 6.4.0 ///</summary> using (ImapClient client = new ImapClient("host.domain.com", 84, "username", "password")) { try { // Append some test messages int messagesNum = 12; int itemsPerPage = 5; MailMessage message = null; for (int i = 0; i < messagesNum; i++) { message = new MailMessage( "*****@*****.**", "*****@*****.**", "EMAILNET-35128 - " + Guid.NewGuid(), "111111111111111"); client.AppendMessage(ImapFolderInfo.InBox, message); } string body = "2222222222222"; for (int i = 0; i < messagesNum; i++) { message = new MailMessage( "*****@*****.**", "*****@*****.**", "EMAILNET-35128 - " + Guid.NewGuid(), body); client.AppendMessage(ImapFolderInfo.InBox, message); } ImapQueryBuilder iqb = new ImapQueryBuilder(); iqb.Body.Contains(body); MailQuery query = iqb.GetQuery(); client.SelectFolder(ImapFolderInfo.InBox); ImapMessageInfoCollection totalMessageInfoCol = client.ListMessages(query); Console.WriteLine(totalMessageInfoCol.Count); ////////////////////////////////////////////////////// List<ImapPageInfo> pages = new List<ImapPageInfo>(); ImapPageInfo pageInfo = client.ListMessagesByPage(ImapFolderInfo.InBox, query, itemsPerPage); pages.Add(pageInfo); while (!pageInfo.LastPage) { pageInfo = client.ListMessagesByPage(ImapFolderInfo.InBox, query, pageInfo.NextPage); pages.Add(pageInfo); } int retrievedItems = 0; foreach (ImapPageInfo folderCol in pages) retrievedItems += folderCol.Items.Count; } finally { } } // ExEnd: SearchWithPagingSupport }