private static void fetchAllMessages(object sender, DoWorkEventArgs e)
        {
            int percentComplete;

            // The client disconnects from the server when being disposed
            using (Pop3Client client = new Pop3Client())
            {
                // Connect to the server
                //client.Connect("pop.gmail.com", 995, true);
                client.Connect("mail1.stofanet.dk", 110, false);

                // Authenticate ourselves towards the server
                client.Authenticate("2241859m002", "big1234");

                // Get the number of messages in the inbox
                int messageCount = client.GetMessageCount();

                // We want to download all messages
                List <OpenPop.Mime.Message> allMessages = new List <OpenPop.Mime.Message>(messageCount);

                // Messages are numbered in the interval: [1, messageCount]
                // Ergo: message numbers are 1-based.
                // Most servers give the latest message the highest number

                for (int i = messageCount; i > 0; i--)
                {
                    allMessages.Add(client.GetMessage(i));
                    percentComplete = Convert.ToInt16((Convert.ToDouble(allMessages.Count) / Convert.ToDouble(messageCount)) * 100);
                    (sender as BackgroundWorker).ReportProgress(percentComplete);
                }

                // Now return the fetched messages
                e.Result = allMessages;
            }
        }
Beispiel #2
0
        //Connect to gmail, get emails and ad it to a MessageList
        public static List <OpenPop.Mime.Message> GetAllMessages(string hostname, int port, bool ssl, string username, string password)
        {
            try
            {
                Pop3Client client = new Pop3Client();

                if (client.Connected)
                {
                    client.Disconnect();
                }

                client.Connect(hostname, port, ssl);
                client.Authenticate(username, password);

                int messageCount = client.GetMessageCount();

                List <OpenPop.Mime.Message> allMessages = new List <OpenPop.Mime.Message>(messageCount);

                for (int i = messageCount; i > messageCount - 5; i--)
                {
                    allMessages.Add(client.GetMessage(i));
                }

                return(allMessages);
            }

            catch (Exception ex)
            {
                MessageBox.Show("You have written username or password wrong or something else has happened! Program will not keep going, please open it again, thank you : " + ex.Message);
                Thread.CurrentThread.Abort();
                //EmailClient.
            }

            return(null);
        }
        /* Function to retrive Email from the POP3 Server */
        public void GetAllMails(object sender, DoWorkEventArgs e)
        {
            int percentComplete;

            using (Pop3Client client = new Pop3Client())
            {
                /* Set the Server, Port, ssl and username/password */
                client.Connect(Setting.Default.pop3_server, Setting.Default.pop3_port, Setting.Default.smtp_ssl);
                client.Authenticate(Setting.Default.username, Setting.Default.password);

                int messageCount = client.GetMessageCount();

                Debug.WriteLine(messageCount);

                /* Create a list to contain the messages */
                List <Message> allMessages = new List <Message>();

                /* A loop to calculate the progress of retriving the Emails  */
                for (int i = messageCount; i > 0; i--)
                {
                    allMessages.Add(client.GetMessage(i));
                    percentComplete = Convert.ToInt16((Convert.ToDouble(allMessages.Count) / Convert.ToDouble(messageCount)) * 100);
                    (sender as BackgroundWorker).ReportProgress(percentComplete);
                }
                e.Result = allMessages;
            }
        }
