Exemplo n.º 1
0
        static public List <Mime> getEmails()
        {
            List <Mime> result = new List <Mime>();

            using (POP3_Client pop3 = new POP3_Client())
            {
                try
                {
                    pop3.Connect("pop3.163.com", 110, false);
                    pop3.Authenticate("*****@*****.**", "6728924", false);

                    //获取邮件信息列表
                    POP3_ClientMessageCollection infos = pop3.Messages;

                    foreach (POP3_ClientMessage info in infos)
                    {
                        byte[] bytes = info.MessageToByte();

                        //解析从Pop3服务器发送过来的邮件信息
                        Mime mime = Mime.Parse(bytes);

                        result.Add(mime);
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message);
                }
            }
            return(result);
        }
Exemplo n.º 2
0
        public override void Authenticate()
        {
            if (channelState == ChannelState.Authenticated)
            {
                return;
            }

            if (channelState != ChannelState.Connected)
            {
                throw new ChannelException("Unable to authenticate in current channel state");
            }

            try
            {
                client.Authenticate(Username, Password, true);

                channelState = ChannelState.Authenticated;
            }
            catch (POP3_ClientException ex)
            {
                channelState = ChannelState.Broken;

                Logger.Debug("FAILED Authenticating connection {0}. Server said {1}", LogSource.Channel, UniqueId, ex.ResponseText);

                throw new ChannelAuthenticationException(ex.ResponseText, ex);
            }
        }
Exemplo n.º 3
0
        private List <Mime> GetEmails(string pop3Server, int pop3Port, bool pop3UseSsl, string username, string password, out List <string> mailMsgIds)
        {
            mailMsgIds = new List <string>();
            List <Mime>   result      = new List <Mime>();
            List <string> gotEmailIds = new List <string>();

            using (POP3_Client pop3 = new POP3_Client())
            {
                pop3.Connect(pop3Server, pop3Port, pop3UseSsl);
                pop3.Authenticate(username, password, false);
                POP3_ClientMessageCollection infos = pop3.Messages;
                int maxRetriveMail = this.GetMaxRetrieveMail();
                int messageCount   = infos != null ? infos.Count : 0;
                int totalEmails    = messageCount > maxRetriveMail ? maxRetriveMail : messageCount;

                //foreach (POP3_ClientMessage info in infos)
                for (int i = totalEmails; i > 0; i--)
                {
                    var info = infos[i - 1];
                    if (gotEmailIds.Contains(info.UID))
                    {
                        continue;
                    }
                    byte[] bytes = info.MessageToByte();
                    gotEmailIds.Add(info.UID);
                    Mime mime = Mime.Parse(bytes);
                    result.Add(mime);
                }

                mailMsgIds.AddRange(gotEmailIds);
            }

            return(result);
        }
Exemplo n.º 4
0
        public static List <Email> GetAllEmails(string server, int port, string user, string pwd, bool usessl, bool deleteafterread, ref List <string> errors)
        {
            var ret = new List <Email>();

            errors.Clear();
            var oClient = new POP3_Client();

            try
            {
                oClient.Connect(server, port, usessl);
                oClient.Authenticate(user, pwd, true);
            }
            catch (Exception exception)
            {
                errors.Add("Error connect/authenticate - " + exception.Message);
                return(null);
            }
            foreach (POP3_ClientMessage message in oClient.Messages)
            {
                var wrapper = new Email();
                wrapper.Uid = message.UID;
                try
                {
                    Mail_Message mime = Mail_Message.ParseFromByte(message.HeaderToByte());
                    wrapper.Subject = mime.Subject;
                    wrapper.From    = mime.From[0].Address;
                    wrapper.To      = mime.To[0].ToString();
                    mime            = Mail_Message.ParseFromByte(message.MessageToByte());

                    string sa = mime.BodyHtmlText;
                    if (sa == null)
                    {
                        wrapper.TextBody = mime.BodyText;
                    }
                    else
                    {
                        wrapper.HtmlBody = sa;
                    }
                }
                catch (Exception exception)
                {
                    errors.Add("Error reading " + wrapper.Uid + " - " + exception.Message);
                }
                if (deleteafterread)
                {
                    message.MarkForDeletion();
                }
                ret.Add(wrapper);
            }
            oClient.Disconnect();
            oClient.Dispose();

            return(ret);
        }
