Example #1
0
        static void Main(string[] args)
        {
            if (!CheckArgs(args))
            {
                return;
            }

            var hostAndPort = args[0].Split(':');
            string host = hostAndPort[0];
            int port = int.Parse(hostAndPort[1]);
            string user = args[1];
            string password = args[2];
            string folder = args[3];

            List<string> paramNames = new List<string>();
            if (args.Length > 4)
            {
                for (int i = 4; i < args.Length; ++i)
                {
                    paramNames.Add(args[i]);
                }
            }

            using (var client = new ImapClient(host, port, true, new NetworkCredential(user, password)))
            {
                client.ParseFailures += HandleParseFailures;

                bool loginOk = false;

                if (user.Equals("XOAUTH", StringComparison.InvariantCultureIgnoreCase))
                {
                    loginOk = client.TrySaslLogin(user, password);
                }
                else
                {
                    loginOk = client.TryLogin();
                }

                if (loginOk)
                {
                    System.Console.WriteLine("INFO: Login to [{0}:{1}] succeeded for user [{2}].", host, port, user);
                    ImapFolder f = client.SelectFolder(folder);
                    System.Console.WriteLine("INFO: 'Next UID value' is [{0}].", f.UidNext);
                    string cmd = null;
                    string upperCmd = null;

                    if (f != null)
                    {
                        System.Console.WriteLine();
                        System.Console.WriteLine(">> To exit, type 'EXIT';  To just dump messages in selected folder 'DUMP [lowUid[:highUid]]';");
                        System.Console.WriteLine(">> to just dump a message body 'BODY msgUid'; otherwise issue IMAP command with the interactive");
                        System.Console.WriteLine(">> console.");
                        System.Console.WriteLine();

                        while (true)
                        {
                            System.Console.Write("C: {0} ", client.NextCommandNumber);

                            cmd = System.Console.ReadLine().Trim();
                            upperCmd = cmd.ToUpperInvariant();
                            if (string.IsNullOrEmpty(cmd))
                            {
                                continue;
                            }
                            if (upperCmd.StartsWith("EXIT"))
                            {
                                return;
                            }
                            else if (upperCmd.StartsWith("DUMP"))
                            {
                                DumpMessages(paramNames, client, f, upperCmd);
                            }
                            else if (upperCmd.StartsWith("BODY"))
                            {
                                DumpBody(paramNames, client, f, upperCmd);
                            }
                            else
                            {
                                var resp = client.SendReceive(cmd);
                                foreach (var line in resp.Lines)
                                {
                                    System.Console.WriteLine("S: {0}", line);
                                }
                            }
                        }
                    }
                    else
                    {
                        Usage(string.Format("ERROR: Server [{0}:{1}] does not support folder [{2]}.", host, port, folder));
                    }
                }
                else
                {
                    Usage(string.Format("ERROR: Login to [{0}:{1}] failed for user [{2}].", host, port, user));
                }
            }
        }