Beispiel #4
0
        public void button1_Click(object sender, EventArgs e)
        {
            Properties.Settings.Default.UserName = textBox1.Text.ToString();
            Properties.Settings.Default.Password = textBox2.Text.ToString();

            lbAuth.Text         = "";
            pbxAuthOk.Visible   = false;
            pbxAuthFail.Visible = false;


            try
            {
                using (Pop3Client client = new Pop3Client())
                {
                    // Connect to the server  pop.myopera.com
                    client.Connect("pop.myopera.com", 995, true);

                    // Authenticate ourselves towards the server /HejHej
                    client.Authenticate(Properties.Settings.Default.UserName, Properties.Settings.Default.Password);

                    if (client.Connected == true)
                    {
                        pbxAuthOk.Visible = true;
                        lbAuth.Text       = "Authentication OK!";
                        Properties.Settings.Default.Save();
                    }

                    client.Disconnect();
                }
            }
            catch (Exception)
            {
                pbxAuthFail.Visible = true;
                lbAuth.Text         = "Something's wrong! \nMaybe bad username or password..";
                Properties.Settings.Default.UserName = null;
                Properties.Settings.Default.Save();

                if (Properties.Settings.Default.UserName != null)
                {
                    lbCurrentUsr.Text = Properties.Settings.Default.UserName;
                }
                else
                {
                    lbCurrentUsr.Text = "No Current user is saved.";
                }
            }
        }
        // Run by Worker, gets messages into a list.
        private static void fetchAllMessages(object sender, DoWorkEventArgs e)
        {
            try
            {
                int percentComplete;
                // The client disconnects from the server when being disposed
                using (Pop3Client client = new Pop3Client())
                {
                    // Connect to the server  pop.myopera.com
                    client.Connect("pop.myopera.com", 995, true);

                    // Authenticate ourselves towards the server /HejHej
                    client.Authenticate(Properties.Settings.Default.UserName, Properties.Settings.Default.Password);


                    // Get the number of messages in the inbox
                    int messageCount = client.GetMessageCount();

                    // We want to download all messages
                    List <OpenPop.Mime.Message> allMessages = new List <OpenPop.Mime.Message>();

                    // Messages are numbered in the interval: [1, messageCount]
                    // Ergo: message numbers are 1-based.
                    // Most servers give the latest message the highest number

                    for (int i = messageCount; i > 0; i--)
                    {
                        allMessages.Add(client.GetMessage(i));
                        percentComplete = Convert.ToInt16((Convert.ToDouble(allMessages.Count) / Convert.ToDouble(messageCount)) * 100);
                        (sender as BackgroundWorker).ReportProgress(percentComplete);
                    }
                    // Now return the fetched messages to e
                    e.Result = allMessages;
                    client.Disconnect();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Connection Failed, maybe you forgot to set a user in Settings?" + ex.Message);
            }
        }
        public static List <Message> GetAllMails(string hostname, int port, bool useSsl, string username, string password)
        {
            using (Pop3Client client = new Pop3Client())
            {
                client.Connect(hostname, port, useSsl);

                client.Authenticate(username, password);

                int messageCount = client.GetMessageCount();

                Debug.WriteLine(messageCount);

                List <Message> allMessages = new List <Message>(messageCount);

                for (int i = messageCount; i > 0; i--)
                {
                    allMessages.Add(client.GetMessage(i));
                    Debug.WriteLine(client.GetMessage(i).MessagePart);
                }
                return(allMessages);
            }
        }
        private string ReceiveMails()
        {
            try
            {
                if (pop3Client.Connected)
                {
                    pop3Client.Disconnect();
                }
                pop3Client.Connect(ConfigurationManager.AppSettings["Server"], int.Parse(ConfigurationManager.AppSettings["Port"]), Convert.ToBoolean(ConfigurationManager.AppSettings["SSL"]));
                pop3Client.Authenticate(ConfigurationManager.AppSettings["Login"], ConfigurationManager.AppSettings["Password"]);
                int count = pop3Client.GetMessageCount();
                messages.Clear();
                int success = 0;
                int fail    = 0;
                for (int i = count; i >= 1; i -= 1)
                {
                    try
                    {
                        Message message = pop3Client.GetMessage(i);
                        // Add the message to the dictionary from the messageNumber to the Message

                        //check if the message id is present in the database

                        DataSet ds = new DataSet();//GetAllMessageID();//Database call for getting all the messageid stored in database
                        ds = FileController.GETMessagesByMsgID(message.Headers.MessageId);
                        if (ds.Tables[0].Rows.Count == 0)
                        {
                            messages.Add(i, message);
                            //GetAttachmentsFromMessage(message);
                        }
                        success++;
                    }
                    catch (Exception e)
                    {
                        DefaultLogger.Log.LogError(
                            "TestForm: Message fetching failed: " + e.Message + "\r\n" +
                            "Stack trace:\r\n" +
                            e.StackTrace);
                        fail++;
                    }
                }
                if (messages.Count > 0)
                {
                    // Create message replies
                    List <MailMessage> replies = new List <MailMessage>();
                    foreach (var msg in messages)
                    {
                        replies.Add(CreateReply(msg.Value));
                    }

                    // Send replies
                    SendReplies(replies);
                }
                if (fail > 0)
                {
                    return("Failed to fetch the mails from pop3 server");
                }
                else
                {
                    return("Mails received successfully");
                }
            }
            catch (InvalidLoginException)
            {
                return("The server did not accept the user credentials!");
            }
            catch (PopServerNotFoundException)
            {
                return("The server could not be found");
            }
            catch (PopServerLockedException)
            {
                return("The mailbox is locked. It might be in use or under maintenance. Are you connected elsewhere?");
            }
            catch (LoginDelayException)
            {
                return("Login not allowed. Server enforces delay between logins. Have you connected recently?");
            }
            catch (Exception e)
            {
                return("Error occurred retrieving mail. " + e.Message);
            }
            finally
            {
            }
        }