Exemplo n.º 5
0
        public void Connect()
        {
            if (_popClient != null)
            {
                throw new ApplicationException("Already connected.");
            }

            _popClient = new POP3_Client();
            _popClient.Connect(PopMailProviderConfiguration.Settings.Server.Name, PopMailProviderConfiguration.Settings.Server.Port);
            _popClient.Authenticate(PopMailProviderConfiguration.Settings.Server.Username, PopMailProviderConfiguration.Settings.Server.Password, true);
        }
Exemplo n.º 6
0
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string    userId = context.Request.QueryString["UserID"];
            DataTable table;

            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                connection.Open();
                SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM [TU_EmailPOP] WHERE UserID='" + userId + "'", connection);
                table = new DataTable();
                sda.Fill(table);
            }
            string host     = string.Empty;
            string userName = string.Empty;;
            string password = string.Empty;;
            int    port     = 993;
            bool   ssl      = true;

            foreach (DataRow row in table.Rows)
            {
                host     = row["Host"].ToString();
                userName = row["UserName"].ToString();
                password = row["Password"].ToString();
                port     = int.Parse(row["Port"].ToString());
            }
            string      json  = string.Empty;
            List <Item> items = new List <Item>();

            using (POP3_Client client = new POP3_Client())
            {
                client.Connect(host, port, ssl);
                client.Authenticate(userName, password, false);
                var messages = client.Messages;
                foreach (POP3_ClientMessage message in messages)
                {
                    Mail_Message email = Mail_Message.ParseFromByte(message.MessageToByte());
                    Item         item  = new Item
                    {
                        Sender   = email.From.ToString().Replace("\"", ""),
                        Subject  = email.Subject,
                        SendDate = email.Date.ToString(),
                        Date     = email.Date.ToString(),
                    };
                    items.Add(item);
                }
            }
            items = items.OrderByDescending(i => i.SendDate).ToList <Item>();
            json  = JsonConvert.SerializeObject(items);
            context.Response.Write(json);
        }
Exemplo n.º 7
0
        protected void btnTestConnection_Click(object sender, EventArgs e)
        {
            int port = 993;

            WX.WXUser user     = WX.Main.CurUser;
            string    host     = this.txtHostAddress.Text.Trim();
            string    userName = this.txtUserName.Text.Trim();
            string    password = this.txtPassword.Text.Trim();
            bool      b        = int.TryParse(this.txtPort.Text, out port);
            bool      ssl      = this.chkSSL.Checked;

            using (POP3_Client client = new POP3_Client())
            {
                try
                {
                    client.Connect(host, port, ssl);
                    client.Authenticate(userName, password, false);
                    using (SqlConnection connection = new SqlConnection(connectionString))
                    {
                        connection.Open();
                        string insertText = "INSERT INTO [TU_EmailPOP] ([UserID],[Host],[UserName],[Password],[Port],[SSL]) VALUES (@UserID,@Host,@UserName,@Password,@Port,@SSL)";
                        string updateText = "UPDATE [TU_EmailPOP] SET [UserID]=@UserID,[Host]=@Host,[UserName]=@UserName,[Password]=@Password,[Port]=@Port,[SSL]=@SSL";

                        SqlCommand command;
                        command = new SqlCommand("SELECT COUNT(ID) FROM [TU_EmailPOP] WHERE UserID='" + user.UserID + "'", connection);
                        int row = (int)command.ExecuteScalar();
                        if (row > 0)
                        {
                            command = new SqlCommand(updateText, connection);
                        }
                        else
                        {
                            command = new SqlCommand(insertText, connection);
                        }
                        command.Parameters.AddWithValue("@UserID", user.UserID);
                        command.Parameters.AddWithValue("@Host", host);
                        command.Parameters.AddWithValue("@UserName", userName);
                        command.Parameters.AddWithValue("@Password", password);
                        command.Parameters.AddWithValue("@Port", port);
                        command.Parameters.AddWithValue("@SSL", true);
                        command.ExecuteNonQuery();
                        MessageBox.ShowAndRedirect(this, "邮箱配置成功", Request.RawUrl);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.ShowAndRedirect(this, "邮箱配置失败:" + ex.Message, Request.RawUrl);
                }
            }
        }
        public static string SearchSingleEmail(string subject,string date)
        {
            string userId = new WX.WXUser().UserID;
            DataTable table;
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                connection.Open();
                SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM [TU_EmailPOP] WHERE UserID='" + userId + "'", connection);
                table = new DataTable();
                sda.Fill(table);
            }
            string host = string.Empty;
            string userName = string.Empty;
            string password = string.Empty;
            int port = 995;
            bool ssl = true;
            foreach (DataRow row in table.Rows)
            {
                host = row["Host"].ToString();
                userName = row["UserName"].ToString();
                password = row["Password"].ToString();
                port = int.Parse(row["Port"].ToString());
            }
            string json = string.Empty;
            using (POP3_Client client = new POP3_Client())
            {
                client.Connect(host, port, ssl);
                client.Authenticate(userName, password, false);
                var messages = client.Messages;

                foreach (POP3_ClientMessage message in messages)
                {
                    Mail_Message email = Mail_Message.ParseFromByte(message.MessageToByte());
                    if (email.Subject.Equals(subject) && email.Date == Convert.ToDateTime(date))
                    {
                        TestItem item = new TestItem
                        {
                            Subject = email.Subject,
                            Body = email.BodyHtmlText
                        };
                        json = JsonConvert.SerializeObject(item);
                    }
                }

            }
            return json;
        }
