Exemple #1
0
        public void FetchMessages(NameValueCollection param)
        {
            Pop3 client = new Pop3();
            bool connected = false;
            try
            {
                string outdir = RequireParam("outdir", param);
                if (!Directory.Exists(outdir)) Directory.CreateDirectory(outdir);

                string server = RequireParam("server", param);
                int port = Convert.ToInt32(RequireParam("port", param));
                string ssl = param["ssl"];
                if (ssl.Length == 0)
                    ssl = port == 995 ? "1" : "0";

                TlsParameters par = new TlsParameters();
                par.CommonName = server;
                par.CertificateVerifier = CertificateVerifier.AcceptAll;
                Pop3Security sec = (Pop3Security)Enum.Parse(typeof(Pop3Security), RequireParam("security", param));
                log.Info("Connecting to {0}:{1}. SSL: {2}", server, port, ssl);

                string sess = client.Connect(server, port, par, sec);

                connected = true;
                log.Info("Connected: {0}", sess);
                Pop3Authentication auth = (Pop3Authentication)Enum.Parse(typeof(Pop3Authentication), RequireParam("auth", param));
                log.Info("Logging in: {0}", RequireParam("user", param));
                client.Login(RequireParam("user", param), RequireParam("pwd", param), auth);
                log.Info("Logged in.");

                Pop3MessageCollection messages = client.GetMessageList();
                log.Info("There are {0} messages", messages.Count);
                int maxcount = Convert.ToInt32(RequireParam("maxcount", param));
                if (maxcount <= 0 || maxcount > messages.Count) maxcount = messages.Count;
                for (int i = 0; i < maxcount; i++)
                {
                    Pop3MessageInfo mi = messages[i];
                    log.Info("Downloading message {0}", mi.SequenceNumber);
                    MailMessage mm = client.GetMailMessage(mi.SequenceNumber);
                    log.Info("Message from: {0}, to: {1}, subject: {2}", mm.From, mm.To, mm.Subject);
                    string g = Guid.NewGuid().ToString();
                    string file = Path.Combine(outdir, g + ".eml");
                    log.Info("Saving message to {0}", file);
                    mm.Save(file);
                    client.Delete(mi.SequenceNumber);
                }

                bool rollback = !"0".Equals(RequireParam("noremove", param));
                client.Disconnect(rollback);
            }
            catch (Exception ex)
            {
                if (connected)
                    client.Disconnect(true);
                throw;
            }
        }
        /// <summary>
        /// Releases unmanaged and - optionally - managed resources.
        /// </summary>
        /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
        /// <remarks>
        /// If the main class was marked as sealed, we could just make this a private void Dispose(bool).  Alternatively, we could (in this case) put
        /// all of our logic directly in Dispose().
        /// </remarks>
        public virtual void Dispose(bool disposing)
        {
            // Use our disposed flag to allow us to call this method multiple times safely.
            // This is a requirement when implementing IDisposable.
            if (!this.disposed)
            {
                if (disposing)
                {
                    // If we have any managed, IDisposable resources, Dispose of them here.
                    // In this case, we don't, so this was unneeded.
                    // Later, we will subclass this class, and use this section.

                    if (Pop3 != null)
                    {
                        Pop3.Disconnect(true);
                        Pop3.Dispose();
                        Pop3 = null;
                    }

                    if (Imap != null)
                    {
                        Imap.Disconnect(true);
                        Imap.Dispose();
                        Imap = null;
                    }
                }

                // Always dispose of undisposed unmanaged resources in Dispose(bool).
            }
            // Mark us as disposed, to prevent multiple calls to dispose from having an effect,
            // and to allow us to handle ObjectDisposedException.
            this.disposed = true;
        }
Exemple #3
0
 public override void Disconnect()
 {
     try
     {
         if (IsConnected())
         {
             _pop3Obj.Disconnect();
         }
     }
     catch (MailBeeException ex)
     {
         Log.WriteException(ex);
         throw new WebMailMailBeeException(ex);
     }
 }
 public void ClosePop3()
 {
     try
     {
         if (Pop3 != null)
         {
             Pop3.Disconnect(true);
             Pop3.Dispose();
             Pop3 = null;
         }
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.ToString());
     }
 }
