private static void Main(string[] args) { XmlConfigurator.Configure(); _logger = LoggerFactory.GetLogger(LoggerFactory.LoggerType.Log4Net, "EMLDownloader"); var options = new Options(); if (CommandLine.Parser.Default.ParseArgumentsStrict(args, options, () => _logger.Info("Bad command line parameters."))) { Imap4Client imap = null; Pop3Client pop = null; try { _logger.Info("Searching account with id {0}", options.MailboxId); var mailbox = GetMailBox(options.MailboxId); if (mailbox == null) { _logger.Info("Account not found."); ShowAnyKey(); return; } string messageEml; if (mailbox.Imap) { _logger.Info("ConnectionType is IMAP4"); imap = MailClientBuilder.Imap(); imap.AuthenticateImap(mailbox, _logger); _logger.Info(imap.ServerCapabilities); var mailboxesString = imap.GetImapMailboxes(); _logger.Info(mailboxesString); if (string.IsNullOrEmpty(options.MessageUid)) { _logger.Info("MessageUid not setup."); ShowAnyKey(); return; } var uidlStucture = ParserImapUidl(options.MessageUid); if (uidlStucture.folderId != 1) { throw new FormatException("Only inbox messages are supported for downloading."); } var mb = imap.SelectMailbox("INBOX"); var uidList = mb.UidSearch("UID 1:*"); if (!uidList.Any(uid => uid == uidlStucture.uid)) { throw new FileNotFoundException(string.Format("Message with uid {0} not found in inbox", uidlStucture.uid)); } _logger.Info("Try Fetch.UidMessageStringPeek"); messageEml = mb.Fetch.UidMessageStringPeek(uidlStucture.uid); } else { if (string.IsNullOrEmpty(options.MessageUid)) { _logger.Info("MessageUid not setup."); ShowAnyKey(); return; } _logger.Info("ConnectionType is POP3"); pop = MailClientBuilder.Pop(); pop.Authorize(new MailServerSettings { AccountName = mailbox.Account, AccountPass = mailbox.Password, AuthenticationType = mailbox.AuthenticationTypeIn, EncryptionType = mailbox.IncomingEncryptionType, Port = mailbox.Port, Url = mailbox.Server }, mailbox.AuthorizeTimeoutInMilliseconds, _logger); _logger.Debug("UpdateStats()"); pop.UpdateStats(); var index = pop.GetMessageIndex(options.MessageUid); if (index < 1) { throw new FileNotFoundException(string.Format("Message with uid {0} not found in inbox", options.MessageUid)); } messageEml = pop.RetrieveMessageString(index); } _logger.Info("Try StoreToFile"); var now = DateTime.Now; 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); _logger.Info("[SUCCESS] File was stored into path \"{0}\"", pathFile); } catch (Exception ex) { _logger.Info(ex.ToString()); } finally { if (imap != null && imap.IsConnected) { imap.Disconnect(); } else if (pop != null && pop.IsConnected) { pop.Disconnect(); } } } ShowAnyKey(); }