コード例 #1
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.";
                }
            }
        }
コード例 #2
0
        // 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);
            }
        }
コード例 #3
0
        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
            {
            }
        }