Exemplo n.º 9
0
        private void DeleteMail(string pop3Server, int pop3Port, bool pop3UseSsl, string username, string password, List <string> mailMsgIds)
        {
            using (POP3_Client c = new POP3_Client())
            {
                c.Connect(pop3Server, pop3Port);
                c.Authenticate(username, password, pop3UseSsl);

                if (c.Messages.Count > 0)
                {
                    foreach (POP3_ClientMessage mail in c.Messages)
                    {
                        if (mailMsgIds.Contains(mail.UID))
                        {
                            Logger.Info(_logMsg.Clear().SetPrefixMsg("Mark For Deletion").Add("UID", mail.UID));
                            mail.MarkForDeletion();
                        }
                    }
                }
            }
        }
Exemplo n.º 10
0
        /// <summary>
        /// Starts messages fetching.
        /// </summary>
        public void StartFetching()
        {
            if (m_Fetching)
            {
                return;
            }

            m_Fetching = true;

            try{
                DataView dvUsers = m_pApi.GetUsers("ALL");

                using (DataView dvServers = m_pApi.GetUserRemoteServers("")){
                    foreach (DataRowView drV in dvServers)
                    {
                        try{
                            if (!ConvertEx.ToBoolean(drV["Enabled"]))
                            {
                                continue;
                            }

                            // Find user name from user ID
                            string userName = "";
                            dvUsers.RowFilter = "UserID='" + drV["UserID"] + "'";
                            if (dvUsers.Count > 0)
                            {
                                userName = dvUsers[0]["UserName"].ToString();
                            }
                            else
                            {
                                continue;
                            }

                            string server = drV.Row["RemoteServer"].ToString();
                            int    port   = Convert.ToInt32(drV.Row["RemotePort"]);
                            string user   = drV.Row["RemoteUserName"].ToString();
                            string passw  = drV.Row["RemotePassword"].ToString();
                            bool   useSSL = ConvertEx.ToBoolean(drV["UseSSL"]);

                            // Connect and login to pop3 server
                            using (POP3_Client clnt = new POP3_Client()){
                                clnt.Logger           = new LumiSoft.Net.Log.Logger();
                                clnt.Logger.WriteLog += new EventHandler <WriteLogEventArgs>(Pop3_WriteLog);
                                clnt.Connect(server, port, useSSL);
                                clnt.Authenticate(user, passw, false);

                                foreach (POP3_ClientMessage message in clnt.Messages)
                                {
                                    // Store message
                                    m_pServer.ProcessUserMsg("", "", userName, "Inbox", new MemoryStream(message.MessageToByte()), null);

                                    message.MarkForDeletion();
                                }
                            }
                        }
                        catch {
                        }
                    }
                }

                m_LastFetch = DateTime.Now;
            }
            catch (Exception x) {
                Error.DumpError(m_pServer.Name, x);
            }
            m_Fetching = false;
        }