Exemple #5
0
        private void Connect(Func <Pop3, bool> onConnectFunction)
        {
            Debug.WriteLine("GetAll");

            Pop3.LicenseKey = "MN200-539BA4259BD99BD69B32E3AB96B2-6863";
            var pop = new Pop3();


            if (_ssl)
            {
                pop.SslProtocol = SecurityProtocol.Auto;
                pop.SslMode     = SslStartupMode.OnConnect;
            }

            try
            {
                if (pop.Connect(_server, _port, true))
                {
                    Debug.WriteLine("Connected");

                    if (pop.Login(_login,
                                  _password,
                                  AuthenticationMethods.Auto,
                                  AuthenticationOptions.PreferSimpleMethods,
                                  null))
                    {
                        Debug.WriteLine("Logged in " + pop.InboxMessageCount);
                        onConnectFunction(pop);
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("GetAll");
                Debug.WriteLine(ex);
                _lastError = ex.Message;
            }
            finally
            {
                if (pop.IsConnected)
                {
                    pop.Disconnect();
                }
                pop.Dispose();
            }
        }
Exemple #6
0
 public void Disconnect()
 {
     try
     {
         if (_client.IsConnected)
         {
             _client.Disconnect();
         }
     }
     catch (EmailException)
     {
         throw;
     }
     catch (Exception ex)
     {
         throw new EmailException("Cannot disconnect", ex);
     }
 }
Exemple #7
0
            internal int Pop3Check()
            {
                try
                {
                    var pop3Settings = new Pop3Settings();
                    var obj          = new Pop3();
                    obj.Connect(pop3Settings.Host, pop3Settings.Username, pop3Settings.Password, pop3Settings.Port);
                    string KeyWord = Helpers.Xml.AppConfigQuery("jaNET/System/Comm/MailKeyword").Item(0).InnerText;

                    foreach (Pop3Message msg in obj.List())
                    {
                        Pop3Message msg2 = obj.Retrieve(msg);

                        /*Console.WriteLine("Message {0}: {1}",
                         *  msg2.number, msg2.message);*/
                        if (msg2.Message.Contains("<" + KeyWord + ">"))
                        {
                            //If a command found to mail subject
                            Match Command = Regex.Match(msg2.Message.Replace("\r\n", " "), @"(<" + KeyWord + ">)(.*?)(?=</" + KeyWord + ">)");
                            Command.ToString().ToLower().Replace("<" + KeyWord + ">", string.Empty).Parse();
                            obj.Delete(msg2);
                        }
                        else
                        {
                            //For Future Use

                            /*Match From = Regex.Match(msg2.message, @"(?<=From: )(.*?)(?= <)");
                             * Match Subject = Regex.Match(msg2.message, @"(?<=Subject: )(.*?)(?=\\r\\nDate: )"); //(?<=Subject:</B> )(.*?)(?=</)");
                             * MailList.Add("From " + From.ToString() + ", Subject " + Subject.ToString());*/
                            //From pattern (?<=From: \\\")(.*?)(?=\\\")
                            //Subject pattern (?<=Subject: )(.*?)(?=\\r)
                        }
                    }
                    obj.Disconnect();
                    return(obj.List().Count);
                }
                catch
                {
                    return(0);
                }
            }
Exemple #8
0
        private void CheckEmail(object sender, System.Timers.ElapsedEventArgs args)
        {
            try
            {
                m_pop3 = new Pop3();
                m_pop3.Connect(m_strServer, m_strLogin, m_strPassword);
                ArrayList list = m_pop3.List();

                foreach (Pop3Message msg in list)
                {
                    Pop3Message msg2 = m_pop3.Retrieve(msg);
                    m_pop3.Delete(msg);

                    _OnMessageReceived(msg2.From, msg2.Subject, false);
                }

                m_pop3.Disconnect();
            }
            catch (Pop3Exception e)
            {
                _OnError(this, e.Message);
                return;
            }
        }
Exemple #9
0
            internal int Pop3Check()
            {
                try {
                    var pop3Settings = new Pop3Settings();
                    var obj = new Pop3();
                    obj.Connect(pop3Settings.Host, pop3Settings.Username, pop3Settings.Password, pop3Settings.Port);
                    string KeyWord = Helpers.Xml.AppConfigQuery("jaNET/System/Comm/MailKeyword").Item(0).InnerText;

                    foreach (Pop3Message msg in obj.List()) {
                        Pop3Message msg2 = obj.Retrieve(msg);
                        /*Console.WriteLine("Message {0}: {1}",
                            msg2.number, msg2.message);*/
                        if (msg2.Message.Contains("<" + KeyWord + ">")) {
                            //If a command found to mail subject
                            Match Command = Regex.Match(msg2.Message.Replace("\r\n", " "), @"(<" + KeyWord + ">)(.*?)(?=</" + KeyWord + ">)");
                            Command.ToString().ToLower().Replace("<" + KeyWord + ">", string.Empty).Parse();
                            obj.Delete(msg2);
                        }
                        else {
                            //For Future Use
                            /*Match From = Regex.Match(msg2.message, @"(?<=From: )(.*?)(?= <)");
                            Match Subject = Regex.Match(msg2.message, @"(?<=Subject: )(.*?)(?=\\r\\nDate: )"); //(?<=Subject:</B> )(.*?)(?=</)");
                            MailList.Add("From " + From.ToString() + ", Subject " + Subject.ToString());*/
                            //From pattern (?<=From: \\\")(.*?)(?=\\\")
                            //Subject pattern (?<=Subject: )(.*?)(?=\\r)
                        }
                    }
                    obj.Disconnect();
                    return obj.List().Count;
                }
                catch {
                    return 0;
                }
            }
 public void Disconnect()
 {
     _pop3.Disconnect();
 }