public IList<Stream> Load(Mailbox mailbox) { IList<Stream> result = new List<Stream>(); using (var client = new Pop3Client()) { client.Connect(mailbox.Server, mailbox.Port, mailbox.Ssl); client.Authenticate(mailbox.Username, mailbox.Password); var msgCount = client.Count; Console.WriteLine("[{2}] - [{1}] {0} new messages found", msgCount, mailbox.Username, DateTime.Now); for (var li = 0; li < msgCount; li++) { try { var letter = client.GetMessage(li); var parts = letter.BodyParts; foreach (var part in parts) { if (part.IsAttachment) { var stream = new MemoryStream(); ((MimePart)part).ContentObject.WriteTo(stream); stream.Position = 0; result.Add(stream); } } } catch (Exception ex) { Console.WriteLine("Message recieve error: {0}", ex.Message); } finally { client.DeleteMessage(li); } } client.Disconnect(true); } return result; }
public static void DownloadMessages () { using (var client = new Pop3Client (new ProtocolLogger ("pop3.log"))) { client.Connect ("pop.gmail.com", 995, SecureSocketOptions.SslOnConnect); client.Authenticate ("username", "password"); for (int i = 0; i < client.Count; i++) { var message = client.GetMessage (i); // write the message to a file message.WriteTo (string.Format ("{0}.msg", i)); // mark the message for deletion client.DeleteMessage (i); } client.Disconnect (true); } }
public void TestAuthenticationExceptions() { var commands = new List<Pop3ReplayCommand> (); commands.Add (new Pop3ReplayCommand ("", "comcast.greeting.txt")); commands.Add (new Pop3ReplayCommand ("CAPA\r\n", "comcast.capa1.txt")); commands.Add (new Pop3ReplayCommand ("USER username\r\n", "comcast.ok.txt")); commands.Add (new Pop3ReplayCommand ("PASS password\r\n", "comcast.err.txt")); commands.Add (new Pop3ReplayCommand ("QUIT\r\n", "comcast.quit.txt")); using (var client = new Pop3Client ()) { try { client.ReplayConnect ("localhost", new Pop3ReplayStream (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 (ComcastCapa1, client.Capabilities); Assert.AreEqual (0, client.AuthenticationMechanisms.Count); Assert.AreEqual (31, client.ExpirePolicy); try { var credentials = new NetworkCredential ("username", "password"); client.Authenticate (credentials, CancellationToken.None); Assert.Fail ("Expected AuthenticationException"); } catch (AuthenticationException) { // we expect this exception... } catch (Exception ex) { Assert.Fail ("Did not expect an exception in Authenticate: {0}", ex); } Assert.IsTrue (client.IsConnected, "AuthenticationException should not cause a disconnect."); try { var count = client.GetMessageCount (CancellationToken.None); Assert.Fail ("Expected UnauthorizedAccessException"); } catch (UnauthorizedAccessException) { // we expect this exception... } catch (Exception ex) { Assert.Fail ("Did not expect an exception in Count: {0}", ex); } Assert.IsTrue (client.IsConnected, "UnauthorizedAccessException should not cause a disconnect."); try { var sizes = client.GetMessageSizes (CancellationToken.None); Assert.Fail ("Expected UnauthorizedAccessException"); } catch (UnauthorizedAccessException) { // we expect this exception... } catch (Exception ex) { Assert.Fail ("Did not expect an exception in GetMessageSizes: {0}", ex); } Assert.IsTrue (client.IsConnected, "UnauthorizedAccessException should not cause a disconnect."); try { var size = client.GetMessageSize ("uid", CancellationToken.None); Assert.Fail ("Expected UnauthorizedAccessException"); } catch (UnauthorizedAccessException) { // we expect this exception... } catch (Exception ex) { Assert.Fail ("Did not expect an exception in GetMessageSize(uid): {0}", ex); } Assert.IsTrue (client.IsConnected, "UnauthorizedAccessException should not cause a disconnect."); try { var size = client.GetMessageSize (0, CancellationToken.None); Assert.Fail ("Expected UnauthorizedAccessException"); } catch (UnauthorizedAccessException) { // we expect this exception... } catch (Exception ex) { Assert.Fail ("Did not expect an exception in GetMessageSize(int): {0}", ex); } Assert.IsTrue (client.IsConnected, "UnauthorizedAccessException should not cause a disconnect."); try { var uids = client.GetMessageUids (CancellationToken.None); Assert.Fail ("Expected UnauthorizedAccessException"); } catch (UnauthorizedAccessException) { // we expect this exception... } catch (Exception ex) { Assert.Fail ("Did not expect an exception in GetMessageUids: {0}", ex); } Assert.IsTrue (client.IsConnected, "UnauthorizedAccessException should not cause a disconnect."); try { var uid = client.GetMessageUid (0, CancellationToken.None); Assert.Fail ("Expected UnauthorizedAccessException"); } catch (UnauthorizedAccessException) { // we expect this exception... } catch (Exception ex) { Assert.Fail ("Did not expect an exception in GetMessageUid: {0}", ex); } Assert.IsTrue (client.IsConnected, "UnauthorizedAccessException should not cause a disconnect."); try { var message = client.GetMessage ("uid", CancellationToken.None); Assert.Fail ("Expected UnauthorizedAccessException"); } catch (UnauthorizedAccessException) { // we expect this exception... } catch (Exception ex) { Assert.Fail ("Did not expect an exception in GetMessage(uid): {0}", ex); } Assert.IsTrue (client.IsConnected, "UnauthorizedAccessException should not cause a disconnect."); try { var message = client.GetMessage (0, CancellationToken.None); Assert.Fail ("Expected UnauthorizedAccessException"); } catch (UnauthorizedAccessException) { // we expect this exception... } catch (Exception ex) { Assert.Fail ("Did not expect an exception in GetMessage(int): {0}", ex); } Assert.IsTrue (client.IsConnected, "UnauthorizedAccessException should not cause a disconnect."); try { client.DeleteMessage ("uid", CancellationToken.None); Assert.Fail ("Expected UnauthorizedAccessException"); } catch (UnauthorizedAccessException) { // we expect this exception... } catch (Exception ex) { Assert.Fail ("Did not expect an exception in DeleteMessage(uid): {0}", ex); } Assert.IsTrue (client.IsConnected, "UnauthorizedAccessException should not cause a disconnect."); try { client.DeleteMessage (0, CancellationToken.None); Assert.Fail ("Expected UnauthorizedAccessException"); } catch (UnauthorizedAccessException) { // we expect this exception... } catch (Exception ex) { Assert.Fail ("Did not expect an exception in DeleteMessage(int): {0}", ex); } Assert.IsTrue (client.IsConnected, "UnauthorizedAccessException should not cause a disconnect."); try { client.Disconnect (true, CancellationToken.None); } catch (Exception ex) { Assert.Fail ("Did not expect an exception in Disconnect: {0}", ex); } Assert.IsFalse (client.IsConnected, "Failed to disconnect"); } }
/* * retrieves messages from pop3 server */ private static void RetrieveMessagesFromServer( Node pars, Node dp, Node ip, string basePath, string attachmentDirectory, string linkedAttachmentDirectory, Node exeNode, Pop3Client client) { int serverCount = client.GetMessageCount(); int count = Expressions.GetExpressionValue<int>(ip.GetValue("count", serverCount.ToString()), dp, ip, false); count = Math.Min(serverCount, count); bool delete = Expressions.GetExpressionValue<bool>(ip.GetValue("delete", "false"), dp, ip, false); for (int idxMsg = 0; idxMsg < count; idxMsg++) { MimeMessage msg = client.GetMessage(idxMsg); HandleMessage( pars, ip, basePath, attachmentDirectory, linkedAttachmentDirectory, exeNode, idxMsg, msg); if (delete) client.DeleteMessage(idxMsg); } }
/* * retrieves messages from pop3 server */ private static void RetrieveMessagesFromServer( Node pars, Node dp, Node ip, string basePath, string attachmentDirectory, string linkedAttachmentDirectory, Node exeNode, Pop3Client client) { int serverCount = client.GetMessageCount(); int count = Expressions.GetExpressionValue<int>(ip.GetValue("count", serverCount.ToString()), dp, ip, false); count = Math.Min(serverCount, count); bool delete = Expressions.GetExpressionValue<bool>(ip.GetValue("delete", "false"), dp, ip, false); for (int idxMsg = 0; idxMsg < count; idxMsg++) { // short circuting in case message is already downloaded string uuid = null; if (client.SupportsUids) { uuid = client.GetMessageUid(idxMsg); if (MessageAlreadyDownloaded(uuid)) continue; } MimeMessage msg = client.GetMessage(idxMsg); HandleMessage( pars, ip, basePath, attachmentDirectory, linkedAttachmentDirectory, exeNode, idxMsg, msg, uuid); if (delete) client.DeleteMessage(idxMsg); } }