public ActionResult Index(int currentPageNumber, int nextPageNumber) { int startSerialNumber = ((nextPageNumber - 1) * 30) + 1; int endSerialNumber = ((nextPageNumber) * 30); List<Email> emails = new List<Email>(); try { userGmailConfig = FetchUserGmailProfile(); using (ImapClient client = new ImapClient()) { client.Connect(userGmailConfig.IncomingServerAddress, userGmailConfig.IncomingServerPort, true); client.Authenticate(new NetworkCredential(userGmailConfig.GmailUsername, userGmailConfig.GmailPassword)); var inbox = client.Inbox; inbox.Open(FolderAccess.ReadOnly); if (inbox.Count > 0) { int pageEndIndex = Math.Max(inbox.Count - startSerialNumber, 0); int pageStartIndex = Math.Max(inbox.Count - endSerialNumber, 0); var messages = inbox.Fetch(pageStartIndex, pageEndIndex, MessageSummaryItems.Envelope); messages = messages.OrderByDescending(message => message.Envelope.Date.Value.DateTime).ToList(); foreach (var message in messages) { if (startSerialNumber <= endSerialNumber) { Email tempEmail = new Email() { SerialNo = startSerialNumber, Uid = message.UniqueId, FromDisplayName = message.Envelope.From.First().Name, FromEmail = message.Envelope.From.First().ToString(), To = message.Envelope.To.ToString(), Subject = message.NormalizedSubject, TimeReceived = message.Envelope.Date.Value.DateTime, HasAttachment = message.Attachments.Count() > 0 ? true : false }; emails.Add(tempEmail); startSerialNumber++; } } } } ViewBag.EmailId = userGmailConfig.GmailUsername; ViewBag.NoOfEmails = endSerialNumber; if (currentPageNumber > nextPageNumber) { ViewBag.PageNumber = currentPageNumber - 1; } else { ViewBag.PageNumber = currentPageNumber + 1; } } catch (Exception ex) { ViewBag.ErrorMessage = "There was an error in processing your request. Exception: " + ex.Message; } return View(emails); }
public static void DownloadMessages () { using (var client = new ImapClient (new ProtocolLogger ("imap.log"))) { client.Connect ("imap.gmail.com", 993, SecureSocketOptions.SslOnConnect); client.Authenticate ("username", "password"); client.Inbox.Open (FolderAccess.ReadOnly); var uids = client.Inbox.Search (SearchQuery.All); foreach (var uid in uids) { var message = client.Inbox.GetMessage (uid); // write the message to a file message.WriteTo (string.Format ("{0}.msg", uid)); } client.Disconnect (true); } }
public void login() { result.credential = GoogleWebAuthorizationBroker.AuthorizeAsync( new ClientSecrets { ClientId = "316924129116-efm2iopj3strkrpbjkf7oeqmptn8ph27.apps.googleusercontent.com", ClientSecret = "MAxnno9YGxGTdtNiEqItt8-n" }, new[] { "https://mail.google.com/ email" }, "Tiramisu", CancellationToken.None, new FileDataStore("Analytics.Auth.Store")).Result; if (result.credential != null) { result.service = new PlusService(new BaseClientService.Initializer() { HttpClientInitializer = result.credential, ApplicationName = "Google mail", }); Google.Apis.Plus.v1.Data.Person me = result.service.People.Get("me").Execute(); Google.Apis.Plus.v1.Data.Person.EmailsData myAccountEmail = me.Emails.Where(a => a.Type == "account").FirstOrDefault(); imapClient = new MailKit.Net.Imap.ImapClient(); var credentials = new NetworkCredential(myAccountEmail.Value, result.credential.Token.AccessToken); imapClient.Connect("imap.gmail.com", 993, true, CancellationToken.None); imapClient.Authenticate(credentials, CancellationToken.None); } }
public static void Capabilities () { using (var client = new ImapClient ()) { client.Connect ("imap.gmail.com", 993, SecureSocketOptions.SslOnConnect); var mechanisms = string.Join (", ", client.AuthenticationMechanisms); Console.WriteLine ("The IMAP server supports the following SASL authentication mechanisms: {0}", mechanisms); client.Authenticate ("username", "password"); if (client.Capabilities.HasFlag (ImapCapabilities.Id)) { var clientImplementation = new ImapImplementation { Name = "MailKit", Version = "1.0" }; var serverImplementation = client.Identify (clientImplementation); Console.WriteLine ("Server implementation details:"); foreach (var property in serverImplementation.Properties) Console.WriteLine (" {0} = {1}", property.Key, property.Value); } if (client.Capabilities.HasFlag (ImapCapabilities.Acl)) { Console.WriteLine ("The IMAP server supports Access Control Lists."); Console.WriteLine ("The IMAP server supports the following access rights: {0}", client.Rights); Console.WriteLine ("The Inbox has the following access controls:"); var acl = client.Inbox.GetAccessControlList (); foreach (var ac in acl) Console.WriteLine (" {0} = {1}", ac.Name, ac.Rights); var myRights = client.Inbox.GetMyAccessRights (); Console.WriteLine ("Your current rights for the Inbox folder are: {0}", myRights); } if (client.Capabilities.HasFlag (ImapCapabilities.Quota)) { Console.WriteLine ("The IMAP server supports quotas."); Console.WriteLine ("The current quota for the Inbox is:"); var quota = client.Inbox.GetQuota (); if (quota.StorageLimit.HasValue && quota.StorageLimit.Value) Console.WriteLine (" Limited by storage space. Using {0} out of {1} bytes.", quota.CurrentStorageSize.Value, quota.StorageLimit.Value); if (quota.MessageLimit.HasValue && quota.MessageLimit.Value) Console.WriteLine (" Limited by the number of messages. Using {0} out of {1} bytes.", quota.CurrentMessageCount.Value, quota.MessageLimit.Value); Console.WriteLine ("The quota root is: {0}", quota.QuotaRoot); } if (client.Capabilities.HasFlag (ImapCapabilities.Thread)) { if (client.ThreadingAlgorithms.Contains (ThreadingAlgorithm.OrderedSubject)) Console.WriteLine ("The IMAP server supports threading by subject."); if (client.ThreadingAlgorithms.Contains (ThreadingAlgorithm.References)) Console.WriteLine ("The IMAP server supports threading by references."); } client.Disconnect (true); } }
public ImapClient Init() { if (BindingInfo == null) { throw new MissingFieldException("BindingInfo is missing"); } Client = new ImapClient(); Client.Timeout = 10000; Client.Connect(host: BindingInfo.Server, port: BindingInfo.Port, useSsl: BindingInfo.EnableSSL); Client.AuthenticationMechanisms.Remove("XOAUTH2"); Client.Authenticate(userName: BindingInfo.Mail, password: BindingInfo.Token); return Client; }
static async Task Process() { using (var client = new ImapClient()) { // Connection await client.ConnectAsync(_host, _port, _secure); // Note: since we don't have an OAuth2 token, disable // the XOAUTH2 authentication mechanism. client.AuthenticationMechanisms.Remove("XOAUTH"); // Authentication await client.AuthenticateAsync(_userName, _password); } }
public void Process() { using (var client = new ImapClient()) { Connect(client); var inbox = client.Inbox as ImapFolder; // process the analysis ProcessAnalysis(inbox); // add rules from different sources to this processor AddRules(); // process rules ProcessRules(inbox); client.Disconnect(true); } }
public void TestImapClientGreetingCapabilities () { var commands = new List<ImapReplayCommand> (); commands.Add (new ImapReplayCommand ("", "common.capability-greeting.txt")); using (var client = new ImapClient ()) { try { client.ReplayConnect ("localhost", new ImapReplayStream (commands, false), CancellationToken.None); } catch (Exception ex) { Assert.Fail ("Did not expect an exception in Connect: {0}", ex); } Assert.IsTrue (client.IsConnected, "Client failed to connect."); Assert.AreEqual (GreetingCapabilities, client.Capabilities); Assert.AreEqual (1, client.AuthenticationMechanisms.Count); Assert.IsTrue (client.AuthenticationMechanisms.Contains ("PLAIN"), "Expected SASL PLAIN auth mechanism"); } }
public void Start() { if (Account.IsValid () == false) return; client = new ImapClient (); IsActive = true; Task.Run (async () => { while (IsActive) { await ReadMessages(); await Task.Delay(500); } if (client.IsConnected) { Console.WriteLine("Disconnect is required"); await client.DisconnectAsync(true); } }); }
public static void Main(string[] args) { var logger = new ProtocolLogger (Console.OpenStandardError ()); using (var client = new ImapClient (logger)) { var credentials = new NetworkCredential ("*****@*****.**", "password"); var uri = new Uri ("imaps://imap.gmail.com"); client.Connect (uri); // Remove the XOAUTH2 authentication mechanism since we don't have an OAuth2 token. client.AuthenticationMechanisms.Remove ("XOAUTH2"); client.Authenticate (credentials); client.Inbox.Open (FolderAccess.ReadOnly); // keep track of the messages var messages = client.Inbox.Fetch (0, -1, MessageSummaryItems.Full | MessageSummaryItems.UniqueId).ToList (); // connect to some events... client.Inbox.MessagesArrived += (sender, e) => { // Note: the CountChanged event will fire when new messages arrive in the folder. var folder = (ImapFolder) sender; // New messages have arrived in the folder. Console.WriteLine ("{0}: {1} new messages have arrived.", folder, e.Count); // Note: your first instict may be to fetch these new messages now, but you cannot do // that in an event handler (the ImapFolder is not re-entrant). }; client.Inbox.MessageExpunged += (sender, e) => { var folder = (ImapFolder) sender; if (e.Index < messages.Count) { var message = messages[e.Index]; Console.WriteLine ("{0}: expunged message {1}: Subject: {2}", folder, e.Index, message.Envelope.Subject); // Note: If you are keeping a local cache of message information // (e.g. MessageSummary data) for the folder, then you'll need // to remove the message at e.Index. messages.RemoveAt (e.Index); } else { Console.WriteLine ("{0}: expunged message {1}: Unknown message.", folder, e.Index); } }; client.Inbox.MessageFlagsChanged += (sender, e) => { var folder = (ImapFolder) sender; Console.WriteLine ("{0}: flags for message {1} have changed to: {2}.", folder, e.Index, e.Flags); }; Console.WriteLine ("Hit any key to end the IDLE loop."); using (var done = new CancellationTokenSource ()) { var thread = new Thread (IdleLoop); thread.Start (new IdleState (client, done.Token)); Console.ReadKey (); done.Cancel (); thread.Join (); } if (client.Inbox.Count > messages.Count) { Console.WriteLine ("The new messages that arrived during IDLE are:"); foreach (var message in client.Inbox.Fetch (messages.Count, -1, MessageSummaryItems.Full | MessageSummaryItems.UniqueId)) Console.WriteLine ("Subject: {0}", message.Envelope.Subject); } client.Disconnect (true); } }
public async void TestImapClientDovecot () { var expectedFlags = MessageFlags.Answered | MessageFlags.Flagged | MessageFlags.Deleted | MessageFlags.Seen | MessageFlags.Draft; var expectedPermanentFlags = expectedFlags | MessageFlags.UserDefined; var commands = new List<ImapReplayCommand> (); commands.Add (new ImapReplayCommand ("", "dovecot.greeting.txt")); commands.Add (new ImapReplayCommand ("A00000000 LOGIN username password\r\n", "dovecot.authenticate.txt")); commands.Add (new ImapReplayCommand ("A00000001 NAMESPACE\r\n", "dovecot.namespace.txt")); commands.Add (new ImapReplayCommand ("A00000002 LIST \"\" \"INBOX\"\r\n", "dovecot.list-inbox.txt")); commands.Add (new ImapReplayCommand ("A00000003 LIST (SPECIAL-USE) \"\" \"*\"\r\n", "dovecot.list-special-use.txt")); commands.Add (new ImapReplayCommand ("A00000004 ENABLE QRESYNC CONDSTORE\r\n", "dovecot.enable-qresync.txt")); commands.Add (new ImapReplayCommand ("A00000005 LIST \"\" \"%\" RETURN (SUBSCRIBED CHILDREN STATUS (MESSAGES RECENT UIDNEXT UIDVALIDITY UNSEEN HIGHESTMODSEQ))\r\n", "dovecot.list-personal.txt")); commands.Add (new ImapReplayCommand ("A00000006 CREATE UnitTests.\r\n", ImapReplayCommandResponse.OK)); commands.Add (new ImapReplayCommand ("A00000007 LIST \"\" UnitTests\r\n", "dovecot.list-unittests.txt")); commands.Add (new ImapReplayCommand ("A00000008 CREATE UnitTests.Messages\r\n", ImapReplayCommandResponse.OK)); commands.Add (new ImapReplayCommand ("A00000009 LIST \"\" UnitTests.Messages\r\n", "dovecot.list-unittests-messages.txt")); var command = new StringBuilder ("A00000010 APPEND UnitTests.Messages"); var internalDates = new List<DateTimeOffset> (); var messages = new List<MimeMessage> (); var flags = new List<MessageFlags> (); var now = DateTimeOffset.Now; messages.Add (CreateThreadableMessage ("A", "<*****@*****.**>", null, now.AddMinutes (-7))); messages.Add (CreateThreadableMessage ("B", "<*****@*****.**>", "<*****@*****.**>", now.AddMinutes (-6))); messages.Add (CreateThreadableMessage ("C", "<*****@*****.**>", "<*****@*****.**> <*****@*****.**>", now.AddMinutes (-5))); messages.Add (CreateThreadableMessage ("D", "<*****@*****.**>", "<*****@*****.**>", now.AddMinutes (-4))); messages.Add (CreateThreadableMessage ("E", "<*****@*****.**>", "<*****@*****.**> <*****@*****.**> <*****@*****.**> <*****@*****.**>", now.AddMinutes (-3))); messages.Add (CreateThreadableMessage ("F", "<*****@*****.**>", "<*****@*****.**>", now.AddMinutes (-2))); messages.Add (CreateThreadableMessage ("G", "<*****@*****.**>", null, now.AddMinutes (-1))); messages.Add (CreateThreadableMessage ("H", "<*****@*****.**>", null, now)); for (int i = 0; i < messages.Count; i++) { var message = messages[i]; string latin1; long length; internalDates.Add (messages[i].Date); flags.Add (MessageFlags.Draft); using (var stream = new MemoryStream ()) { var options = FormatOptions.Default.Clone (); options.NewLineFormat = NewLineFormat.Dos; message.WriteTo (options, stream); length = stream.Length; stream.Position = 0; using (var reader = new StreamReader (stream, Latin1)) latin1 = reader.ReadToEnd (); } command.AppendFormat (" (\\Draft) \"{0}\" ", ImapUtils.FormatInternalDate (message.Date)); command.Append ('{'); command.AppendFormat ("{0}+", length); command.Append ("}\r\n"); command.Append (latin1); } command.Append ("\r\n"); commands.Add (new ImapReplayCommand (command.ToString (), "dovecot.multiappend.txt")); commands.Add (new ImapReplayCommand ("A00000011 SELECT UnitTests.Messages (CONDSTORE)\r\n", "dovecot.select-unittests-messages.txt")); commands.Add (new ImapReplayCommand ("A00000012 UID STORE 1:8 +FLAGS.SILENT (\\Seen)\r\n", "dovecot.store-seen.txt")); commands.Add (new ImapReplayCommand ("A00000013 UID STORE 1:3 +FLAGS.SILENT (\\Answered)\r\n", "dovecot.store-answered.txt")); commands.Add (new ImapReplayCommand ("A00000014 UID STORE 8 +FLAGS.SILENT (\\Deleted)\r\n", "dovecot.store-deleted.txt")); commands.Add (new ImapReplayCommand ("A00000015 UID EXPUNGE 8\r\n", "dovecot.uid-expunge.txt")); commands.Add (new ImapReplayCommand ("A00000016 UID THREAD REFERENCES US-ASCII ALL\r\n", "dovecot.thread-references.txt")); commands.Add (new ImapReplayCommand ("A00000017 UID THREAD ORDEREDSUBJECT US-ASCII UID 1:* ALL\r\n", "dovecot.thread-orderedsubject.txt")); commands.Add (new ImapReplayCommand ("A00000018 UNSELECT\r\n", ImapReplayCommandResponse.OK)); commands.Add (new ImapReplayCommand ("A00000019 SELECT UnitTests.Messages (QRESYNC (1436832084 2 1:8))\r\n", "dovecot.select-unittests-messages-qresync.txt")); commands.Add (new ImapReplayCommand ("A00000020 UID SEARCH RETURN (ALL COUNT MIN MAX) MODSEQ 2\r\n", "dovecot.search-changed-since.txt")); commands.Add (new ImapReplayCommand ("A00000021 UID FETCH 1:7 (UID FLAGS MODSEQ)\r\n", "dovecot.fetch1.txt")); commands.Add (new ImapReplayCommand ("A00000022 UID FETCH 1:* (UID FLAGS MODSEQ) (CHANGEDSINCE 2 VANISHED)\r\n", "dovecot.fetch2.txt")); commands.Add (new ImapReplayCommand ("A00000023 UID SORT RETURN (ALL COUNT MIN MAX) (REVERSE ARRIVAL) US-ASCII ALL\r\n", "dovecot.sort-reverse-arrival.txt")); commands.Add (new ImapReplayCommand ("A00000024 UID SEARCH RETURN () UNDELETED SEEN\r\n", "dovecot.optimized-search.txt")); commands.Add (new ImapReplayCommand ("A00000025 CREATE UnitTests.Destination\r\n", ImapReplayCommandResponse.OK)); commands.Add (new ImapReplayCommand ("A00000026 LIST \"\" UnitTests.Destination\r\n", "dovecot.list-unittests-destination.txt")); commands.Add (new ImapReplayCommand ("A00000027 UID COPY 1:7 UnitTests.Destination\r\n", "dovecot.copy.txt")); commands.Add (new ImapReplayCommand ("A00000028 UID MOVE 1:7 UnitTests.Destination\r\n", "dovecot.move.txt")); commands.Add (new ImapReplayCommand ("A00000029 STATUS UnitTests.Destination (MESSAGES RECENT UIDNEXT UIDVALIDITY UNSEEN HIGHESTMODSEQ)\r\n", "dovecot.status-unittests-destination.txt")); commands.Add (new ImapReplayCommand ("A00000030 SELECT UnitTests.Destination (CONDSTORE)\r\n", "dovecot.select-unittests-destination.txt")); commands.Add (new ImapReplayCommand ("A00000031 UID FETCH 1:* (UID FLAGS INTERNALDATE RFC822.SIZE ENVELOPE BODYSTRUCTURE MODSEQ BODY.PEEK[HEADER.FIELDS (REFERENCES X-MAILER)]) (CHANGEDSINCE 1 VANISHED)\r\n", "dovecot.fetch3.txt")); commands.Add (new ImapReplayCommand ("A00000032 FETCH 1:* (UID FLAGS INTERNALDATE RFC822.SIZE ENVELOPE BODYSTRUCTURE MODSEQ BODY.PEEK[HEADER.FIELDS (REFERENCES X-MAILER)]) (CHANGEDSINCE 1)\r\n", "dovecot.fetch4.txt")); commands.Add (new ImapReplayCommand ("A00000033 FETCH 1:14 (UID FLAGS INTERNALDATE RFC822.SIZE ENVELOPE BODYSTRUCTURE MODSEQ BODY.PEEK[HEADER.FIELDS (REFERENCES X-MAILER)]) (CHANGEDSINCE 1)\r\n", "dovecot.fetch5.txt")); commands.Add (new ImapReplayCommand ("A00000034 FETCH 1:* (UID FLAGS INTERNALDATE RFC822.SIZE ENVELOPE BODYSTRUCTURE MODSEQ BODY.PEEK[HEADER.FIELDS (REFERENCES)]) (CHANGEDSINCE 1)\r\n", "dovecot.fetch6.txt")); commands.Add (new ImapReplayCommand ("A00000035 FETCH 1:14 (UID FLAGS INTERNALDATE RFC822.SIZE ENVELOPE BODYSTRUCTURE MODSEQ BODY.PEEK[HEADER.FIELDS (REFERENCES)]) (CHANGEDSINCE 1)\r\n", "dovecot.fetch7.txt")); commands.Add (new ImapReplayCommand ("A00000036 UID FETCH 1:* (UID FLAGS INTERNALDATE RFC822.SIZE ENVELOPE BODYSTRUCTURE MODSEQ BODY.PEEK[HEADER.FIELDS (REFERENCES X-MAILER)])\r\n", "dovecot.fetch8.txt")); commands.Add (new ImapReplayCommand ("A00000037 FETCH 1:* (UID FLAGS INTERNALDATE RFC822.SIZE ENVELOPE BODYSTRUCTURE MODSEQ BODY.PEEK[HEADER.FIELDS (REFERENCES X-MAILER)])\r\n", "dovecot.fetch9.txt")); commands.Add (new ImapReplayCommand ("A00000038 FETCH 1:14 (UID FLAGS INTERNALDATE RFC822.SIZE ENVELOPE BODYSTRUCTURE MODSEQ BODY.PEEK[HEADER.FIELDS (REFERENCES X-MAILER)])\r\n", "dovecot.fetch10.txt")); commands.Add (new ImapReplayCommand ("A00000039 FETCH 1:* (UID FLAGS INTERNALDATE RFC822.SIZE ENVELOPE BODYSTRUCTURE MODSEQ BODY.PEEK[HEADER.FIELDS (REFERENCES)])\r\n", "dovecot.fetch11.txt")); commands.Add (new ImapReplayCommand ("A00000040 FETCH 1:14 (UID FLAGS INTERNALDATE RFC822.SIZE ENVELOPE BODYSTRUCTURE MODSEQ BODY.PEEK[HEADER.FIELDS (REFERENCES)])\r\n", "dovecot.fetch12.txt")); commands.Add (new ImapReplayCommand ("A00000041 UID FETCH 1 (BODY.PEEK[HEADER] BODY.PEEK[TEXT])\r\n", "dovecot.getbodypart.txt")); commands.Add (new ImapReplayCommand ("A00000042 FETCH 1 (BODY.PEEK[HEADER] BODY.PEEK[TEXT])\r\n", "dovecot.getbodypart2.txt")); commands.Add (new ImapReplayCommand ("A00000043 UID FETCH 1 (BODY.PEEK[]<128.64>)\r\n", "dovecot.getstream.txt")); commands.Add (new ImapReplayCommand ("A00000044 UID FETCH 1 (BODY.PEEK[]<128.64>)\r\n", "dovecot.getstream2.txt")); commands.Add (new ImapReplayCommand ("A00000045 FETCH 1 (BODY.PEEK[]<128.64>)\r\n", "dovecot.getstream3.txt")); commands.Add (new ImapReplayCommand ("A00000046 FETCH 1 (BODY.PEEK[]<128.64>)\r\n", "dovecot.getstream4.txt")); commands.Add (new ImapReplayCommand ("A00000047 UID FETCH 1 (BODY.PEEK[HEADER.FIELDS (MIME-VERSION CONTENT-TYPE)])\r\n", "dovecot.getstream-section.txt")); commands.Add (new ImapReplayCommand ("A00000048 FETCH 1 (BODY.PEEK[HEADER.FIELDS (MIME-VERSION CONTENT-TYPE)])\r\n", "dovecot.getstream-section2.txt")); commands.Add (new ImapReplayCommand ("A00000049 UID STORE 1:14 (UNCHANGEDSINCE 3) +FLAGS.SILENT (\\Deleted $MailKit)\r\n", "dovecot.store-deleted-custom.txt")); commands.Add (new ImapReplayCommand ("A00000050 STORE 1:7 (UNCHANGEDSINCE 5) FLAGS.SILENT (\\Deleted \\Seen $MailKit)\r\n", "dovecot.setflags-unchangedsince.txt")); commands.Add (new ImapReplayCommand ("A00000051 UID SEARCH RETURN () UID 1:14 OR ANSWERED OR DELETED OR DRAFT OR FLAGGED OR RECENT OR UNANSWERED OR UNDELETED OR UNDRAFT OR UNFLAGGED OR UNSEEN OR KEYWORD $MailKit UNKEYWORD $MailKit\r\n", "dovecot.search-uids.txt")); commands.Add (new ImapReplayCommand ("A00000052 UID SEARCH RETURN (ALL COUNT MIN MAX) UID 1:14 LARGER 256 SMALLER 512\r\n", "dovecot.search-uids-options.txt")); commands.Add (new ImapReplayCommand ("A00000053 UID SORT RETURN () (REVERSE DATE SUBJECT DISPLAYFROM SIZE) US-ASCII OR OR (SENTBEFORE 12-Oct-2016 SENTSINCE 10-Oct-2016) NOT SENTON 11-Oct-2016 OR (BEFORE 12-Oct-2016 SINCE 10-Oct-2016) NOT ON 11-Oct-2016\r\n", "dovecot.sort-by-date.txt")); commands.Add (new ImapReplayCommand ("A00000054 UID SORT RETURN () (FROM TO CC) US-ASCII UID 1:14 OR BCC xyz OR CC xyz OR FROM xyz OR TO xyz OR SUBJECT xyz OR HEADER Message-Id mimekit.net OR BODY \"This is the message body.\" TEXT message\r\n", "dovecot.sort-by-strings.txt")); commands.Add (new ImapReplayCommand ("A00000055 UID SORT RETURN (ALL COUNT MIN MAX) (DISPLAYTO) US-ASCII UID 1:14 OLDER 1 YOUNGER 3600\r\n", "dovecot.sort-uids-options.txt")); commands.Add (new ImapReplayCommand ("A00000056 UID SEARCH ALL\r\n", "dovecot.search-raw.txt")); commands.Add (new ImapReplayCommand ("A00000057 UID SORT (REVERSE ARRIVAL) US-ASCII ALL\r\n", "dovecot.sort-raw.txt")); commands.Add (new ImapReplayCommand ("A00000058 EXPUNGE\r\n", "dovecot.expunge.txt")); commands.Add (new ImapReplayCommand ("A00000059 CLOSE\r\n", ImapReplayCommandResponse.OK)); using (var client = new ImapClient ()) { try { client.ReplayConnect ("localhost", new ImapReplayStream (commands, false)); } catch (Exception ex) { Assert.Fail ("Did not expect an exception in Connect: {0}", ex); } Assert.IsTrue (client.IsConnected, "Client failed to connect."); Assert.AreEqual (DovecotInitialCapabilities, client.Capabilities); Assert.AreEqual (4, client.AuthenticationMechanisms.Count); Assert.IsTrue (client.AuthenticationMechanisms.Contains ("PLAIN"), "Expected SASL PLAIN auth mechanism"); Assert.IsTrue (client.AuthenticationMechanisms.Contains ("DIGEST-MD5"), "Expected SASL DIGEST-MD5 auth mechanism"); Assert.IsTrue (client.AuthenticationMechanisms.Contains ("CRAM-MD5"), "Expected SASL CRAM-MD5 auth mechanism"); Assert.IsTrue (client.AuthenticationMechanisms.Contains ("NTLM"), "Expected SASL NTLM auth mechanism"); // Note: we do not want to use SASL at all... client.AuthenticationMechanisms.Clear (); try { await client.AuthenticateAsync ("username", "password"); } catch (Exception ex) { Assert.Fail ("Did not expect an exception in Authenticate: {0}", ex); } Assert.AreEqual (DovecotAuthenticatedCapabilities, client.Capabilities); Assert.AreEqual (1, client.InternationalizationLevel, "Expected I18NLEVEL=1"); Assert.IsTrue (client.ThreadingAlgorithms.Contains (ThreadingAlgorithm.OrderedSubject), "Expected THREAD=ORDEREDSUBJECT"); Assert.IsTrue (client.ThreadingAlgorithms.Contains (ThreadingAlgorithm.References), "Expected THREAD=REFERENCES"); // TODO: verify CONTEXT=SEARCH var personal = client.GetFolder (client.PersonalNamespaces[0]); // Make sure these all throw NotSupportedException Assert.Throws<NotSupportedException> (() => client.EnableUTF8 ()); Assert.Throws<NotSupportedException> (() => client.Inbox.GetAccessRights ("smith")); Assert.Throws<NotSupportedException> (() => client.Inbox.GetMyAccessRights ()); var rights = new AccessRights ("lrswida"); Assert.Throws<NotSupportedException> (() => client.Inbox.AddAccessRights ("smith", rights)); Assert.Throws<NotSupportedException> (() => client.Inbox.RemoveAccessRights ("smith", rights)); Assert.Throws<NotSupportedException> (() => client.Inbox.SetAccessRights ("smith", rights)); Assert.Throws<NotSupportedException> (() => client.Inbox.RemoveAccess ("smith")); Assert.Throws<NotSupportedException> (() => client.Inbox.GetQuota ()); Assert.Throws<NotSupportedException> (() => client.Inbox.SetQuota (null, null)); Assert.Throws<NotSupportedException> (() => client.GetMetadata (MetadataTag.PrivateComment)); Assert.Throws<NotSupportedException> (() => client.GetMetadata (new MetadataTag[] { MetadataTag.PrivateComment })); Assert.Throws<NotSupportedException> (() => client.SetMetadata (new MetadataCollection ())); var labels = new string[] { "Label1", "Label2" }; Assert.Throws<NotSupportedException> (() => client.Inbox.AddLabels (UniqueId.MinValue, labels, true)); Assert.Throws<NotSupportedException> (() => client.Inbox.AddLabels (UniqueIdRange.All, labels, true)); Assert.Throws<NotSupportedException> (() => client.Inbox.AddLabels (UniqueIdRange.All, 1, labels, true)); Assert.Throws<NotSupportedException> (() => client.Inbox.AddLabels (0, labels, true)); Assert.Throws<NotSupportedException> (() => client.Inbox.AddLabels (new int[] { 0 }, labels, true)); Assert.Throws<NotSupportedException> (() => client.Inbox.AddLabels (new int[] { 0 }, 1, labels, true)); Assert.Throws<NotSupportedException> (() => client.Inbox.RemoveLabels (UniqueId.MinValue, labels, true)); Assert.Throws<NotSupportedException> (() => client.Inbox.RemoveLabels (UniqueIdRange.All, labels, true)); Assert.Throws<NotSupportedException> (() => client.Inbox.RemoveLabels (UniqueIdRange.All, 1, labels, true)); Assert.Throws<NotSupportedException> (() => client.Inbox.RemoveLabels (0, labels, true)); Assert.Throws<NotSupportedException> (() => client.Inbox.RemoveLabels (new int[] { 0 }, labels, true)); Assert.Throws<NotSupportedException> (() => client.Inbox.RemoveLabels (new int[] { 0 }, 1, labels, true)); Assert.Throws<NotSupportedException> (() => client.Inbox.SetLabels (UniqueId.MinValue, labels, true)); Assert.Throws<NotSupportedException> (() => client.Inbox.SetLabels (UniqueIdRange.All, labels, true)); Assert.Throws<NotSupportedException> (() => client.Inbox.SetLabels (UniqueIdRange.All, 1, labels, true)); Assert.Throws<NotSupportedException> (() => client.Inbox.SetLabels (0, labels, true)); Assert.Throws<NotSupportedException> (() => client.Inbox.SetLabels (new int[] { 0 }, labels, true)); Assert.Throws<NotSupportedException> (() => client.Inbox.SetLabels (new int[] { 0 }, 1, labels, true)); try { await client.EnableQuickResyncAsync (); } catch (Exception ex) { Assert.Fail ("Did not expect an exception when enabling QRESYNC: {0}", ex); } // take advantage of LIST-STATUS to get top-level personal folders... var statusItems = StatusItems.Count | StatusItems.HighestModSeq | StatusItems.Recent | StatusItems.UidNext | StatusItems.UidValidity | StatusItems.Unread; var folders = (await personal.GetSubfoldersAsync (statusItems, false)).ToArray (); Assert.AreEqual (6, folders.Length, "Expected 6 folders"); var expectedFolderNames = new [] { "Archives", "Drafts", "Junk", "Sent Messages", "Trash", "INBOX" }; var expectedUidValidities = new [] { 1436832059, 1436832060, 1436832061, 1436832062, 1436832063, 1436832057 }; var expectedHighestModSeq = new [] { 1, 1, 1, 1, 1, 15 }; var expectedMessages = new [] { 0, 0, 0, 0, 0, 4 }; var expectedUidNext = new [] { 1, 1, 1, 1, 1, 5 }; var expectedRecent = new [] { 0, 0, 0, 0, 0, 0 }; var expectedUnseen = new [] { 0, 0, 0, 0, 0, 0 }; for (int i = 0; i < folders.Length; i++) { Assert.AreEqual (expectedFolderNames[i], folders[i].FullName, "FullName did not match"); Assert.AreEqual (expectedFolderNames[i], folders[i].Name, "Name did not match"); Assert.AreEqual (expectedUidValidities[i], folders[i].UidValidity, "UidValidity did not match"); Assert.AreEqual (expectedHighestModSeq[i], folders[i].HighestModSeq, "HighestModSeq did not match"); Assert.AreEqual (expectedMessages[i], folders[i].Count, "Count did not match"); Assert.AreEqual (expectedRecent[i], folders[i].Recent, "Recent did not match"); Assert.AreEqual (expectedUnseen[i], folders[i].Unread, "Unread did not match"); } var unitTests = await personal.CreateAsync ("UnitTests", false); Assert.AreEqual (FolderAttributes.HasNoChildren, unitTests.Attributes, "Unexpected UnitTests folder attributes"); var folder = await unitTests.CreateAsync ("Messages", true); Assert.AreEqual (FolderAttributes.HasNoChildren, folder.Attributes, "Unexpected UnitTests.Messages folder attributes"); //Assert.AreEqual (FolderAttributes.HasChildren, unitTests.Attributes, "Expected UnitTests Attributes to be updated"); // Use MULTIAPPEND to append some test messages var appended = await folder.AppendAsync (messages, flags, internalDates); Assert.AreEqual (8, appended.Count, "Unexpected number of messages appended"); // SELECT the folder so that we can test some stuff var access = await folder.OpenAsync (FolderAccess.ReadWrite); Assert.AreEqual (expectedPermanentFlags, folder.PermanentFlags, "UnitTests.Messages PERMANENTFLAGS"); Assert.AreEqual (expectedFlags, folder.AcceptedFlags, "UnitTests.Messages FLAGS"); Assert.AreEqual (8, folder.Count, "UnitTests.Messages EXISTS"); Assert.AreEqual (8, folder.Recent, "UnitTests.Messages RECENT"); Assert.AreEqual (0, folder.FirstUnread, "UnitTests.Messages UNSEEN"); Assert.AreEqual (1436832084U, folder.UidValidity, "UnitTests.Messages UIDVALIDITY"); Assert.AreEqual (9, folder.UidNext.Value.Id, "UnitTests.Messages UIDNEXT"); Assert.AreEqual (2UL, folder.HighestModSeq, "UnitTests.Messages HIGHESTMODSEQ"); Assert.AreEqual (FolderAccess.ReadWrite, access, "Expected UnitTests.Messages to be opened in READ-WRITE mode"); // Keep track of various folder events var flagsChanged = new List<MessageFlagsChangedEventArgs> (); var modSeqChanged = new List<ModSeqChangedEventArgs> (); var vanished = new List<MessagesVanishedEventArgs> (); bool recentChanged = false; folder.MessageFlagsChanged += (sender, e) => { flagsChanged.Add (e); }; folder.ModSeqChanged += (sender, e) => { modSeqChanged.Add (e); }; folder.MessagesVanished += (sender, e) => { vanished.Add (e); }; folder.RecentChanged += (sender, e) => { recentChanged = true; }; // Keep track of UIDVALIDITY and HIGHESTMODSEQ values for our QRESYNC test later var highestModSeq = folder.HighestModSeq; var uidValidity = folder.UidValidity; // Make some FLAGS changes to our messages so we can test QRESYNC await folder.AddFlagsAsync (appended, MessageFlags.Seen, true); Assert.AreEqual (0, flagsChanged.Count, "Unexpected number of FlagsChanged events"); Assert.AreEqual (8, modSeqChanged.Count, "Unexpected number of ModSeqChanged events"); for (int i = 0; i < modSeqChanged.Count; i++) { Assert.AreEqual (i, modSeqChanged[i].Index, "Unexpected modSeqChanged[{0}].Index", i); Assert.AreEqual (i + 1, modSeqChanged[i].UniqueId.Value.Id, "Unexpected modSeqChanged[{0}].UniqueId", i); Assert.AreEqual (3, modSeqChanged[i].ModSeq, "Unexpected modSeqChanged[{0}].ModSeq", i); } Assert.IsFalse (recentChanged, "Unexpected RecentChanged event"); modSeqChanged.Clear (); flagsChanged.Clear (); var answered = new UniqueIdSet (SortOrder.Ascending); answered.Add (appended[0]); // A answered.Add (appended[1]); // B answered.Add (appended[2]); // C await folder.AddFlagsAsync (answered, MessageFlags.Answered, true); Assert.AreEqual (0, flagsChanged.Count, "Unexpected number of FlagsChanged events"); Assert.AreEqual (3, modSeqChanged.Count, "Unexpected number of ModSeqChanged events"); for (int i = 0; i < modSeqChanged.Count; i++) { Assert.AreEqual (i, modSeqChanged[i].Index, "Unexpected modSeqChanged[{0}].Index", i); Assert.AreEqual (i + 1, modSeqChanged[i].UniqueId.Value.Id, "Unexpected modSeqChanged[{0}].UniqueId", i); Assert.AreEqual (4, modSeqChanged[i].ModSeq, "Unexpected modSeqChanged[{0}].ModSeq", i); } Assert.IsFalse (recentChanged, "Unexpected RecentChanged event"); modSeqChanged.Clear (); flagsChanged.Clear (); // Delete some messages so we can test that QRESYNC emits some MessageVanished events // both now *and* when we use QRESYNC to re-open the folder var deleted = new UniqueIdSet (SortOrder.Ascending); deleted.Add (appended[7]); // H await folder.AddFlagsAsync (deleted, MessageFlags.Deleted, true); Assert.AreEqual (0, flagsChanged.Count, "Unexpected number of FlagsChanged events"); Assert.AreEqual (1, modSeqChanged.Count, "Unexpected number of ModSeqChanged events"); Assert.AreEqual (7, modSeqChanged[0].Index, "Unexpected modSeqChanged[{0}].Index", 0); Assert.AreEqual (8, modSeqChanged[0].UniqueId.Value.Id, "Unexpected modSeqChanged[{0}].UniqueId", 0); Assert.AreEqual (5, modSeqChanged[0].ModSeq, "Unexpected modSeqChanged[{0}].ModSeq", 0); Assert.IsFalse (recentChanged, "Unexpected RecentChanged event"); modSeqChanged.Clear (); flagsChanged.Clear (); await folder.ExpungeAsync (deleted); Assert.AreEqual (1, vanished.Count, "Expected MessagesVanished event"); Assert.AreEqual (1, vanished[0].UniqueIds.Count, "Unexpected number of messages vanished"); Assert.AreEqual (8, vanished[0].UniqueIds[0].Id, "Unexpected UID for vanished message"); Assert.IsFalse (vanished[0].Earlier, "Expected EARLIER to be false"); Assert.IsTrue (recentChanged, "Expected RecentChanged event"); recentChanged = false; vanished.Clear (); // Verify that THREAD works correctly var threaded = await folder.ThreadAsync (ThreadingAlgorithm.References, SearchQuery.All); Assert.AreEqual (2, threaded.Count, "Unexpected number of root nodes in threaded results"); threaded = await folder.ThreadAsync (UniqueIdRange.All, ThreadingAlgorithm.OrderedSubject, SearchQuery.All); Assert.AreEqual (7, threaded.Count, "Unexpected number of root nodes in threaded results"); // UNSELECT the folder so we can re-open it using QRESYNC await folder.CloseAsync (); // Use QRESYNC to get the changes since last time we opened the folder access = await folder.OpenAsync (FolderAccess.ReadWrite, uidValidity, highestModSeq, appended); Assert.AreEqual (FolderAccess.ReadWrite, access, "Expected UnitTests.Messages to be opened in READ-WRITE mode"); Assert.AreEqual (7, flagsChanged.Count, "Unexpected number of MessageFlagsChanged events"); Assert.AreEqual (7, modSeqChanged.Count, "Unexpected number of ModSeqChanged events"); for (int i = 0; i < flagsChanged.Count; i++) { var messageFlags = MessageFlags.Seen | MessageFlags.Draft; if (i < 3) messageFlags |= MessageFlags.Answered; Assert.AreEqual (i, flagsChanged[i].Index, "Unexpected value for flagsChanged[{0}].Index", i); Assert.AreEqual ((uint) (i + 1), flagsChanged[i].UniqueId.Value.Id, "Unexpected value for flagsChanged[{0}].UniqueId", i); Assert.AreEqual (messageFlags, flagsChanged[i].Flags, "Unexpected value for flagsChanged[{0}].Flags", i); Assert.AreEqual (i, modSeqChanged[i].Index, "Unexpected value for modSeqChanged[{0}].Index", i); if (i < 3) Assert.AreEqual (4, modSeqChanged[i].ModSeq, "Unexpected value for modSeqChanged[{0}].ModSeq", i); else Assert.AreEqual (3, modSeqChanged[i].ModSeq, "Unexpected value for modSeqChanged[{0}].ModSeq", i); } modSeqChanged.Clear (); flagsChanged.Clear (); Assert.AreEqual (1, vanished.Count, "Unexpected number of MessagesVanished events"); Assert.IsTrue (vanished[0].Earlier, "Expected VANISHED EARLIER"); Assert.AreEqual (1, vanished[0].UniqueIds.Count, "Unexpected number of messages vanished"); Assert.AreEqual (8, vanished[0].UniqueIds[0].Id, "Unexpected UID for vanished message"); vanished.Clear (); // Use SEARCH and FETCH to get the same info var searchOptions = SearchOptions.All | SearchOptions.Count | SearchOptions.Min | SearchOptions.Max; var changed = await folder.SearchAsync (searchOptions, SearchQuery.ChangedSince (highestModSeq)); Assert.AreEqual (7, changed.UniqueIds.Count, "Unexpected number of UIDs"); Assert.IsTrue (changed.ModSeq.HasValue, "Expected the ModSeq property to be set"); Assert.AreEqual (4, changed.ModSeq.Value, "Unexpected ModSeq value"); Assert.AreEqual (1, changed.Min.Value.Id, "Unexpected Min"); Assert.AreEqual (7, changed.Max.Value.Id, "Unexpected Max"); Assert.AreEqual (7, changed.Count, "Unexpected Count"); var fetched = await folder.FetchAsync (changed.UniqueIds, MessageSummaryItems.UniqueId | MessageSummaryItems.Flags | MessageSummaryItems.ModSeq); Assert.AreEqual (7, fetched.Count, "Unexpected number of messages fetched"); for (int i = 0; i < fetched.Count; i++) { Assert.AreEqual (i, fetched[i].Index, "Unexpected Index"); Assert.AreEqual (i + 1, fetched[i].UniqueId.Id, "Unexpected UniqueId"); } // or... we could just use a single UID FETCH command like so: fetched = await folder.FetchAsync (UniqueIdRange.All, highestModSeq, MessageSummaryItems.UniqueId | MessageSummaryItems.Flags | MessageSummaryItems.ModSeq); for (int i = 0; i < fetched.Count; i++) { Assert.AreEqual (i, fetched[i].Index, "Unexpected Index"); Assert.AreEqual (i + 1, fetched[i].UniqueId.Id, "Unexpected UniqueId"); } Assert.AreEqual (7, fetched.Count, "Unexpected number of messages fetched"); Assert.AreEqual (1, vanished.Count, "Unexpected number of MessagesVanished events"); Assert.IsTrue (vanished[0].Earlier, "Expected VANISHED EARLIER"); Assert.AreEqual (1, vanished[0].UniqueIds.Count, "Unexpected number of messages vanished"); Assert.AreEqual (8, vanished[0].UniqueIds[0].Id, "Unexpected UID for vanished message"); vanished.Clear (); // Use SORT to order by reverse arrival order var orderBy = new OrderBy[] { new OrderBy (OrderByType.Arrival, SortOrder.Descending) }; var sorted = await folder.SearchAsync (searchOptions, SearchQuery.All, orderBy); Assert.AreEqual (7, sorted.UniqueIds.Count, "Unexpected number of UIDs"); for (int i = 0; i < sorted.UniqueIds.Count; i++) Assert.AreEqual (7 - i, sorted.UniqueIds[i].Id, "Unexpected value for UniqueId[{0}]", i); Assert.IsFalse (sorted.ModSeq.HasValue, "Expected the ModSeq property to be null"); Assert.AreEqual (7, sorted.Min.Value.Id, "Unexpected Min"); Assert.AreEqual (1, sorted.Max.Value.Id, "Unexpected Max"); Assert.AreEqual (7, sorted.Count, "Unexpected Count"); // Verify that optimizing NOT queries works correctly var uids = await folder.SearchAsync (SearchQuery.Not (SearchQuery.Deleted).And (SearchQuery.Not (SearchQuery.NotSeen))); Assert.AreEqual (7, uids.Count, "Unexpected number of UIDs"); for (int i = 0; i < uids.Count; i++) Assert.AreEqual (i + 1, uids[i].Id, "Unexpected value for uids[{0}]", i); // Create a Destination folder to use for copying/moving messages to var destination = await unitTests.CreateAsync ("Destination", true); Assert.AreEqual (FolderAttributes.HasNoChildren, destination.Attributes, "Unexpected UnitTests.Destination folder attributes"); // COPY messages to the Destination folder var copied = await folder.CopyToAsync (uids, destination); Assert.AreEqual (uids.Count, copied.Source.Count, "Unexpetced Source.Count"); Assert.AreEqual (uids.Count, copied.Destination.Count, "Unexpetced Destination.Count"); // MOVE messages to the Destination folder var moved = await folder.MoveToAsync (uids, destination); Assert.AreEqual (uids.Count, copied.Source.Count, "Unexpetced Source.Count"); Assert.AreEqual (uids.Count, copied.Destination.Count, "Unexpetced Destination.Count"); Assert.AreEqual (1, vanished.Count, "Expected VANISHED event"); vanished.Clear (); await destination.StatusAsync (statusItems); Assert.AreEqual (moved.Destination[0].Validity, destination.UidValidity, "Unexpected UIDVALIDITY"); destination.MessageFlagsChanged += (sender, e) => { flagsChanged.Add (e); }; destination.ModSeqChanged += (sender, e) => { modSeqChanged.Add (e); }; destination.MessagesVanished += (sender, e) => { vanished.Add (e); }; destination.RecentChanged += (sender, e) => { recentChanged = true; }; await destination.OpenAsync (FolderAccess.ReadWrite); Assert.AreEqual (FolderAccess.ReadWrite, access, "Expected UnitTests.Destination to be opened in READ-WRITE mode"); var fetchHeaders = new HashSet<HeaderId> (); fetchHeaders.Add (HeaderId.References); fetchHeaders.Add (HeaderId.XMailer); var indexes = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 }; // Fetch + modseq fetched = await destination.FetchAsync (UniqueIdRange.All, 1, MessageSummaryItems.Full | MessageSummaryItems.UniqueId | MessageSummaryItems.BodyStructure | MessageSummaryItems.ModSeq | MessageSummaryItems.References, fetchHeaders); Assert.AreEqual (14, fetched.Count, "Unexpected number of messages fetched"); fetched = await destination.FetchAsync (0, -1, 1, MessageSummaryItems.Full | MessageSummaryItems.UniqueId | MessageSummaryItems.BodyStructure | MessageSummaryItems.ModSeq | MessageSummaryItems.References, fetchHeaders); Assert.AreEqual (14, fetched.Count, "Unexpected number of messages fetched"); fetched = await destination.FetchAsync (indexes, 1, MessageSummaryItems.Full | MessageSummaryItems.UniqueId | MessageSummaryItems.BodyStructure | MessageSummaryItems.ModSeq | MessageSummaryItems.References, fetchHeaders); Assert.AreEqual (14, fetched.Count, "Unexpected number of messages fetched"); fetched = await destination.FetchAsync (0, -1, 1, MessageSummaryItems.Full | MessageSummaryItems.UniqueId | MessageSummaryItems.BodyStructure | MessageSummaryItems.ModSeq | MessageSummaryItems.References); Assert.AreEqual (14, fetched.Count, "Unexpected number of messages fetched"); fetched = await destination.FetchAsync (indexes, 1, MessageSummaryItems.Full | MessageSummaryItems.UniqueId | MessageSummaryItems.BodyStructure | MessageSummaryItems.ModSeq | MessageSummaryItems.References); Assert.AreEqual (14, fetched.Count, "Unexpected number of messages fetched"); // Fetch fetched = await destination.FetchAsync (UniqueIdRange.All, MessageSummaryItems.Full | MessageSummaryItems.UniqueId | MessageSummaryItems.BodyStructure | MessageSummaryItems.ModSeq | MessageSummaryItems.References, fetchHeaders); Assert.AreEqual (14, fetched.Count, "Unexpected number of messages fetched"); fetched = await destination.FetchAsync (0, -1, MessageSummaryItems.Full | MessageSummaryItems.UniqueId | MessageSummaryItems.BodyStructure | MessageSummaryItems.ModSeq | MessageSummaryItems.References, fetchHeaders); Assert.AreEqual (14, fetched.Count, "Unexpected number of messages fetched"); fetched = await destination.FetchAsync (indexes, MessageSummaryItems.Full | MessageSummaryItems.UniqueId | MessageSummaryItems.BodyStructure | MessageSummaryItems.ModSeq | MessageSummaryItems.References, fetchHeaders); Assert.AreEqual (14, fetched.Count, "Unexpected number of messages fetched"); fetched = await destination.FetchAsync (0, -1, MessageSummaryItems.Full | MessageSummaryItems.UniqueId | MessageSummaryItems.BodyStructure | MessageSummaryItems.ModSeq | MessageSummaryItems.References); Assert.AreEqual (14, fetched.Count, "Unexpected number of messages fetched"); fetched = await destination.FetchAsync (indexes, MessageSummaryItems.Full | MessageSummaryItems.UniqueId | MessageSummaryItems.BodyStructure | MessageSummaryItems.ModSeq | MessageSummaryItems.References); Assert.AreEqual (14, fetched.Count, "Unexpected number of messages fetched"); uids = new UniqueIdSet (SortOrder.Ascending); for (int i = 0; i < fetched.Count; i++) { Assert.AreEqual (i, fetched[i].Index, "Unexpected Index"); Assert.AreEqual (i + 1, fetched[i].UniqueId.Id, "Unexpected UniqueId"); uids.Add (fetched[i].UniqueId); } var entity = await destination.GetBodyPartAsync (fetched[0].UniqueId, fetched[0].TextBody); Assert.IsInstanceOf<TextPart> (entity); entity = await destination.GetBodyPartAsync (fetched[0].Index, fetched[0].TextBody); Assert.IsInstanceOf<TextPart> (entity); using (var stream = await destination.GetStreamAsync (fetched[0].UniqueId, 128, 64)) { Assert.AreEqual (64, stream.Length, "Unexpected stream length"); string text; using (var reader = new StreamReader (stream)) text = reader.ReadToEnd (); Assert.AreEqual ("nit Tests <*****@*****.**>\r\nMIME-Version: 1.0\r\nContent-T", text); } using (var stream = await destination.GetStreamAsync (fetched[0].UniqueId, "", 128, 64)) { Assert.AreEqual (64, stream.Length, "Unexpected stream length"); string text; using (var reader = new StreamReader (stream)) text = reader.ReadToEnd (); Assert.AreEqual ("nit Tests <*****@*****.**>\r\nMIME-Version: 1.0\r\nContent-T", text); } using (var stream = await destination.GetStreamAsync (fetched[0].Index, 128, 64)) { Assert.AreEqual (64, stream.Length, "Unexpected stream length"); string text; using (var reader = new StreamReader (stream)) text = reader.ReadToEnd (); Assert.AreEqual ("nit Tests <*****@*****.**>\r\nMIME-Version: 1.0\r\nContent-T", text); } using (var stream = await destination.GetStreamAsync (fetched[0].Index, "", 128, 64)) { Assert.AreEqual (64, stream.Length, "Unexpected stream length"); string text; using (var reader = new StreamReader (stream)) text = reader.ReadToEnd (); Assert.AreEqual ("nit Tests <*****@*****.**>\r\nMIME-Version: 1.0\r\nContent-T", text); } using (var stream = await destination.GetStreamAsync (fetched[0].UniqueId, "HEADER.FIELDS (MIME-VERSION CONTENT-TYPE)")) { Assert.AreEqual (62, stream.Length, "Unexpected stream length"); string text; using (var reader = new StreamReader (stream)) text = reader.ReadToEnd (); Assert.AreEqual ("MIME-Version: 1.0\r\nContent-Type: text/plain; charset=utf-8\r\n\r\n", text); } using (var stream = await destination.GetStreamAsync (fetched[0].Index, "HEADER.FIELDS (MIME-VERSION CONTENT-TYPE)")) { Assert.AreEqual (62, stream.Length, "Unexpected stream length"); string text; using (var reader = new StreamReader (stream)) text = reader.ReadToEnd (); Assert.AreEqual ("MIME-Version: 1.0\r\nContent-Type: text/plain; charset=utf-8\r\n\r\n", text); } var custom = new HashSet<string> (); custom.Add ("$MailKit"); await destination.AddFlagsAsync (uids, destination.HighestModSeq, MessageFlags.Deleted, custom, true); Assert.AreEqual (14, modSeqChanged.Count, "Unexpected number of ModSeqChanged events"); Assert.AreEqual (5, destination.HighestModSeq); for (int i = 0; i < modSeqChanged.Count; i++) { Assert.AreEqual (i, modSeqChanged[i].Index, "Unexpected value for modSeqChanged[{0}].Index", i); Assert.AreEqual (5, modSeqChanged[i].ModSeq, "Unexpected value for modSeqChanged[{0}].ModSeq", i); } modSeqChanged.Clear (); await destination.SetFlagsAsync (new int[] { 0, 1, 2, 3, 4, 5, 6 }, destination.HighestModSeq, MessageFlags.Seen | MessageFlags.Deleted, custom, true); Assert.AreEqual (7, modSeqChanged.Count, "Unexpected number of ModSeqChanged events"); Assert.AreEqual (6, destination.HighestModSeq); for (int i = 0; i < modSeqChanged.Count; i++) { Assert.AreEqual (i, modSeqChanged[i].Index, "Unexpected value for modSeqChanged[{0}].Index", i); Assert.AreEqual (6, modSeqChanged[i].ModSeq, "Unexpected value for modSeqChanged[{0}].ModSeq", i); } modSeqChanged.Clear (); var results = await destination.SearchAsync (uids, SearchQuery.Answered.Or (SearchQuery.Deleted.Or (SearchQuery.Draft.Or (SearchQuery.Flagged.Or (SearchQuery.Recent.Or (SearchQuery.NotAnswered.Or (SearchQuery.NotDeleted.Or (SearchQuery.NotDraft.Or (SearchQuery.NotFlagged.Or (SearchQuery.NotSeen.Or (SearchQuery.HasCustomFlag ("$MailKit").Or (SearchQuery.DoesNotHaveCustomFlag ("$MailKit"))))))))))))); Assert.AreEqual (14, results.Count, "Unexpected number of UIDs"); var matches = await destination.SearchAsync (searchOptions, uids, SearchQuery.LargerThan (256).And (SearchQuery.SmallerThan (512))); var expectedMatchedUids = new uint[] { 2, 3, 4, 5, 6, 9, 10, 11, 12, 13 }; Assert.AreEqual (10, matches.Count, "Unexpected COUNT"); Assert.AreEqual (13, matches.Max.Value.Id, "Unexpected MAX"); Assert.AreEqual (2, matches.Min.Value.Id, "Unexpected MIN"); Assert.AreEqual (10, matches.UniqueIds.Count, "Unexpected number of UIDs"); for (int i = 0; i < matches.UniqueIds.Count; i++) Assert.AreEqual (expectedMatchedUids[i], matches.UniqueIds[i].Id); orderBy = new OrderBy[] { OrderBy.ReverseDate, OrderBy.Subject, OrderBy.DisplayFrom, OrderBy.Size }; var sentDateQuery = SearchQuery.Or (SearchQuery.And (SearchQuery.SentBefore (new DateTime (2016, 10, 12)), SearchQuery.SentAfter (new DateTime (2016, 10, 10))), SearchQuery.Not (SearchQuery.SentOn (new DateTime (2016, 10, 11)))); var deliveredDateQuery = SearchQuery.Or (SearchQuery.And (SearchQuery.DeliveredBefore (new DateTime (2016, 10, 12)), SearchQuery.DeliveredAfter (new DateTime (2016, 10, 10))), SearchQuery.Not (SearchQuery.DeliveredOn (new DateTime (2016, 10, 11)))); results = await destination.SearchAsync (sentDateQuery.Or (deliveredDateQuery), orderBy); var expectedSortByDateResults = new uint[] { 7, 14, 6, 13, 5, 12, 4, 11, 3, 10, 2, 9, 1, 8 }; Assert.AreEqual (14, results.Count, "Unexpected number of UIDs"); for (int i = 0; i < results.Count; i++) Assert.AreEqual (expectedSortByDateResults[i], results[i].Id); var stringQuery = SearchQuery.BccContains ("xyz").Or (SearchQuery.CcContains ("xyz").Or (SearchQuery.FromContains ("xyz").Or (SearchQuery.ToContains ("xyz").Or (SearchQuery.SubjectContains ("xyz").Or (SearchQuery.HeaderContains ("Message-Id", "mimekit.net").Or (SearchQuery.BodyContains ("This is the message body.").Or (SearchQuery.MessageContains ("message")))))))); orderBy = new OrderBy[] { OrderBy.From, OrderBy.To, OrderBy.Cc }; results = await destination.SearchAsync (uids, stringQuery, orderBy); Assert.AreEqual (14, results.Count, "Unexpected number of UIDs"); for (int i = 0; i < results.Count; i++) Assert.AreEqual (i + 1, results[i].Id); orderBy = new OrderBy[] { OrderBy.DisplayTo }; matches = await destination.SearchAsync (searchOptions, uids, SearchQuery.OlderThan (1).And (SearchQuery.YoungerThan (3600)), orderBy); Assert.AreEqual (14, matches.Count, "Unexpected COUNT"); Assert.AreEqual (14, matches.Max.Value.Id, "Unexpected MAX"); Assert.AreEqual (1, matches.Min.Value.Id, "Unexpected MIN"); Assert.AreEqual (14, matches.UniqueIds.Count, "Unexpected number of UIDs"); for (int i = 0; i < matches.UniqueIds.Count; i++) Assert.AreEqual (i + 1, matches.UniqueIds[i].Id); client.Capabilities &= ~ImapCapabilities.ESearch; matches = await ((ImapFolder) destination).SearchAsync ("ALL"); Assert.IsFalse (matches.Max.HasValue, "MAX should not be set"); Assert.IsFalse (matches.Min.HasValue, "MIN should not be set"); Assert.AreEqual (0, matches.Count, "COUNT should not be set"); Assert.AreEqual (14, matches.UniqueIds.Count); for (int i = 0; i < matches.UniqueIds.Count; i++) Assert.AreEqual (i + 1, matches.UniqueIds[i].Id); client.Capabilities &= ~ImapCapabilities.ESort; matches = await ((ImapFolder) destination).SortAsync ("(REVERSE ARRIVAL) US-ASCII ALL"); Assert.IsFalse (matches.Max.HasValue, "MAX should not be set"); Assert.IsFalse (matches.Min.HasValue, "MIN should not be set"); Assert.AreEqual (0, matches.Count, "COUNT should not be set"); Assert.AreEqual (14, matches.UniqueIds.Count); for (int i = 0; i < matches.UniqueIds.Count; i++) Assert.AreEqual (i + 1, matches.UniqueIds[i].Id); await destination.ExpungeAsync (); Assert.AreEqual (7, destination.HighestModSeq); Assert.AreEqual (1, vanished.Count, "Unexpected number of Vanished events"); Assert.AreEqual (14, vanished[0].UniqueIds.Count, "Unexpected number of UIDs in Vanished event"); for (int i = 0; i < vanished[0].UniqueIds.Count; i++) Assert.AreEqual (i + 1, vanished[0].UniqueIds[i].Id); Assert.IsFalse (vanished[0].Earlier, "Unexpected value for Earlier"); vanished.Clear (); await destination.CloseAsync (true); await client.DisconnectAsync (false); } }
public JsonResult Read(uint emailUid) { JsonResult email = new JsonResult(); var outputMessage = ""; try { userGmailConfig = FetchUserGmailProfile(); using (ImapClient client = new ImapClient()) { client.Connect(userGmailConfig.IncomingServerAddress, userGmailConfig.IncomingServerPort, true); client.Authenticate(new NetworkCredential(userGmailConfig.GmailUsername, userGmailConfig.GmailPassword)); var inbox = client.Inbox; inbox.Open(FolderAccess.ReadOnly); MimeMessage message = inbox.GetMessage(new UniqueId(emailUid)); email.Data = new { FromDisplayName = message.From.FirstOrDefault().Name, FromEmail = message.From.FirstOrDefault().ToString(), To = message.To.FirstOrDefault().ToString(), Subject = message.Subject, Body = message.HtmlBody, message = "Email fetched successfully" }; } } catch (Exception ex) { outputMessage = "There was an error in processing your request. Exception: " + ex.Message; } email.Data = new { message = outputMessage, }; return email; }
public JsonResult Delete(string csEmailUids) { JsonResult jsonResult = new JsonResult(); var outputMessage = ""; try { userGmailConfig = FetchUserGmailProfile(); using (ImapClient client = new ImapClient()) { client.Connect(userGmailConfig.IncomingServerAddress, userGmailConfig.IncomingServerPort, true); client.Authenticate(new NetworkCredential(userGmailConfig.GmailUsername, userGmailConfig.GmailPassword)); var inbox = client.Inbox; inbox.Open(FolderAccess.ReadWrite); var uids = new List<UniqueId>(); if (csEmailUids.Contains(',')) { foreach (var item in csEmailUids.Split(',')) { uids.Add(new UniqueId(Convert.ToUInt32(item))); } } else { uids.Add(new UniqueId(Convert.ToUInt32(csEmailUids))); } client.Inbox.AddFlags(uids, MessageFlags.Deleted, true); if (client.Capabilities.HasFlag(ImapCapabilities.UidPlus)) { client.Inbox.Expunge(uids); } else { client.Inbox.Expunge(); } outputMessage = "Email(s) deleted successfully!"; } } catch (Exception ex) { outputMessage = "There was an error in processing your request. Exception: " + ex.Message; } jsonResult.Data = new { message = outputMessage, }; return jsonResult; }
public ActionResult Index() { long noOfEmails = 1; UniqueId lastUid = new UniqueId(); List<Email> emails = new List<Email>(); try { userGmailConfig = FetchUserGmailProfile(); using (ImapClient client = new ImapClient()) { client.Connect(userGmailConfig.IncomingServerAddress, userGmailConfig.IncomingServerPort, true); client.Authenticate(new NetworkCredential(userGmailConfig.GmailUsername, userGmailConfig.GmailPassword)); var inbox = client.Inbox; inbox.Open(FolderAccess.ReadOnly); if (inbox.Count > 0) { int index = Math.Max(inbox.Count - 30, 0); var uids = (from c in inbox.Fetch(index, -1, MessageSummaryItems.UniqueId) select c.UniqueId).ToList(); var messages = inbox.Fetch(uids, MessageSummaryItems.Envelope); messages = messages.OrderByDescending(message => message.Envelope.Date.Value.DateTime).ToList(); foreach (var message in messages) { Email tempEmail = new Email() { SerialNo = noOfEmails, Uid = message.UniqueId, FromDisplayName = message.Envelope.From.First().Name, FromEmail = message.Envelope.From.First().ToString(), To = message.Envelope.To.ToString(), Subject = message.NormalizedSubject, TimeReceived = message.Envelope.Date.Value.DateTime, HasAttachment = message.Attachments.Count() > 0 ? true : false }; lastUid = tempEmail.Uid; emails.Add(tempEmail); noOfEmails++; } } } ViewBag.EmailId = userGmailConfig.GmailUsername; } catch (Exception ex) { ViewBag.ErrorMessage = "There was an error in processing your request. Exception: " + ex.Message; } ViewBag.NoOfEmails = 30; ViewBag.PageNumber = 1; return View(emails); }
public void TestExtractingPrecisePangolinAttachment() { var commands = new List<ImapReplayCommand> (); commands.Add (new ImapReplayCommand ("", "gmail.greeting.txt")); commands.Add (new ImapReplayCommand ("A00000000 CAPABILITY\r\n", "gmail.capability.txt")); commands.Add (new ImapReplayCommand ("A00000001 AUTHENTICATE PLAIN AHVzZXJuYW1lAHBhc3N3b3Jk\r\n", "gmail.authenticate.txt")); commands.Add (new ImapReplayCommand ("A00000002 NAMESPACE\r\n", "gmail.namespace.txt")); commands.Add (new ImapReplayCommand ("A00000003 LIST \"\" \"INBOX\"\r\n", "gmail.list-inbox.txt")); commands.Add (new ImapReplayCommand ("A00000004 XLIST \"\" \"*\"\r\n", "gmail.xlist.txt")); commands.Add (new ImapReplayCommand ("A00000005 LIST \"\" \"%\"\r\n", "gmail.list-personal.txt")); commands.Add (new ImapReplayCommand ("A00000006 EXAMINE INBOX (CONDSTORE)\r\n", "gmail.examine-inbox.txt")); commands.Add (new ImapReplayCommand ("A00000007 FETCH 270 (BODY.PEEK[])\r\n", "gmail.precise-pangolin-message.txt")); using (var client = new ImapClient ()) { try { client.ReplayConnect ("localhost", new ImapReplayStream (commands, false), CancellationToken.None); } catch (Exception ex) { Assert.Fail ("Did not expect an exception in Connect: {0}", ex); } Assert.IsTrue (client.IsConnected, "Client failed to connect."); Assert.AreEqual (GMailInitialCapabilities, client.Capabilities); Assert.AreEqual (4, client.AuthenticationMechanisms.Count); Assert.IsTrue (client.AuthenticationMechanisms.Contains ("XOAUTH"), "Expected SASL XOAUTH auth mechanism"); Assert.IsTrue (client.AuthenticationMechanisms.Contains ("XOAUTH2"), "Expected SASL XOAUTH2 auth mechanism"); Assert.IsTrue (client.AuthenticationMechanisms.Contains ("PLAIN"), "Expected SASL PLAIN auth mechanism"); Assert.IsTrue (client.AuthenticationMechanisms.Contains ("PLAIN-CLIENTTOKEN"), "Expected SASL PLAIN-CLIENTTOKEN auth mechanism"); try { var credentials = new NetworkCredential ("username", "password"); // Note: Do not try XOAUTH2 client.AuthenticationMechanisms.Remove ("XOAUTH2"); client.Authenticate (credentials, CancellationToken.None); } catch (Exception ex) { Assert.Fail ("Did not expect an exception in Authenticate: {0}", ex); } Assert.AreEqual (GMailAuthenticatedCapabilities, client.Capabilities); var inbox = client.Inbox; Assert.IsNotNull (inbox, "Expected non-null Inbox folder."); Assert.AreEqual (FolderAttributes.HasNoChildren, inbox.Attributes, "Expected Inbox attributes to be \\HasNoChildren."); foreach (var special in Enum.GetValues (typeof (SpecialFolder)).OfType<SpecialFolder> ()) { var folder = client.GetFolder (special); if (special != SpecialFolder.Archive) { var expected = GetSpecialFolderAttribute (special) | FolderAttributes.HasNoChildren; Assert.IsNotNull (folder, "Expected non-null {0} folder.", special); Assert.AreEqual (expected, folder.Attributes, "Expected {0} attributes to be \\HasNoChildren.", special); } else { Assert.IsNull (folder, "Expected null {0} folder.", special); } } var personal = client.GetFolder (client.PersonalNamespaces[0]); var folders = personal.GetSubfolders (false, CancellationToken.None).ToList (); Assert.AreEqual (client.Inbox, folders[0], "Expected the first folder to be the Inbox."); Assert.AreEqual ("[Gmail]", folders[1].FullName, "Expected the second folder to be [Gmail]."); Assert.AreEqual (FolderAttributes.NoSelect | FolderAttributes.HasChildren, folders[1].Attributes, "Expected [Gmail] folder to be \\Noselect \\HasChildren."); client.Inbox.Open (FolderAccess.ReadOnly, CancellationToken.None); var message = client.Inbox.GetMessage (269, CancellationToken.None); foreach (var attachment in message.Attachments) { using (var stream = File.Create ("attachment.txt")) { var options = FormatOptions.Default.Clone (); options.NewLineFormat = NewLineFormat.Dos; attachment.WriteTo (options, stream); } using (var stream = File.Create (attachment.FileName)) { attachment.ContentObject.DecodeTo (stream); stream.Close (); } } client.Disconnect (false, CancellationToken.None); } }
public async void TestAccessControlLists () { var commands = new List<ImapReplayCommand> (); commands.Add (new ImapReplayCommand ("", "gmail.greeting.txt")); commands.Add (new ImapReplayCommand ("A00000000 CAPABILITY\r\n", "acl.capability.txt")); commands.Add (new ImapReplayCommand ("A00000001 AUTHENTICATE PLAIN AHVzZXJuYW1lAHBhc3N3b3Jk\r\n", "acl.authenticate.txt")); commands.Add (new ImapReplayCommand ("A00000002 NAMESPACE\r\n", "gmail.namespace.txt")); commands.Add (new ImapReplayCommand ("A00000003 LIST \"\" \"INBOX\"\r\n", "gmail.list-inbox.txt")); commands.Add (new ImapReplayCommand ("A00000004 XLIST \"\" \"*\"\r\n", "gmail.xlist.txt")); commands.Add (new ImapReplayCommand ("A00000005 GETACL INBOX\r\n", "acl.getacl.txt")); commands.Add (new ImapReplayCommand ("A00000006 LISTRIGHTS INBOX smith\r\n", "acl.listrights.txt")); commands.Add (new ImapReplayCommand ("A00000007 MYRIGHTS INBOX\r\n", "acl.myrights.txt")); commands.Add (new ImapReplayCommand ("A00000008 SETACL INBOX smith +lrswida\r\n", ImapReplayCommandResponse.OK)); commands.Add (new ImapReplayCommand ("A00000009 SETACL INBOX smith -lrswida\r\n", ImapReplayCommandResponse.OK)); commands.Add (new ImapReplayCommand ("A00000010 SETACL INBOX smith lrswida\r\n", ImapReplayCommandResponse.OK)); commands.Add (new ImapReplayCommand ("A00000011 DELETEACL INBOX smith\r\n", ImapReplayCommandResponse.OK)); using (var client = new ImapClient ()) { try { client.ReplayConnect ("localhost", new ImapReplayStream (commands, false)); } catch (Exception ex) { Assert.Fail ("Did not expect an exception in Connect: {0}", ex); } Assert.IsTrue (client.IsConnected, "Client failed to connect."); Assert.AreEqual (AclInitialCapabilities, client.Capabilities); Assert.AreEqual (4, client.AuthenticationMechanisms.Count); Assert.IsTrue (client.AuthenticationMechanisms.Contains ("XOAUTH"), "Expected SASL XOAUTH auth mechanism"); Assert.IsTrue (client.AuthenticationMechanisms.Contains ("XOAUTH2"), "Expected SASL XOAUTH2 auth mechanism"); Assert.IsTrue (client.AuthenticationMechanisms.Contains ("PLAIN"), "Expected SASL PLAIN auth mechanism"); Assert.IsTrue (client.AuthenticationMechanisms.Contains ("PLAIN-CLIENTTOKEN"), "Expected SASL PLAIN-CLIENTTOKEN auth mechanism"); // Note: Do not try XOAUTH2 client.AuthenticationMechanisms.Remove ("XOAUTH2"); try { await client.AuthenticateAsync ("username", "password"); } catch (Exception ex) { Assert.Fail ("Did not expect an exception in Authenticate: {0}", ex); } Assert.AreEqual (AclAuthenticatedCapabilities, client.Capabilities); var inbox = client.Inbox; Assert.IsNotNull (inbox, "Expected non-null Inbox folder."); Assert.AreEqual (FolderAttributes.Inbox | FolderAttributes.HasNoChildren, inbox.Attributes, "Expected Inbox attributes to be \\HasNoChildren."); foreach (var special in Enum.GetValues (typeof (SpecialFolder)).OfType<SpecialFolder> ()) { var folder = client.GetFolder (special); if (special != SpecialFolder.Archive) { var expected = GetSpecialFolderAttribute (special) | FolderAttributes.HasNoChildren; Assert.IsNotNull (folder, "Expected non-null {0} folder.", special); Assert.AreEqual (expected, folder.Attributes, "Expected {0} attributes to be \\HasNoChildren.", special); } else { Assert.IsNull (folder, "Expected null {0} folder.", special); } } // GETACL INBOX var acl = await client.Inbox.GetAccessControlListAsync (); Assert.AreEqual (2, acl.Count, "The number of access controls does not match."); Assert.AreEqual ("Fred", acl[0].Name, "The identifier for the first access control does not match."); Assert.AreEqual ("rwipslxetad", acl[0].Rights.ToString (), "The access rights for the first access control does not match."); Assert.AreEqual ("Chris", acl[1].Name, "The identifier for the second access control does not match."); Assert.AreEqual ("lrswi", acl[1].Rights.ToString (), "The access rights for the second access control does not match."); // LISTRIGHTS INBOX smith Assert.Throws<ArgumentNullException> (() => client.Inbox.GetAccessRights (null)); //Assert.Throws<ArgumentException> (() => client.Inbox.GetAccessRights (string.Empty)); Assert.Throws<ArgumentNullException> (async () => await client.Inbox.GetAccessRightsAsync (null)); //Assert.Throws<ArgumentException> (async () => await client.Inbox.GetAccessRightsAsync (string.Empty)); var rights = await client.Inbox.GetAccessRightsAsync ("smith"); Assert.AreEqual ("lrswipkxtecda0123456789", rights.ToString (), "The access rights do not match for user smith."); // MYRIGHTS INBOX rights = await client.Inbox.GetMyAccessRightsAsync (); Assert.AreEqual ("rwiptsldaex", rights.ToString (), "My access rights do not match."); // SETACL INBOX smith +lrswida var empty = new AccessRights (string.Empty); rights = new AccessRights ("lrswida"); Assert.Throws<ArgumentNullException> (() => client.Inbox.AddAccessRights (null, rights)); //Assert.Throws<ArgumentException> (() => client.Inbox.AddAccessRights (string.Empty, rights)); Assert.Throws<ArgumentNullException> (() => client.Inbox.AddAccessRights ("smith", null)); Assert.Throws<ArgumentException> (() => client.Inbox.AddAccessRights ("smith", empty)); Assert.Throws<ArgumentNullException> (async () => await client.Inbox.AddAccessRightsAsync (null, rights)); //Assert.Throws<ArgumentException> (async () => await client.Inbox.AddAccessRightsAsync (string.Empty, rights)); Assert.Throws<ArgumentNullException> (async () => await client.Inbox.AddAccessRightsAsync ("smith", null)); Assert.Throws<ArgumentException> (async () => await client.Inbox.AddAccessRightsAsync ("smith", empty)); await client.Inbox.AddAccessRightsAsync ("smith", rights); // SETACL INBOX smith -lrswida Assert.Throws<ArgumentNullException> (() => client.Inbox.RemoveAccessRights (null, rights)); //Assert.Throws<ArgumentException> (() => client.Inbox.RemoveAccessRights (string.Empty, rights)); Assert.Throws<ArgumentNullException> (() => client.Inbox.RemoveAccessRights ("smith", null)); Assert.Throws<ArgumentException> (() => client.Inbox.RemoveAccessRights ("smith", empty)); Assert.Throws<ArgumentNullException> (async () => await client.Inbox.RemoveAccessRightsAsync (null, rights)); //Assert.Throws<ArgumentException> (async () => await client.Inbox.RemoveAccessRightsAsync (string.Empty, rights)); Assert.Throws<ArgumentNullException> (async () => await client.Inbox.RemoveAccessRightsAsync ("smith", null)); Assert.Throws<ArgumentException> (async () => await client.Inbox.RemoveAccessRightsAsync ("smith", empty)); await client.Inbox.RemoveAccessRightsAsync ("smith", rights); // SETACL INBOX smith lrswida Assert.Throws<ArgumentNullException> (() => client.Inbox.SetAccessRights (null, rights)); //Assert.Throws<ArgumentException> (() => client.Inbox.SetAccessRights (string.Empty, rights)); Assert.Throws<ArgumentNullException> (() => client.Inbox.SetAccessRights ("smith", null)); Assert.Throws<ArgumentNullException> (async () => await client.Inbox.SetAccessRightsAsync (null, rights)); //Assert.Throws<ArgumentException> (async () => await client.Inbox.SetAccessRightsAsync (string.Empty, rights)); Assert.Throws<ArgumentNullException> (async () => await client.Inbox.SetAccessRightsAsync ("smith", null)); await client.Inbox.SetAccessRightsAsync ("smith", rights); // DELETEACL INBOX smith Assert.Throws<ArgumentNullException> (() => client.Inbox.RemoveAccess (null)); //Assert.Throws<ArgumentException> (() => client.Inbox.RemoveAccess (string.Empty)); Assert.Throws<ArgumentNullException> (async () => await client.Inbox.RemoveAccessAsync (null)); //Assert.Throws<ArgumentException> (async () => await client.Inbox.RemoveAccessAsync (string.Empty)); await client.Inbox.RemoveAccessAsync ("smith"); await client.DisconnectAsync (false); } }
private static void RefreshInbox(MailReceiverSettings settings) { using (var client = new ImapClient()) { try { IMailFolder inbox = AuthenticateAndgetInbox(settings, client); BinarySearchQuery query = SearchQuery.DeliveredAfter(timeStamp) .And(SearchQuery.SubjectContains(text: "Work Order - 'SF Recover Equipment'")); IList<UniqueId> indexes = inbox.Search(query); ErrorQueue.Enqueue("Total Mails Read: " + indexes.Count); foreach (var index in indexes) { MimeMessage msg = inbox.GetMessage(index); if (msg.Date.UtcDateTime >= timeStamp) { foreach (var ma in msg.Attachments) { string file = virtualpath + ma.ContentType.Name; RemoveIfAboveTen(virtualpath); if (!File.Exists(file)) { DownloadAttachement((MimePart)ma, file); } var details = PdfExtractor.ExtractInfoWithPolicy(file); Send(details); UpdateSheet(details); } } } client.Disconnect(quit: true); } catch (Exception ex) { ErrorQueue.Enqueue(ex.Message); } } }
private static IMailFolder AuthenticateAndgetInbox(MailReceiverSettings settings, ImapClient client) { client.Connect(settings.Server, port: settings.Port, useSsl: settings.UseSsl); client.AuthenticationMechanisms.Remove(item: "XOAUTH2"); client.Authenticate(settings.User, settings.Password); IMailFolder inbox = client.Inbox; inbox.Open(FolderAccess.ReadOnly); return inbox; }
/// <summary> /// Initializes a new instance of the <see cref="IdleState"/> class. /// </summary> /// <param name="client">The IMAP client.</param> /// <param name="doneToken">The user-controlled 'done' token.</param> /// <param name="cancellationToken">The brute-force cancellation token.</param> public IdleState (ImapClient client, CancellationToken doneToken, CancellationToken cancellationToken = default (CancellationToken)) { CancellationToken = cancellationToken; DoneToken = doneToken; Client = client; // When the user hits a key, end the current timeout as well doneToken.Register (CancelTimeout); }
private bool PrintMails(bool login_only = false) { try { this.mailclient.Connect(this.mail_server_name, this.mail_server_port_number, this.mail_server_ssl); this.mailclient.Authenticate(this.mail_server_username, this.mail_server_password); if (!login_only) { /* * imapclient.Inbox.Open(MailKit.FolderAccess.ReadOnly); * var uids = imapclient.Inbox.Search(MailKit.Search.SearchQuery.All); * foreach(var uid in uids) * { * //var message = imapclient.Inbox.GetMessage(uid); * //Console.Write(message.Body); * } */ if (this.mailclient is MailKit.Net.Pop3.Pop3Client) { MailKit.Net.Pop3.Pop3Client popclient = (MailKit.Net.Pop3.Pop3Client) this.mailclient; for (int i = 0; i < popclient.Count; i++) { var message = popclient.GetMessage(i); if (this.mail_from_condition != null && this.mail_from_condition != "") { bool contains_addr = false; foreach (var addr in message.From) { if (addr is MimeKit.MailboxAddress) { var mailbox_addr = (MimeKit.MailboxAddress)addr; if (mailbox_addr.Address == this.mail_from_condition) { contains_addr = true; break; } } } if (!contains_addr) { continue; } } if (this.mail_subject_condition != null && this.mail_subject_condition != "") { if (!message.Subject.Contains(this.mail_subject_condition)) { continue; } } if (this.mail_body_condition != null && this.mail_body_condition != "") { if (!message.TextBody.Contains(this.mail_body_condition)) { continue; } } if (SendStringToStarPRNT(message.TextBody)) { popclient.DeleteMessage(i); } } } else if (this.mailclient is MailKit.Net.Imap.ImapClient) { MailKit.Net.Imap.ImapClient imapclient = (MailKit.Net.Imap.ImapClient) this.mailclient; MailKit.Search.TextSearchQuery from_query = null, subject_query = null, body_query = null; if (this.mail_from_condition != null && this.mail_from_condition != "") { from_query = MailKit.Search.SearchQuery.FromContains(this.mail_from_condition); } if (this.mail_subject_condition != null && this.mail_subject_condition != "") { subject_query = MailKit.Search.SearchQuery.SubjectContains(this.mail_subject_condition); } if (this.mail_body_condition != null && this.mail_body_condition != "") { body_query = MailKit.Search.SearchQuery.BodyContains(this.mail_body_condition); } MailKit.Search.SearchQuery query = MailKit.Search.SearchQuery.All; if (from_query != null) { Trace.WriteLine(from_query.Term + " " + from_query.Text); query = query.And(from_query); } if (subject_query != null) { Trace.WriteLine(subject_query.Term + " " + subject_query.Text); query = query.And(subject_query); } if (body_query != null) { Trace.WriteLine(body_query.Term + " " + body_query.Text); query = query.And(body_query); } imapclient.Inbox.Open(MailKit.FolderAccess.ReadWrite); var uids = imapclient.Inbox.Search(query); var deleted_uids = new List <MailKit.UniqueId>(); foreach (MailKit.UniqueId uid in uids) { var message = imapclient.Inbox.GetMessage(uid); if (SendStringToStarPRNT(message.TextBody)) { imapclient.Inbox.AddFlags(uid, MailKit.MessageFlags.Deleted, true); deleted_uids.Add(uid); } } if (deleted_uids.Count > 0) { imapclient.Inbox.Expunge(deleted_uids); } } } } catch (Exception e) when(e is System.IO.IOException || e is System.Net.Sockets.SocketException) { Trace.TraceError("メールサーバーとの通信に失敗しました。\n" + e.ToString()); if (this.print_on_error) { SendStringToStarPRNT("【エラー】" + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") + " " + "メールサーバーとの通信に失敗しました。自動印刷ができない状態です。ネットワークかメールサーバーに異常が発生している可能性があります。このエラーが出続ける場合は管理者にお問い合わせ下さい。\n" + e.GetType().ToString() + ": " + e.Message + "\n\n\n\n\n\n\n\n\n\n"); } } catch (MailKit.Security.AuthenticationException e) { Trace.TraceError("メールサーバーへのログインに失敗しました。ユーザー名かパスワードが間違っているか、サーバー側でアカウントロックされた可能性があります。\n" + e.ToString()); if (this.print_on_error) { SendStringToStarPRNT("【エラー】" + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") + " " + "メールサーバーへのログインに失敗しました。自動印刷ができない状態です。管理者にお問い合わせ下さい。" + e.Message + "\n\n\n\n\n\n\n\n\n\n"); } } catch (Exception e) { Trace.TraceError("メール処理でエラーが発生しました。\n" + e.ToString()); if (this.print_on_error) { SendStringToStarPRNT("【エラー】" + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") + " " + "自動印刷ができない状態です。管理者にお問い合わせ下さい。\n" + e.GetType().ToString() + ": " + e.Message + "\n\n\n\n\n\n\n\n\n\n"); } } finally { if (this.mailclient.IsConnected) { this.mailclient.Disconnect(true); } } return(true); }
public async void TestImapClientFeatures () { var commands = new List<ImapReplayCommand> (); commands.Add (new ImapReplayCommand ("", "gmail.greeting.txt")); commands.Add (new ImapReplayCommand ("A00000000 CAPABILITY\r\n", "gmail.capability.txt")); commands.Add (new ImapReplayCommand ("A00000001 AUTHENTICATE PLAIN AHVzZXJuYW1lAHBhc3N3b3Jk\r\n", "gmail.authenticate.txt")); commands.Add (new ImapReplayCommand ("A00000002 NAMESPACE\r\n", "gmail.namespace.txt")); commands.Add (new ImapReplayCommand ("A00000003 LIST \"\" \"INBOX\"\r\n", "gmail.list-inbox.txt")); commands.Add (new ImapReplayCommand ("A00000004 XLIST \"\" \"*\"\r\n", "gmail.xlist.txt")); commands.Add (new ImapReplayCommand ("A00000005 ID (\"name\" \"MailKit\" \"version\" \"1.0\" \"vendor\" \"Xamarin Inc.\")\r\n", "common.id.txt")); commands.Add (new ImapReplayCommand ("A00000006 GETQUOTAROOT INBOX\r\n", "common.getquota.txt")); commands.Add (new ImapReplayCommand ("A00000007 SETQUOTA \"\" (MESSAGE 1000000 STORAGE 5242880)\r\n", "common.setquota.txt")); using (var client = new ImapClient ()) { try { client.ReplayConnect ("localhost", new ImapReplayStream (commands, false)); } catch (Exception ex) { Assert.Fail ("Did not expect an exception in Connect: {0}", ex); } Assert.IsTrue (client.IsConnected, "Client failed to connect."); Assert.IsFalse (client.IsSecure, "IsSecure should be false."); Assert.AreEqual (GMailInitialCapabilities, client.Capabilities); Assert.AreEqual (5, client.AuthenticationMechanisms.Count); Assert.IsTrue (client.AuthenticationMechanisms.Contains ("XOAUTH"), "Expected SASL XOAUTH auth mechanism"); Assert.IsTrue (client.AuthenticationMechanisms.Contains ("XOAUTH2"), "Expected SASL XOAUTH2 auth mechanism"); Assert.IsTrue (client.AuthenticationMechanisms.Contains ("OAUTHBEARER"), "Expected SASL OAUTHBEARER auth mechanism"); Assert.IsTrue (client.AuthenticationMechanisms.Contains ("PLAIN"), "Expected SASL PLAIN auth mechanism"); Assert.IsTrue (client.AuthenticationMechanisms.Contains ("PLAIN-CLIENTTOKEN"), "Expected SASL PLAIN-CLIENTTOKEN auth mechanism"); Assert.AreEqual (100000, client.Timeout, "Timeout"); client.Timeout *= 2; // Note: Do not try XOAUTH2 client.AuthenticationMechanisms.Remove ("XOAUTH2"); try { await client.AuthenticateAsync (new NetworkCredential ("username", "password")); } catch (Exception ex) { Assert.Fail ("Did not expect an exception in Authenticate: {0}", ex); } Assert.AreEqual (GMailAuthenticatedCapabilities, client.Capabilities); Assert.IsTrue (client.SupportsQuotas, "SupportsQuotas"); var implementation = new ImapImplementation { Name = "MailKit", Version = "1.0", Vendor = "Xamarin Inc." }; implementation = await client.IdentifyAsync (implementation); Assert.IsNotNull (implementation, "Expected a non-null ID response."); Assert.AreEqual ("GImap", implementation.Name); Assert.AreEqual ("Google, Inc.", implementation.Vendor); Assert.AreEqual ("http://support.google.com/mail", implementation.SupportUrl); Assert.AreEqual ("gmail_imap_150623.03_p1", implementation.Version); Assert.AreEqual ("127.0.0.1", implementation.Properties["remote-host"]); var personal = client.GetFolder (client.PersonalNamespaces[0]); var inbox = client.Inbox; Assert.IsNotNull (inbox, "Expected non-null Inbox folder."); Assert.AreEqual (FolderAttributes.Inbox | FolderAttributes.HasNoChildren, inbox.Attributes, "Expected Inbox attributes to be \\HasNoChildren."); var quota = await inbox.GetQuotaAsync (); Assert.IsNotNull (quota, "Expected a non-null GETQUOTAROOT response."); Assert.AreEqual (personal.FullName, quota.QuotaRoot.FullName); Assert.AreEqual (personal, quota.QuotaRoot); Assert.AreEqual (3783, quota.CurrentStorageSize.Value); Assert.AreEqual (15728640, quota.StorageLimit.Value); Assert.IsFalse (quota.CurrentMessageCount.HasValue); Assert.IsFalse (quota.MessageLimit.HasValue); quota = await personal.SetQuotaAsync (1000000, 5242880); Assert.IsNotNull (quota, "Expected non-null SETQUOTA response."); Assert.AreEqual (1107, quota.CurrentMessageCount.Value); Assert.AreEqual (3783, quota.CurrentStorageSize.Value); Assert.AreEqual (1000000, quota.MessageLimit.Value); Assert.AreEqual (5242880, quota.StorageLimit.Value); await client.DisconnectAsync (false); } }
public async void TestExtractingPrecisePangolinAttachment () { var commands = new List<ImapReplayCommand> (); commands.Add (new ImapReplayCommand ("", "gmail.greeting.txt")); commands.Add (new ImapReplayCommand ("A00000000 CAPABILITY\r\n", "gmail.capability.txt")); commands.Add (new ImapReplayCommand ("A00000001 AUTHENTICATE PLAIN AHVzZXJuYW1lAHBhc3N3b3Jk\r\n", "gmail.authenticate.txt")); commands.Add (new ImapReplayCommand ("A00000002 NAMESPACE\r\n", "gmail.namespace.txt")); commands.Add (new ImapReplayCommand ("A00000003 LIST \"\" \"INBOX\"\r\n", "gmail.list-inbox.txt")); commands.Add (new ImapReplayCommand ("A00000004 XLIST \"\" \"*\"\r\n", "gmail.xlist.txt")); commands.Add (new ImapReplayCommand ("A00000005 LIST \"\" \"%\"\r\n", "gmail.list-personal.txt")); commands.Add (new ImapReplayCommand ("A00000006 EXAMINE INBOX (CONDSTORE)\r\n", "gmail.examine-inbox.txt")); commands.Add (new ImapReplayCommand ("A00000007 FETCH 270 (BODY.PEEK[])\r\n", "gmail.precise-pangolin-message.txt")); using (var client = new ImapClient ()) { try { client.ReplayConnect ("localhost", new ImapReplayStream (commands, false)); } catch (Exception ex) { Assert.Fail ("Did not expect an exception in Connect: {0}", ex); } Assert.IsTrue (client.IsConnected, "Client failed to connect."); Assert.AreEqual (GMailInitialCapabilities, client.Capabilities); Assert.AreEqual (5, client.AuthenticationMechanisms.Count); Assert.IsTrue (client.AuthenticationMechanisms.Contains ("XOAUTH"), "Expected SASL XOAUTH auth mechanism"); Assert.IsTrue (client.AuthenticationMechanisms.Contains ("XOAUTH2"), "Expected SASL XOAUTH2 auth mechanism"); Assert.IsTrue (client.AuthenticationMechanisms.Contains ("OAUTHBEARER"), "Expected SASL OAUTHBEARER auth mechanism"); Assert.IsTrue (client.AuthenticationMechanisms.Contains ("PLAIN"), "Expected SASL PLAIN auth mechanism"); Assert.IsTrue (client.AuthenticationMechanisms.Contains ("PLAIN-CLIENTTOKEN"), "Expected SASL PLAIN-CLIENTTOKEN auth mechanism"); // Note: Do not try XOAUTH2 client.AuthenticationMechanisms.Remove ("XOAUTH2"); try { await client.AuthenticateAsync ("username", "password"); } catch (Exception ex) { Assert.Fail ("Did not expect an exception in Authenticate: {0}", ex); } Assert.AreEqual (GMailAuthenticatedCapabilities, client.Capabilities); var inbox = client.Inbox; Assert.IsNotNull (inbox, "Expected non-null Inbox folder."); Assert.AreEqual (FolderAttributes.Inbox | FolderAttributes.HasNoChildren, inbox.Attributes, "Expected Inbox attributes to be \\HasNoChildren."); foreach (var special in Enum.GetValues (typeof (SpecialFolder)).OfType<SpecialFolder> ()) { var folder = client.GetFolder (special); if (special != SpecialFolder.Archive) { var expected = GetSpecialFolderAttribute (special) | FolderAttributes.HasNoChildren; Assert.IsNotNull (folder, "Expected non-null {0} folder.", special); Assert.AreEqual (expected, folder.Attributes, "Expected {0} attributes to be \\HasNoChildren.", special); } else { Assert.IsNull (folder, "Expected null {0} folder.", special); } } // disable LIST-EXTENDED client.Capabilities &= ~ImapCapabilities.ListExtended; var personal = client.GetFolder (client.PersonalNamespaces[0]); var folders = (await personal.GetSubfoldersAsync ()).ToList (); Assert.AreEqual (client.Inbox, folders[0], "Expected the first folder to be the Inbox."); Assert.AreEqual ("[Gmail]", folders[1].FullName, "Expected the second folder to be [Gmail]."); Assert.AreEqual (FolderAttributes.NoSelect | FolderAttributes.HasChildren, folders[1].Attributes, "Expected [Gmail] folder to be \\Noselect \\HasChildren."); await client.Inbox.OpenAsync (FolderAccess.ReadOnly); var message = await client.Inbox.GetMessageAsync (269); using (var jpeg = new MemoryStream ()) { var attachment = message.Attachments.OfType<MimePart> ().FirstOrDefault (); attachment.ContentObject.DecodeTo (jpeg); jpeg.Position = 0; using (var md5 = new MD5CryptoServiceProvider ()) { var md5sum = HexEncode (md5.ComputeHash (jpeg)); Assert.AreEqual ("167a46aa81e881da2ea8a840727384d3", md5sum, "MD5 checksums do not match."); } } await client.DisconnectAsync (false); } }
public async void TestMetadata () { var commands = new List<ImapReplayCommand> (); commands.Add (new ImapReplayCommand ("", "gmail.greeting.txt")); commands.Add (new ImapReplayCommand ("A00000000 CAPABILITY\r\n", "metadata.capability.txt")); commands.Add (new ImapReplayCommand ("A00000001 AUTHENTICATE PLAIN AHVzZXJuYW1lAHBhc3N3b3Jk\r\n", "metadata.authenticate.txt")); commands.Add (new ImapReplayCommand ("A00000002 NAMESPACE\r\n", "gmail.namespace.txt")); commands.Add (new ImapReplayCommand ("A00000003 LIST \"\" \"INBOX\"\r\n", "gmail.list-inbox.txt")); commands.Add (new ImapReplayCommand ("A00000004 XLIST \"\" \"*\"\r\n", "gmail.xlist.txt")); commands.Add (new ImapReplayCommand ("A00000005 GETMETADATA \"\" /private/comment\r\n", "metadata.getmetadata.txt")); commands.Add (new ImapReplayCommand ("A00000006 GETMETADATA \"\" (MAXSIZE 1024 DEPTH infinity) (/private)\r\n", "metadata.getmetadata-options.txt")); commands.Add (new ImapReplayCommand ("A00000007 GETMETADATA \"\" /private/comment /shared/comment\r\n", "metadata.getmetadata-multi.txt")); commands.Add (new ImapReplayCommand ("A00000008 SETMETADATA \"\" (/private/comment \"this is a comment\")\r\n", "metadata.setmetadata-noprivate.txt")); commands.Add (new ImapReplayCommand ("A00000009 SETMETADATA \"\" (/private/comment \"this comment is too long!\")\r\n", "metadata.setmetadata-maxsize.txt")); commands.Add (new ImapReplayCommand ("A00000010 SETMETADATA \"\" (/private/comment \"this is a private comment\" /shared/comment \"this is a shared comment\")\r\n", "metadata.setmetadata-toomany.txt")); commands.Add (new ImapReplayCommand ("A00000011 SETMETADATA \"\" (/private/comment NIL)\r\n", ImapReplayCommandResponse.OK)); commands.Add (new ImapReplayCommand ("A00000012 GETMETADATA INBOX /private/comment\r\n", "metadata.inbox-getmetadata.txt")); commands.Add (new ImapReplayCommand ("A00000013 GETMETADATA INBOX (MAXSIZE 1024 DEPTH infinity) (/private)\r\n", "metadata.inbox-getmetadata-options.txt")); commands.Add (new ImapReplayCommand ("A00000014 GETMETADATA INBOX /private/comment /shared/comment\r\n", "metadata.inbox-getmetadata-multi.txt")); commands.Add (new ImapReplayCommand ("A00000015 SETMETADATA INBOX (/private/comment \"this is a comment\")\r\n", "metadata.inbox-setmetadata-noprivate.txt")); commands.Add (new ImapReplayCommand ("A00000016 SETMETADATA INBOX (/private/comment \"this comment is too long!\")\r\n", "metadata.inbox-setmetadata-maxsize.txt")); commands.Add (new ImapReplayCommand ("A00000017 SETMETADATA INBOX (/private/comment \"this is a private comment\" /shared/comment \"this is a shared comment\")\r\n", "metadata.inbox-setmetadata-toomany.txt")); commands.Add (new ImapReplayCommand ("A00000018 SETMETADATA INBOX (/private/comment NIL)\r\n", ImapReplayCommandResponse.OK)); using (var client = new ImapClient ()) { MetadataCollection metadata; MetadataOptions options; try { client.ReplayConnect ("localhost", new ImapReplayStream (commands, false)); } catch (Exception ex) { Assert.Fail ("Did not expect an exception in Connect: {0}", ex); } Assert.IsTrue (client.IsConnected, "Client failed to connect."); Assert.AreEqual (MetadataInitialCapabilities, client.Capabilities); Assert.AreEqual (4, client.AuthenticationMechanisms.Count); Assert.IsTrue (client.AuthenticationMechanisms.Contains ("XOAUTH"), "Expected SASL XOAUTH auth mechanism"); Assert.IsTrue (client.AuthenticationMechanisms.Contains ("XOAUTH2"), "Expected SASL XOAUTH2 auth mechanism"); Assert.IsTrue (client.AuthenticationMechanisms.Contains ("PLAIN"), "Expected SASL PLAIN auth mechanism"); Assert.IsTrue (client.AuthenticationMechanisms.Contains ("PLAIN-CLIENTTOKEN"), "Expected SASL PLAIN-CLIENTTOKEN auth mechanism"); // Note: Do not try XOAUTH2 client.AuthenticationMechanisms.Remove ("XOAUTH2"); try { await client.AuthenticateAsync ("username", "password"); } catch (Exception ex) { Assert.Fail ("Did not expect an exception in Authenticate: {0}", ex); } Assert.AreEqual (MetadataAuthenticatedCapabilities, client.Capabilities); var inbox = client.Inbox; Assert.IsNotNull (inbox, "Expected non-null Inbox folder."); Assert.AreEqual (FolderAttributes.Inbox | FolderAttributes.HasNoChildren, inbox.Attributes, "Expected Inbox attributes to be \\HasNoChildren."); foreach (var special in Enum.GetValues (typeof (SpecialFolder)).OfType<SpecialFolder> ()) { var folder = client.GetFolder (special); if (special != SpecialFolder.Archive) { var expected = GetSpecialFolderAttribute (special) | FolderAttributes.HasNoChildren; Assert.IsNotNull (folder, "Expected non-null {0} folder.", special); Assert.AreEqual (expected, folder.Attributes, "Expected {0} attributes to be \\HasNoChildren.", special); } else { Assert.IsNull (folder, "Expected null {0} folder.", special); } } // GETMETADATA Assert.AreEqual ("this is a comment", await client.GetMetadataAsync (MetadataTag.PrivateComment), "The shared comment does not match."); options = new MetadataOptions { Depth = int.MaxValue, MaxSize = 1024 }; metadata = await client.GetMetadataAsync (options, new [] { new MetadataTag ("/private") }); Assert.AreEqual (1, metadata.Count, "Expected 1 metadata value."); Assert.AreEqual (MetadataTag.PrivateComment.Id, metadata[0].Tag.Id, "Metadata tag did not match."); Assert.AreEqual ("this is a private comment", metadata[0].Value, "Metadata value did not match."); Assert.AreEqual (2199, options.LongEntries, "LongEntries does not match."); metadata = await client.GetMetadataAsync (new [] { MetadataTag.PrivateComment, MetadataTag.SharedComment }); Assert.AreEqual (2, metadata.Count, "Expected 2 metadata values."); Assert.AreEqual (MetadataTag.PrivateComment.Id, metadata[0].Tag.Id, "First metadata tag did not match."); Assert.AreEqual (MetadataTag.SharedComment.Id, metadata[1].Tag.Id, "Second metadata tag did not match."); Assert.AreEqual ("this is a private comment", metadata[0].Value, "First metadata value did not match."); Assert.AreEqual ("this is a shared comment", metadata[1].Value, "Second metadata value did not match."); // SETMETADATA Assert.Throws<ImapCommandException> (async () => await client.SetMetadataAsync (new MetadataCollection (new [] { new Metadata (MetadataTag.PrivateComment, "this is a comment") })), "Expected NOPRIVATE RESP-CODE."); Assert.Throws<ImapCommandException> (async () => await client.SetMetadataAsync (new MetadataCollection (new [] { new Metadata (MetadataTag.PrivateComment, "this comment is too long!") })), "Expected MAXSIZE RESP-CODE."); Assert.Throws<ImapCommandException> (async () => await client.SetMetadataAsync (new MetadataCollection (new [] { new Metadata (MetadataTag.PrivateComment, "this is a private comment"), new Metadata (MetadataTag.SharedComment, "this is a shared comment"), })), "Expected TOOMANY RESP-CODE."); await client.SetMetadataAsync (new MetadataCollection (new [] { new Metadata (MetadataTag.PrivateComment, null) })); // GETMETADATA folder Assert.AreEqual ("this is a comment", await inbox.GetMetadataAsync (MetadataTag.PrivateComment), "The shared comment does not match."); options = new MetadataOptions { Depth = int.MaxValue, MaxSize = 1024 }; metadata = await inbox.GetMetadataAsync (options, new [] { new MetadataTag ("/private") }); Assert.AreEqual (1, metadata.Count, "Expected 1 metadata value."); Assert.AreEqual (MetadataTag.PrivateComment.Id, metadata[0].Tag.Id, "Metadata tag did not match."); Assert.AreEqual ("this is a private comment", metadata[0].Value, "Metadata value did not match."); Assert.AreEqual (2199, options.LongEntries, "LongEntries does not match."); metadata = await inbox.GetMetadataAsync (new [] { MetadataTag.PrivateComment, MetadataTag.SharedComment }); Assert.AreEqual (2, metadata.Count, "Expected 2 metadata values."); Assert.AreEqual (MetadataTag.PrivateComment.Id, metadata[0].Tag.Id, "First metadata tag did not match."); Assert.AreEqual (MetadataTag.SharedComment.Id, metadata[1].Tag.Id, "Second metadata tag did not match."); Assert.AreEqual ("this is a private comment", metadata[0].Value, "First metadata value did not match."); Assert.AreEqual ("this is a shared comment", metadata[1].Value, "Second metadata value did not match."); // SETMETADATA folder Assert.Throws<ImapCommandException> (async () => await inbox.SetMetadataAsync (new MetadataCollection (new [] { new Metadata (MetadataTag.PrivateComment, "this is a comment") })), "Expected NOPRIVATE RESP-CODE."); Assert.Throws<ImapCommandException> (async () => await inbox.SetMetadataAsync (new MetadataCollection (new [] { new Metadata (MetadataTag.PrivateComment, "this comment is too long!") })), "Expected MAXSIZE RESP-CODE."); Assert.Throws<ImapCommandException> (async () => await inbox.SetMetadataAsync (new MetadataCollection (new [] { new Metadata (MetadataTag.PrivateComment, "this is a private comment"), new Metadata (MetadataTag.SharedComment, "this is a shared comment"), })), "Expected TOOMANY RESP-CODE."); await inbox.SetMetadataAsync (new MetadataCollection (new [] { new Metadata (MetadataTag.PrivateComment, null) })); await client.DisconnectAsync (false); } }
public bool CheckAndSend(string user, string pwd, string imapUri, string smtpUri) { using (var client = new ImapClient()) { var credentials = new NetworkCredential(user, pwd); var uriObj = new Uri(imapUri); using (var cancel = new CancellationTokenSource()) { client.Connect(uriObj, cancel.Token); // Note: since we don't have an OAuth2 token, disable // the XOAUTH2 authentication mechanism. client.AuthenticationMechanisms.Remove("XOAUTH"); client.Authenticate(credentials, cancel.Token); // The Inbox folder is always available on all IMAP servers... var draftBox = client.GetFolder(SpecialFolder.Drafts); if (draftBox == null) draftBox = client.GetFolder("Drafts"); draftBox.Open(FolderAccess.ReadWrite, cancel.Token); Console.WriteLine("Total messages: {0}", draftBox.Count); Console.WriteLine("Recent messages: {0}", draftBox.Recent); SearchQuery query = SearchQuery.All; //query = SearchQuery.ToContains("*****@*****.**"); UniqueId[] uids = draftBox.Search(query); int count = 0; for (int i = uids.Length - 1; i >= 0; i--) { UniqueId uid = uids[i]; var message = draftBox.GetMessage(uid, cancel.Token); DateTimeOffset dto1 = DateTimeOffset.UtcNow; if (dto1.AddHours(-12) >= message.Date) break; Console.WriteLine("Subject: {0}", message.Subject); Console.WriteLine("Date: {0}", message.Date); if (message.Bcc.Count > 0) { foreach (InternetAddress addr in message.Bcc) { string addrString = addr.Name; if (string.IsNullOrEmpty(addrString)) { addrString = addr.ToString(); if (addrString.Contains("<")) addrString = addrString.Substring(addrString.LastIndexOf("<") + 1, addrString.LastIndexOf(">") - addrString.LastIndexOf("<") -1); } //if (addr.Name.Contains("send@")) if (addrString.Contains("send@")) { //string time = addr.Name.Replace("send@", ""); string time = addrString.Replace("send@", ""); Console.WriteLine("Scheduled mail is found. Scheduled time: {0}", time); count++; string[] parts = time.Split(new char[] { '.' }); if (parts.Length != 2) continue; int hour = 0; int minute = 0; DateTimeOffset dt2 = message.Date; DateTimeOffset offset2; if (int.TryParse(parts[0], out hour)) { if (int.TryParse(parts[1], out minute)) { dt2 = dt2.AddHours(hour - dt2.Hour); dt2 = dt2.AddMinutes(minute - dt2.Minute); dt2 = dt2.AddSeconds(0 - dt2.Second); if (dt2 <= DateTimeOffset.UtcNow && dt2 >= DateTimeOffset.UtcNow.AddHours(-1)) { message.Bcc.Remove(addr); this.Send(message, user, pwd, smtpUri); for (int retry = 1, maxRetry = 10; retry <= maxRetry; i++) { Thread.Sleep(20 * 1000); try { Console.WriteLine("{0} in {1} trys to delete draft", retry, maxRetry); draftBox.SetFlags(new UniqueId[] { uid }, MessageFlags.Deleted, true); draftBox.Expunge(); } catch (IOException e) { retry++; continue; } break; } Console.WriteLine("Scheduled mail is sent. Subject: {0}", message.Subject); break; } else Console.WriteLine("Scheduled time is not satisfied. Mail is not sent. Subject: {0}", message.Subject); } } } } } } Console.WriteLine("Total count for scheduled mails: {0}", count); client.Disconnect(true, cancel.Token); } } return false; }
public RC getMessages(bool bUsePop3 = false, int iPortToUse = 0) { try { if (bUsePop3) { using (var client = new Pop3Client ()) { if (iPortToUse == 0) { client.Connect (m_EmailServiceDescription.Pop3Url, 110, true); } else { client.Connect (m_EmailServiceDescription.Pop3Url, iPortToUse, true); } // Note: since we don't have an OAuth2 token, disable // the XOAUTH2 authentication mechanism. client.AuthenticationMechanisms.Remove ("XOAUTH2"); client.Authenticate (m_AuthInfo.m_sId, m_AuthInfo.m_sPassword); for (int i = 0; i < client.Count; i++) { var message = client.GetMessage (i); string sPlainBody = m_OpenPgpCrypter.DecryptPgpString(message.GetTextBody(MimeKit.Text.TextFormat.Text)); m_ConversationManager.addMessage (m_sProtocol, message.Subject + " " + sPlainBody, message.Sender.Address, m_AuthInfo.m_sId); } client.Disconnect(true); return RC.RC_OK; } } else //use IMAP { using (var client = new ImapClient ()) { if (iPortToUse == 0) { client.Connect (m_EmailServiceDescription.ImapUrl, 993, true); } else { client.Connect (m_EmailServiceDescription.ImapUrl, iPortToUse, true); } // Note: since we don't have an OAuth2 token, disable // the XOAUTH2 authentication mechanism. client.AuthenticationMechanisms.Remove ("XOAUTH2"); client.Authenticate (m_AuthInfo.m_sId, m_AuthInfo.m_sPassword); // The Inbox folder is always available on all IMAP servers... var inbox = client.Inbox; inbox.Open (FolderAccess.ReadOnly); //TODO: delete writeline Console.WriteLine ("Total messages: {0}", inbox.Count); Console.WriteLine ("Recent messages: {0}", inbox.Recent); for (int i = 0; i < inbox.Count; i++) { var message = inbox.GetMessage (i); m_ConversationManager.addMessage (m_sProtocol, message.Subject + message.Body, message.Sender.Address, m_AuthInfo.m_sId); } client.Disconnect (true); return RC.RC_OK; } } } catch(Exception e) { m_Logger.log (ELogLevel.LVL_WARNING, e.Message, m_sModuleName); return RC.RC_INBOX_NOT_AVAILABLE; } }
public void TestImapClientGMail() { var commands = new List<ImapReplayCommand> (); commands.Add (new ImapReplayCommand ("", "gmail.greeting.txt")); commands.Add (new ImapReplayCommand ("A00000000 CAPABILITY\r\n", "gmail.capability.txt")); commands.Add (new ImapReplayCommand ("A00000001 AUTHENTICATE PLAIN AHVzZXJuYW1lAHBhc3N3b3Jk\r\n", "gmail.authenticate.txt")); commands.Add (new ImapReplayCommand ("A00000002 NAMESPACE\r\n", "gmail.namespace.txt")); commands.Add (new ImapReplayCommand ("A00000003 LIST \"\" \"INBOX\"\r\n", "gmail.list-inbox.txt")); commands.Add (new ImapReplayCommand ("A00000004 XLIST \"\" \"*\"\r\n", "gmail.xlist.txt")); commands.Add (new ImapReplayCommand ("A00000005 LIST \"\" \"%\"\r\n", "gmail.list-personal.txt")); commands.Add (new ImapReplayCommand ("A00000006 CREATE UnitTests\r\n", "gmail.create-unittests.txt")); commands.Add (new ImapReplayCommand ("A00000007 LIST \"\" UnitTests\r\n", "gmail.list-unittests.txt")); commands.Add (new ImapReplayCommand ("A00000008 SELECT UnitTests (CONDSTORE)\r\n", "gmail.select-unittests.txt")); for (int i = 0; i < 50; i++) { using (var stream = GetResourceStream (string.Format ("common.message.{0}.msg", i))) { var message = MimeMessage.Load (stream); long length = stream.Length; string latin1; stream.Position = 0; using (var reader = new StreamReader (stream, Latin1)) latin1 = reader.ReadToEnd (); var command = string.Format ("A{0:D8} APPEND UnitTests (\\Seen) ", i + 9); command += "{" + length + "}\r\n"; commands.Add (new ImapReplayCommand (command, "gmail.go-ahead.txt")); commands.Add (new ImapReplayCommand (latin1 + "\r\n", string.Format ("gmail.append.{0}.txt", i + 1))); } } commands.Add (new ImapReplayCommand ("A00000059 UID SEARCH RETURN () CHARSET US-ASCII OR TO nsb CC nsb\r\n", "gmail.search.txt")); commands.Add (new ImapReplayCommand ("A00000060 UID FETCH 1:3,5,7:9,11:14,26:29,31,34,41:43,50 (UID FLAGS INTERNALDATE RFC822.SIZE ENVELOPE BODY)\r\n", "gmail.search-summary.txt")); commands.Add (new ImapReplayCommand ("A00000061 UID FETCH 1 (BODY.PEEK[])\r\n", "gmail.fetch.1.txt")); commands.Add (new ImapReplayCommand ("A00000062 UID FETCH 2 (BODY.PEEK[])\r\n", "gmail.fetch.2.txt")); commands.Add (new ImapReplayCommand ("A00000063 UID FETCH 3 (BODY.PEEK[])\r\n", "gmail.fetch.3.txt")); commands.Add (new ImapReplayCommand ("A00000064 UID FETCH 5 (BODY.PEEK[])\r\n", "gmail.fetch.5.txt")); commands.Add (new ImapReplayCommand ("A00000065 UID FETCH 7 (BODY.PEEK[])\r\n", "gmail.fetch.7.txt")); commands.Add (new ImapReplayCommand ("A00000066 UID FETCH 8 (BODY.PEEK[])\r\n", "gmail.fetch.8.txt")); commands.Add (new ImapReplayCommand ("A00000067 UID FETCH 9 (BODY.PEEK[])\r\n", "gmail.fetch.9.txt")); commands.Add (new ImapReplayCommand ("A00000068 UID FETCH 11 (BODY.PEEK[])\r\n", "gmail.fetch.11.txt")); commands.Add (new ImapReplayCommand ("A00000069 UID FETCH 12 (BODY.PEEK[])\r\n", "gmail.fetch.12.txt")); commands.Add (new ImapReplayCommand ("A00000070 UID FETCH 13 (BODY.PEEK[])\r\n", "gmail.fetch.13.txt")); commands.Add (new ImapReplayCommand ("A00000071 UID FETCH 14 (BODY.PEEK[])\r\n", "gmail.fetch.14.txt")); commands.Add (new ImapReplayCommand ("A00000072 UID FETCH 26 (BODY.PEEK[])\r\n", "gmail.fetch.26.txt")); commands.Add (new ImapReplayCommand ("A00000073 UID FETCH 27 (BODY.PEEK[])\r\n", "gmail.fetch.27.txt")); commands.Add (new ImapReplayCommand ("A00000074 UID FETCH 28 (BODY.PEEK[])\r\n", "gmail.fetch.28.txt")); commands.Add (new ImapReplayCommand ("A00000075 UID FETCH 29 (BODY.PEEK[])\r\n", "gmail.fetch.29.txt")); commands.Add (new ImapReplayCommand ("A00000076 UID FETCH 31 (BODY.PEEK[])\r\n", "gmail.fetch.31.txt")); commands.Add (new ImapReplayCommand ("A00000077 UID FETCH 34 (BODY.PEEK[])\r\n", "gmail.fetch.34.txt")); commands.Add (new ImapReplayCommand ("A00000078 UID FETCH 41 (BODY.PEEK[])\r\n", "gmail.fetch.41.txt")); commands.Add (new ImapReplayCommand ("A00000079 UID FETCH 42 (BODY.PEEK[])\r\n", "gmail.fetch.42.txt")); commands.Add (new ImapReplayCommand ("A00000080 UID FETCH 43 (BODY.PEEK[])\r\n", "gmail.fetch.43.txt")); commands.Add (new ImapReplayCommand ("A00000081 UID FETCH 50 (BODY.PEEK[])\r\n", "gmail.fetch.50.txt")); commands.Add (new ImapReplayCommand ("A00000082 UID STORE 1:3,5,7:9,11:14,26:29,31,34,41:43,50 FLAGS (\\Answered \\Seen)\r\n", "gmail.set-flags.txt")); commands.Add (new ImapReplayCommand ("A00000083 UID STORE 1:3,5,7:9,11:14,26:29,31,34,41:43,50 -FLAGS.SILENT (\\Answered)\r\n", "gmail.remove-flags.txt")); commands.Add (new ImapReplayCommand ("A00000084 UID STORE 1:3,5,7:9,11:14,26:29,31,34,41:43,50 +FLAGS.SILENT (\\Deleted)\r\n", "gmail.add-flags.txt")); commands.Add (new ImapReplayCommand ("A00000085 UNSELECT\r\n", "gmail.unselect-unittests.txt")); commands.Add (new ImapReplayCommand ("A00000086 DELETE UnitTests\r\n", "gmail.delete-unittests.txt")); commands.Add (new ImapReplayCommand ("A00000087 LOGOUT\r\n", "gmail.logout.txt")); using (var client = new ImapClient ()) { try { client.ReplayConnect ("localhost", new ImapReplayStream (commands, false), CancellationToken.None); } catch (Exception ex) { Assert.Fail ("Did not expect an exception in Connect: {0}", ex); } Assert.IsTrue (client.IsConnected, "Client failed to connect."); Assert.AreEqual (GMailInitialCapabilities, client.Capabilities); Assert.AreEqual (4, client.AuthenticationMechanisms.Count); Assert.IsTrue (client.AuthenticationMechanisms.Contains ("XOAUTH"), "Expected SASL XOAUTH auth mechanism"); Assert.IsTrue (client.AuthenticationMechanisms.Contains ("XOAUTH2"), "Expected SASL XOAUTH2 auth mechanism"); Assert.IsTrue (client.AuthenticationMechanisms.Contains ("PLAIN"), "Expected SASL PLAIN auth mechanism"); Assert.IsTrue (client.AuthenticationMechanisms.Contains ("PLAIN-CLIENTTOKEN"), "Expected SASL PLAIN-CLIENTTOKEN auth mechanism"); try { var credentials = new NetworkCredential ("username", "password"); // Note: Do not try XOAUTH2 client.AuthenticationMechanisms.Remove ("XOAUTH2"); client.Authenticate (credentials, CancellationToken.None); } catch (Exception ex) { Assert.Fail ("Did not expect an exception in Authenticate: {0}", ex); } Assert.AreEqual (GMailAuthenticatedCapabilities, client.Capabilities); var inbox = client.Inbox; Assert.IsNotNull (inbox, "Expected non-null Inbox folder."); Assert.AreEqual (FolderAttributes.HasNoChildren, inbox.Attributes, "Expected Inbox attributes to be \\HasNoChildren."); foreach (var special in Enum.GetValues (typeof (SpecialFolder)).OfType<SpecialFolder> ()) { var folder = client.GetFolder (special); if (special != SpecialFolder.Archive) { var expected = GetSpecialFolderAttribute (special) | FolderAttributes.HasNoChildren; Assert.IsNotNull (folder, "Expected non-null {0} folder.", special); Assert.AreEqual (expected, folder.Attributes, "Expected {0} attributes to be \\HasNoChildren.", special); } else { Assert.IsNull (folder, "Expected null {0} folder.", special); } } var personal = client.GetFolder (client.PersonalNamespaces[0]); var folders = personal.GetSubfolders (false, CancellationToken.None).ToList (); Assert.AreEqual (client.Inbox, folders[0], "Expected the first folder to be the Inbox."); Assert.AreEqual ("[Gmail]", folders[1].FullName, "Expected the second folder to be [Gmail]."); Assert.AreEqual (FolderAttributes.NoSelect | FolderAttributes.HasChildren, folders[1].Attributes, "Expected [Gmail] folder to be \\Noselect \\HasChildren."); var created = personal.Create ("UnitTests", true, CancellationToken.None); created.Open (FolderAccess.ReadWrite, CancellationToken.None); for (int i = 0; i < 50; i++) { using (var stream = GetResourceStream (string.Format ("common.message.{0}.msg", i))) { var message = MimeMessage.Load (stream); created.Append (message, MessageFlags.Seen, CancellationToken.None); } } var query = SearchQuery.ToContains ("nsb").Or (SearchQuery.CcContains ("nsb")); var matches = created.Search (query, CancellationToken.None); const MessageSummaryItems items = MessageSummaryItems.Full | MessageSummaryItems.UniqueId; var summaries = created.Fetch (matches, items, CancellationToken.None); foreach (var summary in summaries) { var message = created.GetMessage (summary.UniqueId, CancellationToken.None); } created.SetFlags (matches, MessageFlags.Seen | MessageFlags.Answered, false, CancellationToken.None); created.RemoveFlags (matches, MessageFlags.Answered, true, CancellationToken.None); created.AddFlags (matches, MessageFlags.Deleted, true, CancellationToken.None); created.Close (false, CancellationToken.None); created.Delete (CancellationToken.None); client.Disconnect (true, CancellationToken.None); } }
public void TestMessageCount() { var commands = new List<ImapReplayCommand> (); commands.Add (new ImapReplayCommand ("", "gmail.greeting.txt")); commands.Add (new ImapReplayCommand ("A00000000 CAPABILITY\r\n", "gmail.capability.txt")); commands.Add (new ImapReplayCommand ("A00000001 AUTHENTICATE PLAIN AHVzZXJuYW1lAHBhc3N3b3Jk\r\n", "gmail.authenticate.txt")); commands.Add (new ImapReplayCommand ("A00000002 NAMESPACE\r\n", "gmail.namespace.txt")); commands.Add (new ImapReplayCommand ("A00000003 LIST \"\" \"INBOX\"\r\n", "gmail.list-inbox.txt")); commands.Add (new ImapReplayCommand ("A00000004 XLIST \"\" \"*\"\r\n", "gmail.xlist.txt")); //INBOX has 1 message present in this test commands.Add (new ImapReplayCommand ("A00000005 EXAMINE INBOX (CONDSTORE)\r\n", "gmail.count.examine.txt")); //next command simulates one expunge + one new message commands.Add (new ImapReplayCommand ("A00000006 NOOP\r\n", "gmail.count.noop.txt")); using (var client = new ImapClient ()) { try { client.ReplayConnect ("localhost", new ImapReplayStream (commands, false), CancellationToken.None); } catch (Exception ex) { Assert.Fail ("Did not expect an exception in Connect: {0}", ex); } Assert.IsTrue (client.IsConnected, "Client failed to connect."); try { var credentials = new NetworkCredential ("username", "password"); // Note: Do not try XOAUTH2 client.AuthenticationMechanisms.Remove ("XOAUTH2"); client.Authenticate (credentials, CancellationToken.None); } catch (Exception ex) { Assert.Fail ("Did not expect an exception in Authenticate: {0}", ex); } var count = -1; client.Inbox.Open(FolderAccess.ReadOnly); client.Inbox.CountChanged += delegate { count = client.Inbox.Count; }; client.NoOp(); Assert.AreEqual(1, count, "Count is not correct"); } }
public async void TestImapClientGetFolders () { var commands = new List<ImapReplayCommand> (); commands.Add (new ImapReplayCommand ("", "gmail.greeting.txt")); commands.Add (new ImapReplayCommand ("A00000000 CAPABILITY\r\n", "gmail.capability.txt")); commands.Add (new ImapReplayCommand ("A00000001 AUTHENTICATE PLAIN AHVzZXJuYW1lAHBhc3N3b3Jk\r\n", "gmail.authenticate.txt")); commands.Add (new ImapReplayCommand ("A00000002 NAMESPACE\r\n", "gmail.namespace.txt")); commands.Add (new ImapReplayCommand ("A00000003 LIST \"\" \"INBOX\"\r\n", "gmail.list-inbox.txt")); commands.Add (new ImapReplayCommand ("A00000004 XLIST \"\" \"*\"\r\n", "gmail.xlist.txt")); commands.Add (new ImapReplayCommand ("A00000005 LIST (SUBSCRIBED) \"\" \"*\" RETURN (CHILDREN STATUS (MESSAGES RECENT UIDNEXT UIDVALIDITY UNSEEN HIGHESTMODSEQ))\r\n", "gmail.list-all.txt")); using (var client = new ImapClient ()) { try { client.ReplayConnect ("localhost", new ImapReplayStream (commands, false)); } catch (Exception ex) { Assert.Fail ("Did not expect an exception in Connect: {0}", ex); } Assert.IsTrue (client.IsConnected, "Client failed to connect."); Assert.AreEqual (GMailInitialCapabilities, client.Capabilities); Assert.AreEqual (5, client.AuthenticationMechanisms.Count); Assert.IsTrue (client.AuthenticationMechanisms.Contains ("XOAUTH"), "Expected SASL XOAUTH auth mechanism"); Assert.IsTrue (client.AuthenticationMechanisms.Contains ("XOAUTH2"), "Expected SASL XOAUTH2 auth mechanism"); Assert.IsTrue (client.AuthenticationMechanisms.Contains ("OAUTHBEARER"), "Expected SASL OAUTHBEARER auth mechanism"); Assert.IsTrue (client.AuthenticationMechanisms.Contains ("PLAIN"), "Expected SASL PLAIN auth mechanism"); Assert.IsTrue (client.AuthenticationMechanisms.Contains ("PLAIN-CLIENTTOKEN"), "Expected SASL PLAIN-CLIENTTOKEN auth mechanism"); // Note: Do not try XOAUTH2 client.AuthenticationMechanisms.Remove ("XOAUTH2"); try { await client.AuthenticateAsync ("username", "password"); } catch (Exception ex) { Assert.Fail ("Did not expect an exception in Authenticate: {0}", ex); } Assert.AreEqual (GMailAuthenticatedCapabilities, client.Capabilities); var all = StatusItems.Count | StatusItems.HighestModSeq | StatusItems.Recent | StatusItems.UidNext | StatusItems.UidValidity | StatusItems.Unread; var folders = (await client.GetFoldersAsync (client.PersonalNamespaces[0], all, true)).ToList (); Assert.AreEqual (9, folders.Count, "Unexpected folder count."); AssertFolder (folders[0], "INBOX", FolderAttributes.HasNoChildren | FolderAttributes.Inbox, true, 41234, 60, 0, 410, 1, 0); AssertFolder (folders[1], "[Gmail]", FolderAttributes.HasChildren | FolderAttributes.NonExistent | FolderAttributes.NoSelect, true, 0, 0, 0, 0, 0, 0); AssertFolder (folders[2], "[Gmail]/All Mail", FolderAttributes.HasNoChildren | FolderAttributes.All, true, 41234, 67, 0, 1210, 11, 3); AssertFolder (folders[3], "[Gmail]/Drafts", FolderAttributes.HasNoChildren | FolderAttributes.Drafts, true, 41234, 0, 0, 1, 6, 0); AssertFolder (folders[4], "[Gmail]/Important", FolderAttributes.HasNoChildren | FolderAttributes.Flagged, true, 41234, 58, 0, 307, 9, 0); AssertFolder (folders[5], "[Gmail]/Sent Mail", FolderAttributes.HasNoChildren | FolderAttributes.Sent, true, 41234, 4, 0, 7, 5, 0); AssertFolder (folders[6], "[Gmail]/Spam", FolderAttributes.HasNoChildren | FolderAttributes.Junk, true, 41234, 0, 0, 1, 3, 0); AssertFolder (folders[7], "[Gmail]/Starred", FolderAttributes.HasNoChildren | FolderAttributes.Flagged, true, 41234, 1, 0, 7, 4, 0); AssertFolder (folders[8], "[Gmail]/Trash", FolderAttributes.HasNoChildren | FolderAttributes.Trash, true, 41234, 0, 0, 1143, 2, 0); AssertFolder (client.Inbox, "INBOX", FolderAttributes.HasNoChildren | FolderAttributes.Inbox, true, 41234, 60, 0, 410, 1, 0); AssertFolder (client.GetFolder (SpecialFolder.All), "[Gmail]/All Mail", FolderAttributes.HasNoChildren | FolderAttributes.All, true, 41234, 67, 0, 1210, 11, 3); AssertFolder (client.GetFolder (SpecialFolder.Drafts), "[Gmail]/Drafts", FolderAttributes.HasNoChildren | FolderAttributes.Drafts, true, 41234, 0, 0, 1, 6, 0); //AssertFolder (client.GetFolder (SpecialFolder.Flagged), "[Gmail]/Important", FolderAttributes.HasNoChildren | FolderAttributes.Flagged, true, 41234, 58, 0, 307, 9, 0); AssertFolder (client.GetFolder (SpecialFolder.Sent), "[Gmail]/Sent Mail", FolderAttributes.HasNoChildren | FolderAttributes.Sent, true, 41234, 4, 0, 7, 5, 0); AssertFolder (client.GetFolder (SpecialFolder.Junk), "[Gmail]/Spam", FolderAttributes.HasNoChildren | FolderAttributes.Junk, true, 41234, 0, 0, 1, 3, 0); AssertFolder (client.GetFolder (SpecialFolder.Flagged), "[Gmail]/Starred", FolderAttributes.HasNoChildren | FolderAttributes.Flagged, true, 41234, 1, 0, 7, 4, 0); AssertFolder (client.GetFolder (SpecialFolder.Trash), "[Gmail]/Trash", FolderAttributes.HasNoChildren | FolderAttributes.Trash, true, 41234, 0, 0, 1143, 2, 0); client.Disconnect (false); } }
public async void TestImapClientGMail () { var commands = new List<ImapReplayCommand> (); commands.Add (new ImapReplayCommand ("", "gmail.greeting.txt")); commands.Add (new ImapReplayCommand ("A00000000 CAPABILITY\r\n", "gmail.capability.txt")); commands.Add (new ImapReplayCommand ("A00000001 AUTHENTICATE PLAIN AHVzZXJuYW1lAHBhc3N3b3Jk\r\n", "gmail.authenticate.txt")); commands.Add (new ImapReplayCommand ("A00000002 NAMESPACE\r\n", "gmail.namespace.txt")); commands.Add (new ImapReplayCommand ("A00000003 LIST \"\" \"INBOX\"\r\n", "gmail.list-inbox.txt")); commands.Add (new ImapReplayCommand ("A00000004 XLIST \"\" \"*\"\r\n", "gmail.xlist.txt")); commands.Add (new ImapReplayCommand ("A00000005 LIST \"\" \"%\"\r\n", "gmail.list-personal.txt")); commands.Add (new ImapReplayCommand ("A00000006 CREATE UnitTests\r\n", ImapReplayCommandResponse.OK)); commands.Add (new ImapReplayCommand ("A00000007 LIST \"\" UnitTests\r\n", "gmail.list-unittests.txt")); commands.Add (new ImapReplayCommand ("A00000008 SELECT UnitTests (CONDSTORE)\r\n", "gmail.select-unittests.txt")); for (int i = 0; i < 50; i++) { MimeMessage message; string latin1; long length; using (var resource = GetResourceStream (string.Format ("common.message.{0}.msg", i))) message = MimeMessage.Load (resource); using (var stream = new MemoryStream ()) { var options = FormatOptions.Default.Clone (); options.NewLineFormat = NewLineFormat.Dos; message.WriteTo (options, stream); length = stream.Length; stream.Position = 0; using (var reader = new StreamReader (stream, Latin1)) latin1 = reader.ReadToEnd (); } var command = string.Format ("A{0:D8} APPEND UnitTests (\\Seen) ", i + 9); if (length > 4096) { command += "{" + length + "}\r\n"; commands.Add (new ImapReplayCommand (command, "gmail.go-ahead.txt")); commands.Add (new ImapReplayCommand (latin1 + "\r\n", string.Format ("gmail.append.{0}.txt", i + 1))); } else { command += "{" + length + "+}\r\n" + latin1 + "\r\n"; commands.Add (new ImapReplayCommand (command, string.Format ("gmail.append.{0}.txt", i + 1))); } } commands.Add (new ImapReplayCommand ("A00000059 UID SEARCH RETURN () OR TO nsb CC nsb\r\n", "gmail.search.txt")); commands.Add (new ImapReplayCommand ("A00000060 UID FETCH 1:3,5,7:9,11:14,26:29,31,34,41:43,50 (UID FLAGS INTERNALDATE RFC822.SIZE ENVELOPE BODY)\r\n", "gmail.search-summary.txt")); commands.Add (new ImapReplayCommand ("A00000061 UID FETCH 1 (BODY.PEEK[])\r\n", "gmail.fetch.1.txt")); commands.Add (new ImapReplayCommand ("A00000062 UID FETCH 2 (BODY.PEEK[])\r\n", "gmail.fetch.2.txt")); commands.Add (new ImapReplayCommand ("A00000063 UID FETCH 3 (BODY.PEEK[])\r\n", "gmail.fetch.3.txt")); commands.Add (new ImapReplayCommand ("A00000064 UID FETCH 5 (BODY.PEEK[])\r\n", "gmail.fetch.5.txt")); commands.Add (new ImapReplayCommand ("A00000065 UID FETCH 7 (BODY.PEEK[])\r\n", "gmail.fetch.7.txt")); commands.Add (new ImapReplayCommand ("A00000066 UID FETCH 8 (BODY.PEEK[])\r\n", "gmail.fetch.8.txt")); commands.Add (new ImapReplayCommand ("A00000067 UID FETCH 9 (BODY.PEEK[])\r\n", "gmail.fetch.9.txt")); commands.Add (new ImapReplayCommand ("A00000068 UID FETCH 11 (BODY.PEEK[])\r\n", "gmail.fetch.11.txt")); commands.Add (new ImapReplayCommand ("A00000069 UID FETCH 12 (BODY.PEEK[])\r\n", "gmail.fetch.12.txt")); commands.Add (new ImapReplayCommand ("A00000070 UID FETCH 13 (BODY.PEEK[])\r\n", "gmail.fetch.13.txt")); commands.Add (new ImapReplayCommand ("A00000071 UID FETCH 14 (BODY.PEEK[])\r\n", "gmail.fetch.14.txt")); commands.Add (new ImapReplayCommand ("A00000072 UID FETCH 26 (BODY.PEEK[])\r\n", "gmail.fetch.26.txt")); commands.Add (new ImapReplayCommand ("A00000073 UID FETCH 27 (BODY.PEEK[])\r\n", "gmail.fetch.27.txt")); commands.Add (new ImapReplayCommand ("A00000074 UID FETCH 28 (BODY.PEEK[])\r\n", "gmail.fetch.28.txt")); commands.Add (new ImapReplayCommand ("A00000075 UID FETCH 29 (BODY.PEEK[])\r\n", "gmail.fetch.29.txt")); commands.Add (new ImapReplayCommand ("A00000076 UID FETCH 31 (BODY.PEEK[])\r\n", "gmail.fetch.31.txt")); commands.Add (new ImapReplayCommand ("A00000077 UID FETCH 34 (BODY.PEEK[])\r\n", "gmail.fetch.34.txt")); commands.Add (new ImapReplayCommand ("A00000078 UID FETCH 41 (BODY.PEEK[])\r\n", "gmail.fetch.41.txt")); commands.Add (new ImapReplayCommand ("A00000079 UID FETCH 42 (BODY.PEEK[])\r\n", "gmail.fetch.42.txt")); commands.Add (new ImapReplayCommand ("A00000080 UID FETCH 43 (BODY.PEEK[])\r\n", "gmail.fetch.43.txt")); commands.Add (new ImapReplayCommand ("A00000081 UID FETCH 50 (BODY.PEEK[])\r\n", "gmail.fetch.50.txt")); commands.Add (new ImapReplayCommand ("A00000082 UID STORE 1:3,5,7:9,11:14,26:29,31,34,41:43,50 FLAGS (\\Answered \\Seen)\r\n", "gmail.set-flags.txt")); commands.Add (new ImapReplayCommand ("A00000083 UID STORE 1:3,5,7:9,11:14,26:29,31,34,41:43,50 -FLAGS.SILENT (\\Answered)\r\n", ImapReplayCommandResponse.OK)); commands.Add (new ImapReplayCommand ("A00000084 UID STORE 1:3,5,7:9,11:14,26:29,31,34,41:43,50 +FLAGS.SILENT (\\Deleted)\r\n", "gmail.add-flags.txt")); commands.Add (new ImapReplayCommand ("A00000085 CHECK\r\n", ImapReplayCommandResponse.OK)); commands.Add (new ImapReplayCommand ("A00000086 UNSELECT\r\n", ImapReplayCommandResponse.OK)); commands.Add (new ImapReplayCommand ("A00000087 SUBSCRIBE UnitTests\r\n", ImapReplayCommandResponse.OK)); commands.Add (new ImapReplayCommand ("A00000088 LSUB \"\" \"%\"\r\n", "gmail.lsub-personal.txt")); commands.Add (new ImapReplayCommand ("A00000089 UNSUBSCRIBE UnitTests\r\n", ImapReplayCommandResponse.OK)); commands.Add (new ImapReplayCommand ("A00000090 CREATE UnitTests/Dummy\r\n", ImapReplayCommandResponse.OK)); commands.Add (new ImapReplayCommand ("A00000091 LIST \"\" UnitTests/Dummy\r\n", "gmail.list-unittests-dummy.txt")); commands.Add (new ImapReplayCommand ("A00000092 RENAME UnitTests RenamedUnitTests\r\n", ImapReplayCommandResponse.OK)); commands.Add (new ImapReplayCommand ("A00000093 DELETE RenamedUnitTests\r\n", ImapReplayCommandResponse.OK)); commands.Add (new ImapReplayCommand ("A00000094 LOGOUT\r\n", "gmail.logout.txt")); using (var client = new ImapClient ()) { try { client.ReplayConnect ("localhost", new ImapReplayStream (commands, false)); } catch (Exception ex) { Assert.Fail ("Did not expect an exception in Connect: {0}", ex); } Assert.IsTrue (client.IsConnected, "Client failed to connect."); Assert.AreEqual (GMailInitialCapabilities, client.Capabilities); Assert.AreEqual (5, client.AuthenticationMechanisms.Count); Assert.IsTrue (client.AuthenticationMechanisms.Contains ("XOAUTH"), "Expected SASL XOAUTH auth mechanism"); Assert.IsTrue (client.AuthenticationMechanisms.Contains ("XOAUTH2"), "Expected SASL XOAUTH2 auth mechanism"); Assert.IsTrue (client.AuthenticationMechanisms.Contains ("OAUTHBEARER"), "Expected SASL OAUTHBEARER auth mechanism"); Assert.IsTrue (client.AuthenticationMechanisms.Contains ("PLAIN"), "Expected SASL PLAIN auth mechanism"); Assert.IsTrue (client.AuthenticationMechanisms.Contains ("PLAIN-CLIENTTOKEN"), "Expected SASL PLAIN-CLIENTTOKEN auth mechanism"); // Note: Do not try XOAUTH2 client.AuthenticationMechanisms.Remove ("XOAUTH2"); try { await client.AuthenticateAsync ("username", "password"); } catch (Exception ex) { Assert.Fail ("Did not expect an exception in Authenticate: {0}", ex); } Assert.AreEqual (GMailAuthenticatedCapabilities, client.Capabilities); Assert.IsTrue (client.AppendLimit.HasValue, "Expected AppendLimit to have a value"); Assert.AreEqual (35651584, client.AppendLimit.Value, "Expected AppendLimit value to match"); var inbox = client.Inbox; Assert.IsNotNull (inbox, "Expected non-null Inbox folder."); Assert.AreEqual (FolderAttributes.Inbox | FolderAttributes.HasNoChildren, inbox.Attributes, "Expected Inbox attributes to be \\HasNoChildren."); foreach (var special in Enum.GetValues (typeof (SpecialFolder)).OfType<SpecialFolder> ()) { var folder = client.GetFolder (special); if (special != SpecialFolder.Archive) { var expected = GetSpecialFolderAttribute (special) | FolderAttributes.HasNoChildren; Assert.IsNotNull (folder, "Expected non-null {0} folder.", special); Assert.AreEqual (expected, folder.Attributes, "Expected {0} attributes to be \\HasNoChildren.", special); } else { Assert.IsNull (folder, "Expected null {0} folder.", special); } } // disable LIST-EXTENDED client.Capabilities &= ~ImapCapabilities.ListExtended; var personal = client.GetFolder (client.PersonalNamespaces[0]); var folders = (await personal.GetSubfoldersAsync ()).ToList (); Assert.AreEqual (client.Inbox, folders[0], "Expected the first folder to be the Inbox."); Assert.AreEqual ("[Gmail]", folders[1].FullName, "Expected the second folder to be [Gmail]."); Assert.AreEqual (FolderAttributes.NoSelect | FolderAttributes.HasChildren, folders[1].Attributes, "Expected [Gmail] folder to be \\Noselect \\HasChildren."); var created = await personal.CreateAsync ("UnitTests", true); Assert.IsNotNull (created, "Expected a non-null created folder."); Assert.AreEqual (FolderAttributes.HasNoChildren, created.Attributes); Assert.IsNotNull (created.ParentFolder, "The ParentFolder property should not be null."); const MessageFlags ExpectedPermanentFlags = MessageFlags.Answered | MessageFlags.Flagged | MessageFlags.Draft | MessageFlags.Deleted | MessageFlags.Seen | MessageFlags.UserDefined; const MessageFlags ExpectedAcceptedFlags = MessageFlags.Answered | MessageFlags.Flagged | MessageFlags.Draft | MessageFlags.Deleted | MessageFlags.Seen; var access = await created.OpenAsync (FolderAccess.ReadWrite); Assert.AreEqual (FolderAccess.ReadWrite, access, "The UnitTests folder was not opened with the expected access mode."); Assert.AreEqual (ExpectedPermanentFlags, created.PermanentFlags, "The PermanentFlags do not match the expected value."); Assert.AreEqual (ExpectedAcceptedFlags, created.AcceptedFlags, "The AcceptedFlags do not match the expected value."); for (int i = 0; i < 50; i++) { using (var stream = GetResourceStream (string.Format ("common.message.{0}.msg", i))) { var message = MimeMessage.Load (stream); var uid = await created.AppendAsync (message, MessageFlags.Seen); Assert.IsTrue (uid.HasValue, "Expected a UID to be returned from folder.Append()."); Assert.AreEqual ((uint) (i + 1), uid.Value.Id, "The UID returned from the APPEND command does not match the expected UID."); } } var query = SearchQuery.ToContains ("nsb").Or (SearchQuery.CcContains ("nsb")); var matches = await created.SearchAsync (query); const MessageSummaryItems items = MessageSummaryItems.Full | MessageSummaryItems.UniqueId; var summaries = await created.FetchAsync (matches, items); foreach (var summary in summaries) { if (summary.UniqueId.IsValid) await created.GetMessageAsync (summary.UniqueId); else await created.GetMessageAsync (summary.Index); } await created.SetFlagsAsync (matches, MessageFlags.Seen | MessageFlags.Answered, false); await created.RemoveFlagsAsync (matches, MessageFlags.Answered, true); await created.AddFlagsAsync (matches, MessageFlags.Deleted, true); await created.CheckAsync (); await created.CloseAsync (); Assert.IsFalse (created.IsOpen, "Expected the UnitTests folder to be closed."); await created.SubscribeAsync (); Assert.IsTrue (created.IsSubscribed, "Expected IsSubscribed to be true after subscribing to the folder."); var subscribed = (await personal.GetSubfoldersAsync (true)).ToList (); Assert.IsTrue (subscribed.Contains (created), "Expected the list of subscribed folders to contain the UnitTests folder."); await created.UnsubscribeAsync (); Assert.IsFalse (created.IsSubscribed, "Expected IsSubscribed to be false after unsubscribing from the folder."); var dummy = await created.CreateAsync ("Dummy", true); bool dummyRenamed = false; bool renamed = false; bool deleted = false; dummy.Renamed += (sender, e) => { dummyRenamed = true; }; created.Renamed += (sender, e) => { renamed = true; }; await created.RenameAsync (created.ParentFolder, "RenamedUnitTests"); Assert.AreEqual ("RenamedUnitTests", created.Name); Assert.AreEqual ("RenamedUnitTests", created.FullName); Assert.IsTrue (renamed, "Expected the Rename event to be emitted for the UnitTests folder."); Assert.AreEqual ("RenamedUnitTests/Dummy", dummy.FullName); Assert.IsTrue (dummyRenamed, "Expected the Rename event to be emitted for the UnitTests/Dummy folder."); created.Deleted += (sender, e) => { deleted = true; }; await created.DeleteAsync (); Assert.IsTrue (deleted, "Expected the Deleted event to be emitted for the UnitTests folder."); Assert.IsFalse (created.Exists, "Expected Exists to be false after deleting the folder."); await client.DisconnectAsync (true); } }
public static void Main (string[] args) { using (var client = new ImapClient (new ProtocolLogger (Console.OpenStandardError ()))) { client.Connect ("imap.gmail.com", 993, true); // Remove the XOAUTH2 authentication mechanism since we don't have an OAuth2 token. client.AuthenticationMechanisms.Remove ("XOAUTH2"); client.Authenticate ("*****@*****.**", "password"); client.Inbox.Open (FolderAccess.ReadOnly); // Get the summary information of all of the messages (suitable for displaying in a message list). var messages = client.Inbox.Fetch (0, -1, MessageSummaryItems.Full | MessageSummaryItems.UniqueId).ToList (); // Keep track of messages being expunged so that when the CountChanged event fires, we can tell if it's // because new messages have arrived vs messages being removed (or some combination of the two). client.Inbox.MessageExpunged += (sender, e) => { var folder = (ImapFolder) sender; if (e.Index < messages.Count) { var message = messages[e.Index]; Console.WriteLine ("{0}: expunged message {1}: Subject: {2}", folder, e.Index, message.Envelope.Subject); // Note: If you are keeping a local cache of message information // (e.g. MessageSummary data) for the folder, then you'll need // to remove the message at e.Index. messages.RemoveAt (e.Index); } else { Console.WriteLine ("{0}: expunged message {1}: Unknown message.", folder, e.Index); } }; // Keep track of changes to the number of messages in the folder (this is how we'll tell if new messages have arrived). client.Inbox.CountChanged += (sender, e) => { // Note: the CountChanged event will fire when new messages arrive in the folder and/or when messages are expunged. var folder = (ImapFolder) sender; Console.WriteLine ("The number of messages in {0} has changed.", folder); // Note: because we are keeping track of the MessageExpunged event and updating our // 'messages' list, we know that if we get a CountChanged event and folder.Count is // larger than messages.Count, then it means that new messages have arrived. if (folder.Count > messages.Count) { Console.WriteLine ("{0} new messages have arrived.", folder.Count - messages.Count); // Note: your first instict may be to fetch these new messages now, but you cannot do // that in this event handler (the ImapFolder is not re-entrant). // // If this code had access to the 'done' CancellationTokenSource (see below), it could // cancel that to cause the IDLE loop to end. } }; // Keep track of flag changes. client.Inbox.MessageFlagsChanged += (sender, e) => { var folder = (ImapFolder) sender; Console.WriteLine ("{0}: flags for message {1} have changed to: {2}.", folder, e.Index, e.Flags); }; Console.WriteLine ("Hit any key to end the IDLE loop."); using (var done = new CancellationTokenSource ()) { // Note: when the 'done' CancellationTokenSource is cancelled, it ends to IDLE loop. var thread = new Thread (IdleLoop); thread.Start (new IdleState (client, done.Token)); Console.ReadKey (); done.Cancel (); thread.Join (); } if (client.Inbox.Count > messages.Count) { Console.WriteLine ("The new messages that arrived during IDLE are:"); foreach (var message in client.Inbox.Fetch (messages.Count, -1, MessageSummaryItems.Full | MessageSummaryItems.UniqueId)) Console.WriteLine ("Subject: {0}", message.Envelope.Subject); } client.Disconnect (true); } }
public static string GetDetailsWithinSpecificTimeSpan(DateTime from, DateTime to, MailReceiverSettings settings) { using (var client = new ImapClient()) { try { IMailFolder inbox = AuthenticateAndgetInbox(settings, client); BinarySearchQuery query = SearchQuery.DeliveredAfter(from) .And(SearchQuery.SubjectContains(text: "Work Order - 'SF Recover Equipment'")) .And(SearchQuery.DeliveredBefore(to)); IList<UniqueId> indexes = inbox.Search(query); var dicts = new List<Dictionary<string, string>>(); foreach (var index in indexes) { MimeMessage msg = inbox.GetMessage(index); foreach (var ma in msg.Attachments) { string file = virtualpath + ma.ContentType.Name; RemoveIfAboveTen(virtualpath); if (!File.Exists(file)) { DownloadAttachement((MimePart)ma, file); } dicts.Add(PdfExtractor.ExtractInfoWithPolicy(file)); } } client.Disconnect(quit: true); return SheetUpdater.Create(dicts, string.Format("{0}-{1}_", from.ToString("dd_MM_yyyy"), to.ToString("dd_MM_yyyy"))); } catch (Exception ex) { ErrorQueue.Enqueue(ex.Message); } } return string.Empty; }