コード例 #1
0
ファイル: Pop3.cs プロジェクト: leigh-pointer/phosphorusfive
        public static void p5_pop3_get_emails (ApplicationContext context, ActiveEventArgs e)
        {
            // Making sure we remove arguments supplied
            using (new Utilities.ArgsRemover (e.Args, true)) {

                // Creating our POP3 client
                using (var client = new Pop3Client ()) {

                    // Connecting to POP3 server using helper from Common class
                    Common.ConnectServer (context, client, e.Args, "pop3");

                    // Figuring out how many messages to retrieve, defaulting to "5" if not explicitly told something else by caller,
                    // making sure we never try to retrieve more messages than server actually has
                    int noMessages = Math.Min (client.Count, e.Args.GetChildValue ("count", context, 5));

                    // Fetching messages from server, but not any more messages than caller requested, or number of available messages
                    for (int idxMsg = 0; idxMsg < noMessages; idxMsg++) {

                        // Process message returned from POP3 server by building Node structure wrapping message
                        var msgNode = ProcessMessage (
                            context, 
                            e.Args,
                            client.GetMessage (idxMsg),
                            e.Args.GetChildValue ("process-envelope", context, true),
                            e.Args.GetChildValue ("process-body", context, true));

                        // Handle message after processing
                        HandleMessage (context, msgNode, e.Args);

                        // Checking if we should delete message from server
                        if (e.Args.GetChildValue ("delete", context, false)) {

                            // Deleting message from server, making sure we wait til deletion is done before continuting execution
                            client.DeleteMessageAsync (idxMsg).Wait ();
                        }
                    }

                    // Disconnecting from server, making sure we send the QUIT signal
                    client.Disconnect (true);
                }
            }
        }
コード例 #2
0
        public async Task DeleteEmailMessageFromPopServerAsync(IList <ExtractedEmailMessage> messages, IEmailConfiguration popEmailConfiguration)
        {
            using (var client = new Pop3Client())
            {
                await ConnectToServer(popEmailConfiguration, client);

                var uids = messages.Select(x => UniqueId.Parse(x.MessageUniqueId)).ToList();

                for (int i = 0; i < client.Count; i++)
                {
                    var message = client.GetMessage(i);
                    var uid     = client.GetMessageUid(i);
                    if (uids.Any(x => x.ToString().Equals(uid)))
                    {
                        await client.DeleteMessageAsync(i);
                    }
                }

                await client.DisconnectAsync(true);
            }
        }
