Exemplo n.º 1
0
        private static void Main(string[] args)
        {
            XmlConfigurator.Configure();

            var options = new Options();

            if (CommandLine.Parser.Default.ParseArgumentsStrict(args, options,
                                                                () => Console.WriteLine("Bad command line parameters.")))
            {
                try
                {
                    Console.WriteLine("Searching account with id {0}", options.MailboxId);

                    if (string.IsNullOrEmpty(options.MessageUid))
                    {
                        Console.WriteLine("MessageUid not setup.");
                        ShowAnyKey();
                        return;
                    }

                    var mailbox = GetMailBox(options.MailboxId);

                    if (mailbox == null)
                    {
                        Console.WriteLine("Account not found.");
                        ShowAnyKey();
                        return;
                    }

                    if (mailbox.Imap)
                    {
                        var uidlStucture = ParserImapUidl(options.MessageUid);
                        if (uidlStucture.folderId != MailFolder.Ids.inbox)
                        {
                            throw new FormatException("Only inbox messages are supported for downloading.");
                        }
                    }

                    var certificatePermit = ConfigurationManager.AppSettings["mail.certificate-permit"] != null &&
                                            Convert.ToBoolean(
                        ConfigurationManager.AppSettings["mail.certificate-permit"]);

                    string messageEml;
                    using (var client = new MailClient(mailbox, CancellationToken.None, certificatePermit: mailbox.IsTeamlab || certificatePermit))
                    {
                        var message = client.GetInboxMessage(options.MessageUid);
                        messageEml = message.ToString();
                    }

                    Console.WriteLine("Try StoreToFile");

                    var now = DateTime.UtcNow;

                    var path = Path.Combine(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Downloads"),
                                            string.Format("uid_{0}_{1}.eml", options.MessageUid,
                                                          now.ToString("dd_MM_yyyy_hh_mm")));


                    var pathFile = StoreToFile(messageEml, path, true);

                    Console.WriteLine("[SUCCESS] File was stored into path \"{0}\"", pathFile);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
            }

            ShowAnyKey();
        }