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 }
private string GetTwoFactorCodeInternal() { var code = ""; try { using (var client = new ImapClient(_hostName, _port, Username, Password, AuthMethod.Auto, _useSsl, (sender, certificate, chain, errors) => true)) { List <uint> uids = client.Search(SearchCondition.To(Username).And(SearchCondition.From("ea.com"))).ToList(); var mails = client.GetMessages(uids, FetchOptions.Normal); foreach (var msg in mails) { if (msg == null) { continue; } if (msg.From.Address.Contains("ea.com") && Regex.IsMatch(msg.Subject, "([0-9]+)") && ((DateTime)msg.Date()).ToUniversalTime() > _codeSent.ToUniversalTime()) { var mailBody = msg.Subject; code = Regex.Match(mailBody, "([0-9]+)").Groups[1].Value; break; } } if (uids.Count > 0) { client.DeleteMessages(uids); } } } #pragma warning disable CS0168 catch (Exception e) { code = "EXC" + e; } #pragma warning restore CS0168 return(code); }
public void DeleteMessages(IEnumerable <uint> uids) { _internalClient.DeleteMessages(uids); }
public List <MailMessage> GetAllMessages(IReadOnlyCollection <string> toAddressFilters) { if (toAddressFilters == null) { throw new ArgumentNullException(nameof(toAddressFilters)); } ImapClient imapClient = null; try { imapClient = new ImapClient(Hostname, Port, UseSSL); EventLog.WriteTrace($"ImapEmailReceiver : Fetching emails from IMAP server for User Id : {UserId}, Port : {Port}, Server : {Hostname}."); var authMethod = UseIntegratedAuthentication ? AuthMethod.Login : AuthMethod.Auto; try { imapClient.Login(UserId, Password, authMethod); } catch (InvalidCredentialsException ex) { throw new Exception($"ImapEmailReceiver: Failed to connect to the IMAP server rejected the supplied credentials. User Id : {UserId}, Server : {Hostname}, Message : {ex.ToString()}"); } catch (Exception ex) { throw new Exception($"ImapEmailReceiver: An error occurred connecting to IMAP Server. User Id : {UserId}, Server : {Hostname}, Message : {ex.ToString()}"); } /* * This implementation of the email fetching is far from ideal however this is the only implementation that works reliably since the * SearchCondition.To() option does not appear to work reliably. Hence we need to fetch and examin each email indivdually to determine * if we are interested in it. */ var yesterday = DateTime.Now.AddDays(-1); var uids = imapClient.Search(SearchCondition.SentSince(yesterday).And(SearchCondition.Smaller(MaxEmailSize)), Folder); // SearchCondition.SentSince has resolution of days (not hours, minutes or seconds) var matchedUids = new List <uint>(); foreach (var uid in uids.Distinct()) { // fetch just the headers and check the To address var message = imapClient.GetMessage(uid, FetchOptions.HeadersOnly); if (!ContainsAddress(message.To, toAddressFilters)) { continue; } matchedUids.Add(uid); } if (matchedUids.Count == 0) { return(new List <MailMessage>()); } // now fetch the full email for those that we have matched. var messages = imapClient.GetMessages(matchedUids, FetchOptions.Normal).ToList(); #if !DEBUG // delete these emails imapClient.DeleteMessages(matchedUids); imapClient.Expunge(Folder); #endif EventLog.WriteTrace($"ImapEmailReceiver : Fetched {messages.Count} emails from IMAP server."); return(messages); } finally { imapClient.Logout(); imapClient.Dispose(); } }