コード例 #3
0
        public async void TestInvalidStateExceptions()
        {
            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()) {
                Assert.Throws <ServiceNotConnectedException> (async() => await client.AuthenticateAsync("username", "password"));
                Assert.Throws <ServiceNotConnectedException> (async() => await client.AuthenticateAsync(new NetworkCredential("username", "password")));

                Assert.Throws <ServiceNotConnectedException> (async() => await client.NoOpAsync());

                Assert.Throws <ServiceNotConnectedException> (async() => await client.GetMessageSizesAsync());
                Assert.Throws <ServiceNotConnectedException> (async() => await client.GetMessageSizeAsync("uid"));
                Assert.Throws <ServiceNotConnectedException> (async() => await client.GetMessageSizeAsync(0));
                Assert.Throws <ServiceNotConnectedException> (async() => await client.GetMessageUidsAsync());
                Assert.Throws <ServiceNotConnectedException> (async() => await client.GetMessageUidAsync(0));
                Assert.Throws <ServiceNotConnectedException> (async() => await client.GetMessageAsync("uid"));
                Assert.Throws <ServiceNotConnectedException> (async() => await client.GetMessageAsync(0));
                Assert.Throws <ServiceNotConnectedException> (async() => await client.GetMessageHeadersAsync("uid"));
                Assert.Throws <ServiceNotConnectedException> (async() => await client.GetMessageHeadersAsync(0));
                Assert.Throws <ServiceNotConnectedException> (async() => await client.GetStreamAsync(0));
                Assert.Throws <ServiceNotConnectedException> (async() => await client.GetStreamsAsync(0, 1));
                Assert.Throws <ServiceNotConnectedException> (async() => await client.GetStreamsAsync(new int[] { 0 }));
                Assert.Throws <ServiceNotConnectedException> (async() => await client.DeleteMessageAsync("uid"));
                Assert.Throws <ServiceNotConnectedException> (async() => await client.DeleteMessageAsync(0));

                try {
                    client.ReplayConnect("localhost", new Pop3ReplayStream(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(ComcastCapa1, client.Capabilities);
                Assert.AreEqual(0, client.AuthenticationMechanisms.Count);
                Assert.AreEqual(31, client.ExpirePolicy);

                Assert.Throws <AuthenticationException> (async() => await client.AuthenticateAsync("username", "password"));
                Assert.IsTrue(client.IsConnected, "AuthenticationException should not cause a disconnect.");

                Assert.Throws <ServiceNotAuthenticatedException> (async() => await client.GetMessageSizesAsync());
                Assert.IsTrue(client.IsConnected, "ServiceNotAuthenticatedException should not cause a disconnect.");

                Assert.Throws <ServiceNotAuthenticatedException> (async() => await client.GetMessageSizeAsync("uid"));
                Assert.IsTrue(client.IsConnected, "ServiceNotAuthenticatedException should not cause a disconnect.");

                Assert.Throws <ServiceNotAuthenticatedException> (async() => await client.GetMessageSizeAsync(0));
                Assert.IsTrue(client.IsConnected, "ServiceNotAuthenticatedException should not cause a disconnect.");

                Assert.Throws <ServiceNotAuthenticatedException> (async() => await client.GetMessageUidsAsync());
                Assert.IsTrue(client.IsConnected, "ServiceNotAuthenticatedException should not cause a disconnect.");

                Assert.Throws <ServiceNotAuthenticatedException> (async() => await client.GetMessageUidAsync(0));
                Assert.IsTrue(client.IsConnected, "ServiceNotAuthenticatedException should not cause a disconnect.");

                Assert.Throws <ServiceNotAuthenticatedException> (async() => await client.GetMessageAsync("uid"));
                Assert.IsTrue(client.IsConnected, "ServiceNotAuthenticatedException should not cause a disconnect.");

                Assert.Throws <ServiceNotAuthenticatedException> (async() => await client.GetMessageAsync(0));
                Assert.IsTrue(client.IsConnected, "ServiceNotAuthenticatedException should not cause a disconnect.");

                Assert.Throws <ServiceNotAuthenticatedException> (async() => await client.GetMessageHeadersAsync("uid"));
                Assert.IsTrue(client.IsConnected, "ServiceNotAuthenticatedException should not cause a disconnect.");

                Assert.Throws <ServiceNotAuthenticatedException> (async() => await client.GetMessageHeadersAsync(0));
                Assert.IsTrue(client.IsConnected, "ServiceNotAuthenticatedException should not cause a disconnect.");

                Assert.Throws <ServiceNotAuthenticatedException> (async() => await client.GetStreamAsync(0));
                Assert.IsTrue(client.IsConnected, "ServiceNotAuthenticatedException should not cause a disconnect.");

                Assert.Throws <ServiceNotAuthenticatedException> (async() => await client.GetStreamsAsync(0, 1));
                Assert.IsTrue(client.IsConnected, "ServiceNotAuthenticatedException should not cause a disconnect.");

                Assert.Throws <ServiceNotAuthenticatedException> (async() => await client.GetStreamsAsync(new int[] { 0 }));
                Assert.IsTrue(client.IsConnected, "ServiceNotAuthenticatedException should not cause a disconnect.");

                Assert.Throws <ServiceNotAuthenticatedException> (async() => await client.DeleteMessageAsync("uid"));
                Assert.IsTrue(client.IsConnected, "ServiceNotAuthenticatedException should not cause a disconnect.");

                Assert.Throws <ServiceNotAuthenticatedException> (async() => await client.DeleteMessageAsync(0));
                Assert.IsTrue(client.IsConnected, "ServiceNotAuthenticatedException should not cause a disconnect.");

                try {
                    await client.DisconnectAsync(true);
                } catch (Exception ex) {
                    Assert.Fail("Did not expect an exception in Disconnect: {0}", ex);
                }

                Assert.IsFalse(client.IsConnected, "Failed to disconnect");
            }
        }
コード例 #4
0
ファイル: Pop3ClientTests.cs プロジェクト: yeaicc/MailKit
        public async void TestGMailPop3Client()
        {
            var commands = new List <Pop3ReplayCommand> ();

            commands.Add(new Pop3ReplayCommand("", "gmail.greeting.txt"));
            commands.Add(new Pop3ReplayCommand("CAPA\r\n", "gmail.capa1.txt"));
            commands.Add(new Pop3ReplayCommand("AUTH PLAIN\r\n", "gmail.plus.txt"));
            commands.Add(new Pop3ReplayCommand("AHVzZXJuYW1lAHBhc3N3b3Jk\r\n", "gmail.auth.txt"));
            commands.Add(new Pop3ReplayCommand("CAPA\r\n", "gmail.capa2.txt"));
            commands.Add(new Pop3ReplayCommand("STAT\r\n", "gmail.stat.txt"));
            commands.Add(new Pop3ReplayCommand("UIDL\r\n", "gmail.uidl.txt"));
            commands.Add(new Pop3ReplayCommand("UIDL 1\r\n", "gmail.uidl1.txt"));
            commands.Add(new Pop3ReplayCommand("UIDL 2\r\n", "gmail.uidl2.txt"));
            commands.Add(new Pop3ReplayCommand("UIDL 3\r\n", "gmail.uidl3.txt"));
            commands.Add(new Pop3ReplayCommand("LIST\r\n", "gmail.list.txt"));
            commands.Add(new Pop3ReplayCommand("LIST 1\r\n", "gmail.list1.txt"));
            commands.Add(new Pop3ReplayCommand("LIST 2\r\n", "gmail.list2.txt"));
            commands.Add(new Pop3ReplayCommand("LIST 3\r\n", "gmail.list3.txt"));
            commands.Add(new Pop3ReplayCommand("RETR 1\r\n", "gmail.retr1.txt"));
            commands.Add(new Pop3ReplayCommand("RETR 1\r\nRETR 2\r\nRETR 3\r\n", "gmail.retr123.txt"));
            commands.Add(new Pop3ReplayCommand("RETR 1\r\nRETR 2\r\nRETR 3\r\n", "gmail.retr123.txt"));
            commands.Add(new Pop3ReplayCommand("TOP 1 0\r\n", "gmail.top.txt"));
            commands.Add(new Pop3ReplayCommand("TOP 1 0\r\nTOP 2 0\r\nTOP 3 0\r\n", "gmail.top123.txt"));
            commands.Add(new Pop3ReplayCommand("TOP 1 0\r\nTOP 2 0\r\nTOP 3 0\r\n", "gmail.top123.txt"));
            commands.Add(new Pop3ReplayCommand("RETR 1\r\n", "gmail.retr1.txt"));
            commands.Add(new Pop3ReplayCommand("RETR 1\r\nRETR 2\r\nRETR 3\r\n", "gmail.retr123.txt"));
            commands.Add(new Pop3ReplayCommand("RETR 1\r\nRETR 2\r\nRETR 3\r\n", "gmail.retr123.txt"));
            commands.Add(new Pop3ReplayCommand("NOOP\r\n", "gmail.noop.txt"));
            commands.Add(new Pop3ReplayCommand("DELE 1\r\n", "gmail.dele.txt"));
            commands.Add(new Pop3ReplayCommand("RSET\r\n", "gmail.rset.txt"));
            commands.Add(new Pop3ReplayCommand("DELE 1\r\nDELE 2\r\nDELE 3\r\n", "gmail.dele123.txt"));
            commands.Add(new Pop3ReplayCommand("RSET\r\n", "gmail.rset.txt"));
            commands.Add(new Pop3ReplayCommand("DELE 1\r\nDELE 2\r\nDELE 3\r\n", "gmail.dele123.txt"));
            commands.Add(new Pop3ReplayCommand("RSET\r\n", "gmail.rset.txt"));
            commands.Add(new Pop3ReplayCommand("DELE 1\r\nDELE 2\r\nDELE 3\r\n", "gmail.dele123.txt"));
            commands.Add(new Pop3ReplayCommand("RSET\r\n", "gmail.rset.txt"));
            commands.Add(new Pop3ReplayCommand("QUIT\r\n", "gmail.quit.txt"));

            using (var client = new Pop3Client()) {
                try {
                    client.ReplayConnect("localhost", new Pop3ReplayStream(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(GMailCapa1, client.Capabilities);
                Assert.AreEqual(2, client.AuthenticationMechanisms.Count);
                Assert.IsTrue(client.AuthenticationMechanisms.Contains("XOAUTH2"), "Expected SASL XOAUTH2 auth mechanism");
                Assert.IsTrue(client.AuthenticationMechanisms.Contains("PLAIN"), "Expected SASL PLAIN auth mechanism");

                // Note: remove the XOAUTH2 auth mechanism to force PLAIN auth
                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(GMailCapa2, client.Capabilities);
                Assert.AreEqual(0, client.AuthenticationMechanisms.Count);

                Assert.AreEqual(3, client.Count, "Expected 3 messages");

                var uids = await client.GetMessageUidsAsync();

                Assert.AreEqual(3, uids.Count);
                Assert.AreEqual("101", uids[0]);
                Assert.AreEqual("102", uids[1]);
                Assert.AreEqual("103", uids[2]);

                for (int i = 0; i < 3; i++)
                {
                    var uid = await client.GetMessageUidAsync(i);

                    Assert.AreEqual(uids[i], uid);
                }

                var sizes = await client.GetMessageSizesAsync();

                Assert.AreEqual(3, sizes.Count);
                Assert.AreEqual(1024, sizes[0]);
                Assert.AreEqual(1025, sizes[1]);
                Assert.AreEqual(1026, sizes[2]);

                for (int i = 0; i < 3; i++)
                {
                    var size = await client.GetMessageSizeAsync(i);

                    Assert.AreEqual(sizes[i], size);
                }

                try {
                    var message = await client.GetMessageAsync(0);

                    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("5b1b8b2c9300c9cd01099f44e1155e2b", md5sum, "MD5 checksums do not match.");
                        }
                    }
                } catch (Exception ex) {
                    Assert.Fail("Did not expect an exception in GetMessage: {0}", ex);
                }

                try {
                    var messages = await client.GetMessagesAsync(0, 3);

                    foreach (var message in messages)
                    {
                        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("5b1b8b2c9300c9cd01099f44e1155e2b", md5sum, "MD5 checksums do not match.");
                            }
                        }
                    }
                } catch (Exception ex) {
                    Assert.Fail("Did not expect an exception in GetMessages: {0}", ex);
                }

                try {
                    var messages = await client.GetMessagesAsync(new [] { 0, 1, 2 });

                    foreach (var message in messages)
                    {
                        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("5b1b8b2c9300c9cd01099f44e1155e2b", md5sum, "MD5 checksums do not match.");
                            }
                        }
                    }
                } catch (Exception ex) {
                    Assert.Fail("Did not expect an exception in GetMessages: {0}", ex);
                }

                try {
                    var header = await client.GetMessageHeadersAsync(0);

                    Assert.AreEqual("Test inline image", header[HeaderId.Subject]);
                } catch (Exception ex) {
                    Assert.Fail("Did not expect an exception in GetMessageHeaders: {0}", ex);
                }

                try {
                    var headers = await client.GetMessageHeadersAsync(0, 3);

                    Assert.AreEqual(3, headers.Count);
                    for (int i = 0; i < headers.Count; i++)
                    {
                        Assert.AreEqual("Test inline image", headers[i][HeaderId.Subject]);
                    }
                } catch (Exception ex) {
                    Assert.Fail("Did not expect an exception in GetMessageHeaders: {0}", ex);
                }

                try {
                    var headers = await client.GetMessageHeadersAsync(new [] { 0, 1, 2 });

                    Assert.AreEqual(3, headers.Count);
                    for (int i = 0; i < headers.Count; i++)
                    {
                        Assert.AreEqual("Test inline image", headers[i][HeaderId.Subject]);
                    }
                } catch (Exception ex) {
                    Assert.Fail("Did not expect an exception in GetMessageHeaders: {0}", ex);
                }

                try {
                    using (var stream = await client.GetStreamAsync(0)) {
                    }
                } catch (Exception ex) {
                    Assert.Fail("Did not expect an exception in GetStream: {0}", ex);
                }

                try {
                    var streams = await client.GetStreamsAsync(0, 3);

                    Assert.AreEqual(3, streams.Count);
                    for (int i = 0; i < 3; i++)
                    {
                        streams[i].Dispose();
                    }
                } catch (Exception ex) {
                    Assert.Fail("Did not expect an exception in GetStreams: {0}", ex);
                }

                try {
                    var streams = await client.GetStreamsAsync(new int[] { 0, 1, 2 });

                    Assert.AreEqual(3, streams.Count);
                    for (int i = 0; i < 3; i++)
                    {
                        streams[i].Dispose();
                    }
                } catch (Exception ex) {
                    Assert.Fail("Did not expect an exception in GetStreams: {0}", ex);
                }

                try {
                    await client.NoOpAsync();
                } catch (Exception ex) {
                    Assert.Fail("Did not expect an exception in NoOp: {0}", ex);
                }

                try {
                    await client.DeleteMessageAsync(0);
                } catch (Exception ex) {
                    Assert.Fail("Did not expect an exception in DeleteMessage: {0}", ex);
                }

                try {
                    await client.ResetAsync();
                } catch (Exception ex) {
                    Assert.Fail("Did not expect an exception in Reset: {0}", ex);
                }

                try {
                    await client.DeleteMessagesAsync(new [] { 0, 1, 2 });
                } catch (Exception ex) {
                    Assert.Fail("Did not expect an exception in DeleteMessages: {0}", ex);
                }

                try {
                    await client.ResetAsync();
                } catch (Exception ex) {
                    Assert.Fail("Did not expect an exception in Reset: {0}", ex);
                }

                try {
                    await client.DeleteMessagesAsync(0, 3);
                } catch (Exception ex) {
                    Assert.Fail("Did not expect an exception in DeleteMessages: {0}", ex);
                }

                try {
                    await client.ResetAsync();
                } catch (Exception ex) {
                    Assert.Fail("Did not expect an exception in Reset: {0}", ex);
                }

                try {
                    await client.DeleteAllMessagesAsync();
                } catch (Exception ex) {
                    Assert.Fail("Did not expect an exception in DeleteAllMessages: {0}", ex);
                }

                try {
                    await client.ResetAsync();
                } catch (Exception ex) {
                    Assert.Fail("Did not expect an exception in Reset: {0}", ex);
                }

                try {
                    await client.DisconnectAsync(true);
                } catch (Exception ex) {
                    Assert.Fail("Did not expect an exception in Disconnect: {0}", ex);
                }

                Assert.IsFalse(client.IsConnected, "Failed to disconnect");
            }
        }