static void Main(string[] args) { try { var cmdArgs = new Args(); CommandLine.Parser.Default.ParseArgumentsStrict(args, cmdArgs); string password = cmdArgs.Password; if (password == "*") { Console.Write("Password: "******"Password: "******"*", Math.Max(20, password.Length)))); } } Console.WriteLine("Connecting to server"); using (ImapClient cli = new ImapClient(cmdArgs.Host, cmdArgs.Port == 0 ? (cmdArgs.Ssl ? 993 : 143) : cmdArgs.Port, cmdArgs.Ssl)) { Console.WriteLine("Logging in"); cli.Login(cmdArgs.UserName, password, AuthMethod.Auto); Console.WriteLine(); Dump(() => cli.ListMailboxes(), "Mailboxes"); //Dump(() => cli.Capabilities(), "Capabilities"); //Dump(cli.GetMailboxInfo("INBOX")); //Dump(cli.GetMailboxInfo("Elementy wysłane")); string mb = cmdArgs.DraftsFolder; //Dump(cli.GetMailboxInfo(mb)); var ids = cli.Search(SearchCondition.All(), mb); Console.WriteLine("{0} message(s)"); foreach (var id in ids) { Console.WriteLine(id); var fs = cli.GetMessageFlags(id, mb); Console.WriteLine("\t{0}: {1}", "Flags", string.Join(" ", fs.Select(f => f.ToString()))); var m = cli.GetMessage(id, FetchOptions.Normal, false, mb); Console.WriteLine("\tHeaders:"); foreach (string h in m.Headers) { Console.WriteLine("\t\t{0}: {1}", h, m.Headers[h]); } if (m.Subject.StartsWith("[PATCH")) { var fileName = string.Format("{0}.patch", id); Console.WriteLine("Saving message to {0}", fileName); Func<string, string> adjustHeaderValue = k => { var v = m.Headers[k]; if (k == "Content-Type") { if (v != null) { var i = v.IndexOf("charset="); if (0 <= i) { return v.Substring(0, i) + "charset=\"UTF-8\""; } } } else if (k == "Content-Transfer-Encoding") { return "8bit"; } return v; }; File.WriteAllBytes( fileName, Encoding.UTF8.GetBytes( string.Join( Environment.NewLine, m.Headers.Cast<string>().Select(k => k + ": " + adjustHeaderValue(k)) .Concat( new[] { string.Empty, m.Body })))); } Console.WriteLine(); } Console.WriteLine("Logging out"); cli.Logout(); } } catch (Exception x) { Console.WriteLine(x); } }
static int Main(string[] args) { try { var cmdArgs = new Args(); CommandLine.Parser.Default.ParseArgumentsStrict(args, cmdArgs); string password = cmdArgs.Password; if (password == "*") { Console.Write("Password: "******"Password: "******"*", Math.Max(20, password.Length)))); } } if (cmdArgs.Verbose) { Console.WriteLine("Connecting to server"); } using (ImapClient cli = new ImapClient(cmdArgs.Host, cmdArgs.Port == 0 ? (cmdArgs.Ssl ? 993 : 143) : cmdArgs.Port, cmdArgs.Ssl)) { if (cmdArgs.Verbose) { Console.WriteLine("Logging in"); } cli.Login(cmdArgs.UserName, password, AuthMethod.Auto); if (cmdArgs.Verbose) { Console.WriteLine(); } string mb = cmdArgs.DraftsFolder; var ids = cli.Search(SearchCondition.All(), mb); foreach (var id in ids) { var fs = cli.GetMessageFlags(id, mb); if (fs.Contains(MessageFlag.Draft)) { continue; } if (cmdArgs.Verbose) { Console.WriteLine(id); Console.WriteLine("\t{0}: {1}", "Flags", string.Join(" ", fs.Select(f => f.ToString()))); var m = cli.GetMessage(id, FetchOptions.HeadersOnly, false, mb); Console.WriteLine("\tHeaders:"); foreach (string h in m.Headers) { Console.WriteLine("\t\t{0}: {1}", h, m.Headers[h]); } } Console.WriteLine("Setting Draft flag on message {0}", id); cli.SetMessageFlags(id, mb, MessageFlag.Draft, MessageFlag.Seen); var fs2 = cli.GetMessageFlags(id, mb); if (cmdArgs.Verbose) { Console.WriteLine("Message flags: {0}", string.Join(" ", fs2.Select(f => f.ToString()))); Console.WriteLine(); } } if (cmdArgs.Verbose) { Console.WriteLine("Logging out"); } cli.Logout(); } return 0; } catch (Exception x) { Console.WriteLine("{0}: {1}", x.GetType().FullName, x.Message); return 1; } }