Exemplo n.º 1
0
        public static void TestIMAPReceiveMessage(string imap_server, string imap_account, string imap_password, int imap_port, bool use_ssl, string uidl)
        {
            var imap = MailClientBuilder.Imap();

            try
            {
                _log.Debug("Connecting to {0}", imap_account);

                var match = _imapUidlRegx.Match(uidl);

                if (!match.Success)
                {
                    _log.Error("Bad UIDL");
                    return;
                }

                var uid          = Convert.ToInt32(match.Groups[1].Value);
                var folder       = Convert.ToInt32(match.Groups[2].Value);
                var uid_validity = Convert.ToInt32(match.Groups[3].Value);

                if (use_ssl)
                {
                    imap.ConnectSsl(imap_server, imap_port);
                }
                else
                {
                    imap.Connect(imap_server, imap_port);
                }

                imap.Login(imap_account, imap_password, "");

                var folders = MailQueueItem.GetImapMailboxes(imap, imap_server);

                var founded = (from n in folders
                               where n.folder_id == folder
                               select n)
                              .FirstOrDefault();

                if (string.IsNullOrEmpty(founded.name))
                {
                    _log.Error("Bad UIDL folder");
                    return;
                }

                var mb = imap.SelectMailbox(founded.name);

                if (mb.UidValidity != uid_validity)
                {
                    _log.Error("Bad UID_VALIDITY");
                    return;
                }

                var message = mb.Fetch.UidMessageObject(uid);

                var mail_info = new MailMessageItem(message);

// ReSharper disable UnusedVariable
                var sanitazed = HtmlSanitizer.Sanitize(mail_info.HtmlBody, true);
// ReSharper restore UnusedVariable
            }
            catch (Pop3Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            finally
            {
                try
                {
                    if (imap.IsConnected)
                    {
                        imap.Disconnect();
                    }
                }
                catch
                { }
            }
        }