示例#1
0
        public static Pop3SettingsResult CheckSettings(string Server, int Port, string User, string Password, Pop3SecureConnectionType SecureConnectionType)
        {
            string strEMailHost = Server;
            int iEMailPort = Port;
            string strEMailUser = User;
            string strEMailPassword = Password;

            Pop3SettingsResult retVal = Pop3SettingsResult.None;

            try
            {
                //IPHostEntry hostInfo = Dns.GetHostEntry(strEMailHost);
                IPAddress[] addresses = Dns.GetHostAddresses(strEMailHost);

                if (addresses.Length > 0)
                {
                    IPEndPoint pop3ServerEndPoint = new IPEndPoint(addresses[0], iEMailPort);

                    Pop3Connection pop3Connection = new Pop3Connection();

                    if (SecureConnectionType == Pop3SecureConnectionType.Ssl)
                    {
                        pop3Connection.OpenSsl(pop3ServerEndPoint, Server);
                    }
                    else
                    {
                        pop3Connection.Open(pop3ServerEndPoint);
                    }

                    if (SecureConnectionType == Pop3SecureConnectionType.Tls)
                    {
                        pop3Connection.Stls(Server);
                    }

                    retVal |= Pop3SettingsResult.ServerName;

                    try
                    {
                        pop3Connection.User(strEMailUser);
                        retVal |= Pop3SettingsResult.Pop3User;

                        pop3Connection.Pass(strEMailPassword);
                        retVal |= Pop3SettingsResult.Pop3Password;

                        Pop3Stat stat = pop3Connection.Stat();
                    }
                    finally
                    {
                        pop3Connection.Quit();
                    }
                }
            }
            catch (Exception ex)
            {
                string strErrMsg = ex.Message;
            }

            return retVal;
        }
        public void ReceiveMessages()
        {
            try
            {
                Pop3Connection connection = new Pop3Connection();

                //IPHostEntry hostInfo = Dns.GetHostEntry(Owner.Server);
                IPAddress[] addresses = Dns.GetHostAddresses(Owner.Server);

                IPEndPoint pop3ServerEndPoint = new IPEndPoint(addresses[0], Owner.Port);

                if (this.Owner.SecureConnectionType == Pop3SecureConnectionType.Ssl)
                {
                    connection.OpenSsl(pop3ServerEndPoint, Owner.Server);
                }
                else
                {
                    connection.Open(pop3ServerEndPoint);
                }

                if (this.Owner.SecureConnectionType == Pop3SecureConnectionType.Tls)
                {
                    connection.Stls(Owner.Server);
                }

                connection.User(Owner.Login);
                connection.Pass(Owner.Pass);

                Pop3Stat stat = connection.Stat();

                EMailRouterPop3Box.UpdateStatistic(this.Owner.EMailRouterPop3BoxId,
                    true,
                    string.Empty, stat.MessageCout);

                if(stat.MessageCout > 0)
                {
                    // Step 3. Request uidl.
                    Pop3UIDInfoList uidList = connection.Uidl();

                    foreach (Pop3UIDInfo uidItem in uidList)
                    {
                        try
                        {
                            // Step 4. Request message.
                            Pop3Message msg = connection.Retr(uidItem.ID);

                            // Step 5. Process message.
                            RaiseNewMessageEvent(msg);

                            // Step 6. Delete message from server.
                            connection.Dele(uidItem.ID);
                        }
                        catch (Exception  ex)
                        {
                            if(RaiseErrorEvent(ex))
                                throw;
                        }
                    }
                }

                connection.Quit();
            }
            catch(Exception ex)
            {
                if(RaiseErrorEvent(ex))
                    throw;
            }
        }
示例#3
0
文件: Pop3Box.cs 项目: 0anion0/IBN
        public void Request()
        {
            System.Diagnostics.Trace.WriteLine("Pop3Box.Request()");

            _lastRequest = DateTime.Now;
            _lastErrorText = String.Empty;

            try
            {
                System.Diagnostics.Trace.WriteLine(string.Format("Dns.GetHostByName({0})", _server));

                //IPHostEntry hostInfo = Dns.GetHostEntry(_server);
                IPAddress[] addresses = Dns.GetHostAddresses(_server);

                if (addresses.Length > 0)
                {
                    IPEndPoint pop3ServerEndPoint = new IPEndPoint(addresses[0], _port);

                    Pop3Connection pop3Connection = new Pop3Connection();

                    pop3Connection.Open(pop3ServerEndPoint);
                    System.Diagnostics.Trace.WriteLine(string.Format("pop3Connection.Open - OK"));

                    pop3Connection.User(_login);
                    System.Diagnostics.Trace.WriteLine(string.Format("pop3Connection.Login - OK"));

                    pop3Connection.Pass(_password);
                    System.Diagnostics.Trace.WriteLine(string.Format("pop3Connection.Password - OK"));

                    Pop3Stat stat = pop3Connection.Stat();
                    System.Diagnostics.Trace.WriteLine(string.Format("Message Count = {0}", stat.MessageCout));

                    Exception innerException = null;

                    if(stat.MessageCout > 0)
                    {
                        Pop3UIDInfoList messageList = pop3Connection.Uidl();
                        foreach(Pop3UIDInfo mi in messageList)
                        {
                            System.Diagnostics.Trace.WriteLine(string.Format("Message ID = {0}, UID = {1}", mi.ID, mi.UID));

                            if (!_AutoKillForRead && DBPop3Box.CheckMessageUId(_id, mi.UID))
                                continue;

                            Pop3Message message = pop3Connection.Retr(mi.ID);

                            System.Diagnostics.Trace.WriteLine(string.Format("pop3Connection.Retr - OK"));

                            foreach(Pop3MessageHandlerInfo handlerInfo in _handlers)
                            {
                                try
                                {
                                    handlerInfo.Handler.ProcessPop3Message(this, message);

                                    if (_AutoKillForRead)
                                    {
                                        pop3Connection.Dele(mi.ID);
                                    }
                                    else
                                    {
                                        DBPop3Box.AddMessageUId(_id, mi.UID);
                                    }

                                    System.Diagnostics.Trace.WriteLine(string.Format("Handler.ProcessPop3Message - OK"));
                                }
                                catch(Exception ex)
                                {
                                    System.Diagnostics.Trace.WriteLine(ex);
                                    innerException = ex;
                                }
                            }
                        }
                    }

                    pop3Connection.Quit();

                    if(innerException!=null)
                        throw innerException;

                    System.Diagnostics.Trace.WriteLine("Quit - OK");
                }
                else
                    throw new ArgumentException(string.Format("Could not find server {0}.", _server) );

                _lastSuccessfulRequest = _lastRequest;
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine(ex);
                _lastErrorText = ex.Message;
            }

            System.Diagnostics.Trace.WriteLine(string.Format("DBPop3Box.UpdateTime(id = {0}, lastRequest = {1}, lastSuccessfulRequest = {2}, lastErrorText = {3})", _id, _lastRequest, _lastSuccessfulRequest, _lastErrorText));
            DBPop3Box.UpdateTime(_id, _lastRequest, _lastSuccessfulRequest, _lastErrorText);
        }