public static List <Message> GetUnreadMessages(string hostname, int port, bool useSsl, string username, string password) { using (Pop3Client client = new Pop3Client()) { // Connect to the server client.Connect(hostname, port, useSsl); // Authenticate ourselves towards the server client.Authenticate(username, password); // Get the number of messages in the inbox int messageCount = client.GetMessageCount(); // We want to download all messages List <Message> allMessages = new List <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)); } // Now return the fetched messages return(allMessages); } }
// Helping methods for Pop3 Client user Interface private void ReceiveEmails() { try { Pop3Client pop_client = new Pop3Client(); pop_client.Pop3Server = this.PopServer.Text; pop_client.Pop3Port = Convert.ToInt32(this.PopPort.Text); pop_client.UserName = this.PopUserName.Text; pop_client.Password = this.PopPassword.Text; this.EnableDisableConnectButton(false); pop_client.ConnectionEstablishing += new ConnectEventHandler(this.pop_ConnectionEstablishing); pop_client.ConnectionEstablished += new ConnectEventHandler(this.pop_ConnectionEstablished); pop_client.AuthenticationBegan += new AuthenticateEventHandler(this.pop_AuthenticationBegan); pop_client.AuthenticationFinished += new AuthenticateEventHandler(this.pop_AuthenticationFinished); pop_client.StartedDataReceiving += new DataReceivingEventHandler(this.pop_StartedDataReceiving); pop_client.EndedDataReceiving += new DataReceivingEventHandler(this.pop_EndedDataReceiving); pop_client.Disconnected += new DisconnectEventHandler(this.pop_Disconnected); this.pop = pop_client; pop_client.Connect(); this.EnableDisableDisconnectButton(true); pop_client.GetMailBoxDetails(); //it sets the TotalEmails and TotalEmailSize properties if (pop_client.TotalEmails >= 1) { this.UpdateStatusBar("Retrieving Emails"); this.FillInboxListView(pop_client); this.UpdateStatusBar("Email Client"); } else { this.UpdateStatusBar("Email Client"); MessageBox.Show(this, "No email message exists in the inbox!.", "Email Client", MessageBoxButtons.OK, MessageBoxIcon.Information); } } catch (SmtpClientException err) { MessageBox.Show(this, err.ErrorMessage, "Email Client", MessageBoxButtons.OK, MessageBoxIcon.Error); this.UpdateStatusBar("Email Client"); this.EnableDisableConnectButton(true); this.EnableDisableDisconnectButton(false); } }